Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: regex to parse (nested) parenthesis delimited string?

by fenLisesi (Priest)
on Feb 06, 2007 at 18:11 UTC ( #598606=note: print w/ replies, xml ) Need Help??


in reply to regex to parse (nested) parenthesis delimited string?

You could use a canned /regexp?/i as follows:

use strict; use warnings; use Regexp::Common qw(balanced); my @streams = ( '(aaa) (bbb (ccc( ddd) eee)) (fff)', ' (a) ((bc (d)) ef) h', '(a)', '(a) ', '(a) (bc)', '', ); my $PATTERN = $RE{balanced}{-keep}; for my $input (@streams) { print qq("$input" => ); my @pieces = ($input =~ /$PATTERN/g); printf qq(%s\n), join qq( ), map {qq("$_")} @pieces; }
which prints:
"(aaa) (bbb (ccc( ddd) eee)) (fff)" => "(aaa)" "(bbb (ccc( ddd) eee))" + "(fff)" " (a) ((bc (d)) ef) h" => "(a)" "((bc (d)) ef)" "(a)" => "(a)" "(a) " => "(a)" "(a) (bc)" => "(a)" "(bc)" "" =>
This assumes that you had a typo in your post and you actually want balanced parens. Cheers.


Comment on Re: regex to parse (nested) parenthesis delimited string?
Select or Download Code
Replies are listed 'Best First'.
Re^2: regex to parse (nested) parenthesis delimited string?
by varian (Chaplain) on Feb 07, 2007 at 08:25 UTC
    Thanks for the pointer, very useful module indeed and it provides what I needed!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-08 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls