Night Walker
Night Walker

Reputation: 21280

Simple Regex needed

I have text file

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

How I can catch all the text between 01:02:39.952 MC: and 01:02:39.952

I have a lot of line like this in my text file with this pattern, I want to catch all of them.

Upvotes: 0

Views: 70

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

Assuming that your pattern is more general than exactly those numbers:

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();
} 

Upvotes: 4

Related Questions