Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Matching nested parens

by BernieC (Pilgrim)
on Oct 01, 2020 at 21:24 UTC ( [id://11122439]=note: print w/replies, xml ) Need Help??


in reply to Re: Matching nested parens
in thread Matching nested parens

Thanks for the advice. Looks like Regexp::Common is just what I wanted {I've just cpan installed it :o)}

Replies are listed 'Best First'.
Re^3: Matching nested parens
by BernieC (Pilgrim) on Oct 01, 2020 at 22:25 UTC
    A footnote. I was curious about Regexp::Common. So:
    use Regexp::Common qw/balanced/; say $RE{balanced}{-parens=>'()'} ;
    And I can now see why I didn't have a hope of figuring out the RE on my own... :o)
      ... 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:  <%-{-{-{-<

        I can see that I'm *WAY* behind the curve. I thought I about the various (? RE operators but I've heard of the ?R. Whew. Recursion inside a regular expression... I wonder if Perl's REs are Turing complete :o) Thanks!

Log In?
Username:
Password:

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

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

    No recent polls found