in reply to regex for nested "<"/">'

I should like to thank all of you for your "help" -- I say "help" because you guys did all the real work!

What I am really after is getting a regex similar to Abigail's where "begin"/"end" must be complete words ie "\bbegin\b" and "\bend\b". Note that Abigail's will accept "beginning a bend".

To the Damian, I apologize for my poor transcription ... but I worked from screen shot and *+ was the best my poor 70 year old eyesight told me.

Replies are listed 'Best First'.
Re^2: regex for nested "<"/">'
by jo37 (Curate) on Feb 12, 2020 at 21:42 UTC
    For balanced begin/end matching, this might do:
    $re = qr{ ( (?: (?&OPEN) (?: (?>(?:.*?(?=(?&OPEN)|(?&CLOSE)))) | (?-1) )* (?&CLOSE) ) ) (?(DEFINE) (?<OPEN>\bBEGIN\b) (?<CLOSE>\bEND\b) )}x;

      You're my hero!

      Now to figure out exactly how it works!

Re^2: regex for nested "<"/">'
by tybalt89 (Monsignor) on Feb 13, 2020 at 17:00 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11112811 use warnings; my $input = <<ENDOFSTRING; beginning a bend before stuff begin one begin three end five end after stuff beginning a bend ENDOFSTRING print $input, '-' x 70, "\n"; my $level = 0; my $nested = $input =~ s/(\bbegin\b)|(\bend\b)|./ ( $1 && $level++, $level ? $& : '', $2 && $level--)[1] /gesr; print "$nested\n";

    Outputs:

    beginning a bend before stuff begin one begin three end five end after stuff beginning a bend ---------------------------------------------------------------------- begin one begin three end five end