Reputation: 321
I have regex:
(?<=<start>\n)([^\n]+\n)+(?=<end>)
And the text:
<start>
123
456
789
<end>
Expected result is:
Actual result is:
How can I modify my regex to get expected result?
Upvotes: 0
Views: 57
Reputation: 786091
Using PCRE regex engines that support \G
:
(?:<start>(?=[\s\S]*<end>)|(?<!\A)\G)\s\K(?!<end>)\S+
Regex Details:
(?:
: Start non-capture group<start>
: Match <start>
(?=[\s\S]*?<end>)
: Assert that we have a line <end>
ahead of us|
: OR(?<!\A)\G)
: Start from the end of the previous match\s
: Match a whitespace\K
: Reset match info(?!<end>)
: Don't match <end>
\S+
: Match 1+ non-whitespace charactersAlternatively, if you're using Javascript that now support dynamic length lookbehind like .Net
:
(?<=<start>\n[^]*)^.+(?=[^]*<end>)
Upvotes: 2
Reputation: 782498
When you quantify a capture group, it only captures the last occurrence.
The trick is to put a capture group around the quantified group to get all of them.
(?<=<start>\n)((?:[^\n]+\n)+)(?=<end>)
Upvotes: 0