简单的正则表达式需要

时间:2011-11-01 09:22:13

标签: c# .net regex

我有文本文件

01:02:39.952 MC:My text with several lines
01:02:39.952

如何捕捉01:02:39.952 MC:01:02:39.952

之间的所有文字

我在这个模式的文本文件中有很多这样的行,我想抓住所有这些。

1 个答案:

答案 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();
} 
相关问题