... 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:

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:  <%-{-{-{-<


In reply to Re^4: Matching nested parens by AnomalousMonk
in thread Matching nested parens by BernieC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.