LyndaOlexandr
LyndaOlexandr

Reputation: 321

How to get separate groups using one capturing group

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

Answers (2)

anubhava
anubhava

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 characters

RegEx Demo


Alternatively, if you're using Javascript that now support dynamic length lookbehind like .Net:

(?<=<start>\n[^]*)^.+(?=[^]*<end>)

RegEx Demo 2

Upvotes: 2

Barmar
Barmar

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

Related Questions