我有文本文件
01:02:39.952 MC:My text with several lines
01:02:39.952
如何捕捉01:02:39.952 MC:
和01:02:39.952
我在这个模式的文本文件中有很多这样的行,我想抓住所有这些。
答案 0 :(得分:4)
假设您的模式比这些数字更通用:
Regex regexObj = new Regex(
@"
(?<= # Assert that the following can be matched before the current position:
\b # start of ""word""
\d{2}:\d{2}:\d{2}\.\d{3} # nn:nn:nn.nnn
\sMC: # <space>MC:
) # End of lookbehind assertion
.*? # Match any number of characters (as few as possible)...
(?= # ...until the following can be matched after the current position:
\b # start of word
\d{2}:\d{2}:\d{2}\.\d{3} # nn:nn:nn.nnn
\b # end of word
) # End of lookahead assertion",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
matchResult = matchResult.NextMatch();
}