Multiline regex match for EPL labels

Viewed 12

EPL label's format looks like this:

N                      // START OF LABEL 1
.... EPL CODE ....     // LABEL CODE
P#                     // END OF LABEL 1, BEING # A NUMBER
N                      // START OF LABEL 2
.... EPL CODE ....     // LABEL CODE
P#                     // END OF LABEL 2, BEING # A NUMBER
                       // EMPTY LINE

I want to split this text into two labels, so I wrote a regular expression: /^N$.+?^P\d$/gsm. And I tested it at regexr.com.

Then, I tried to code it into my application but I had no matches. This is my code simplified:

var epl = "......"; // EPL LABEL TEXT
var regexPattern = @"^N$.+?^P\d$";
var regexOptions = RegexOptions.Singleline | RegexOptions.Multiline;
var regex = new Regex(regexPattern, regexOptions);

var matches = regex.Matches(epl);
Console.WriteLine($"Found {matches.Count} matches");    // -> Found 0 matches

If I replace the regex by @"N.+?P\d" it works, except if P# or N are part of the EPL label code. I can get it to work if I use [\r|\n]+ instead of $ and ^ symbols, but I wonder why is this code failing.

Working pattern by now: @"(N[\r|\n]+)(.+?)([\r|\n]+P\d[\r|\n]+)". It's valid, but I'd like to get the $ and ^ symbols to work.

0 Answers
Related