Reputation: 485
I am trying to write a regex (which is never a good idea for me to try) that finds a newline pattern, that is not followed by the character <
So the pattern would only match this string once:
some text\nsome more text\n<end of file>
Any help?
Upvotes: 0
Views: 1361
Reputation: 1917
This should work
(?s).*(?=<)
The (?s) switches into the single line mode, allowing the . to match newlines, and the positive lookahead allows the expression to stop at the < without consuming it (i.e. adding it to the match)
The thing to remember here is that the * is greedy, that is, it will will stop at the last match, not the first. So if you have the <end of file>
twice in the same file, it will match all text up to the second one.
If you wanted to catch repeating eof tags (with the eof tag), you could do something like this
(?s)([^>]+>)
Or without the eof tags
(?s)([^<]+)(?:[^>]+>)
In both cases we are using a negated character class to match any character except the < or >. We stop when one of those is hit, and start a new match.
Upvotes: 1