Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

( #16794=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do I split a string on highly structured/nested data? 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.

Comment on Answer: How do I split a string on highly structured/nested data?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-18 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls