in reply to Re^3: Matching nested parens
in thread Matching nested parens
... I didn't have a hope of figuring out the RE on my own...
This problem is actually used as an an example of the use of (?R) and friends in perlre:
(Of course, if you use something like this, do thorough testing to be sure it really does what you want. :)Win8 Strawberry 5.30.3.1 (64) Thu 10/01/2020 19:06:47 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use 5.010; # need 5.10+ extended patterns (?R) use Data::Dump qw(dd); my $str = '(a a (a a)(a a))(b b(b)b b) xxx (c (c (c c)))'; my $rx_bal_parens = qr{ \( ( # Capture group 1 (contents of parens) (?: (?> [^()]+ ) # Non-parens without backtracking | (?R) # Recurse to start of regex )* ) \) }xms; my @bal_parens = $str =~ m{ $rx_bal_parens }xmsg; dd \@bal_parens; ^Z ["a a (a a)(a a)", "b b(b)b b", "c (c (c c))"]
Update: Fixed first line of code (the use 5.010; statement). It somehow got stuck onto the end of the command line during cut-pasting.
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Matching nested parens
by BernieC (Pilgrim) on Oct 06, 2020 at 15:29 UTC |