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))"]
(Of course, if you use something like this, do thorough testing to
be sure it really does what you want. :)
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: <%-{-{-{-<
|