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

Answer: How do I split a string on highly structured/nested data?

by lhoward (Vicar)
on Jun 07, 2000 at 03:50 UTC ( #16794=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do I split a string on highly structured/nested data? - Answer contributed by lhoward

One approach is to use a parser like Parse::RecDescent. A real parser (as opposed to parsing a string with a regular expression alone) is much more powerful and can be more apropriate for parsing highly structured/nested data like your example.
use Parse::RecDescent; my $teststr="a,b,op2(c,d),op3(e,op4(f,g))"; my $grammar = q { content: /[^\)\(\,]+/ function: content '(' list ')' value: content item: function | value list: item ',' list | item startrule: list }; my $parser = new Parse::RecDescent ($grammar) or die "Bad grammar!\n"; defined $parser->startrule($teststr) or print "Bad text!\n";
For other approaches see the discussion on Balancing Parens.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-04-21 04:37 GMT
Find Nodes?
    Voting Booth?