I have a string that may contain a separator. This probably means that I have three cases:
First and only paragraph
First and only paragraph with redundant separator
<!-- separator -->
First paragraph
<!-- separator -->
Second paragraph
I need a regular expression that returns the first and second paragraph (if present) — separately. Here is what I have tried so far:
(.*)(<!-- separator -->.*) // fails case 1
(.*)(<!-- separator -->.*)? // fails case 2 and 3 (surprise)
I just need the pattern.
The problem is probably, that the
.does not match newline characters by default. You can change this by using the modifiers.sis the single line modifier, it makes the regex treat the input string as “singleline”, means the.will also match newline characters.But you should then make the quantifier ungreedy, otherwise the first
.*will match till the last<!-- separator -->in the string.