1

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.

Antonio Rodríguez
  • 976
  • 2
  • 11
  • 25
  • 1
    The only thing you need to fix is add `\r?` before `$`. Just FYI: if there is really a *number* after `P`, you should use `\d+`. And consider using `RegexOptions.ECMAScript` option, too, so that `\d` could only match ASCII digits. – Wiktor Stribiżew Dec 10 '21 at 09:55

0 Answers0