Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: regex for nested "<"/">'

by clueless newbie (Curate)
on Feb 12, 2020 at 15:12 UTC ( [id://11112868]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
Re^2: regex for nested "<"/">'
by jo37 (Deacon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112868]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found