0

I need to use a multiline regex to extract part of a text, but I also have to allow those parts to be nested.

Here's an example of my scenario:

<!--BEGIN DELIMITER(A)-->
    sample text
    <!--BEGIN DELIMITER(B)-->
        another sample text
    <!--END DELIMITER(B)-->
<!--END DELIMITER(A)-->

The expected result would be to extract both blocks, including the begin/end lines as A1 and A2 are required for other things.

What happens is that I only get one match, which is the outer group.

Here's my regex:

<!--BEGIN DELIMITER\((?<expr>(\w+))\)-->(?<content>([\s\S]*?))<!--END DELIMITER\((\k<expr>.*)\)-->

Here's a regex101.com link for a quick visual reference.

Any idea on how to match both groups?

STT
  • 215
  • 1
  • 11

0 Answers0