Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by merlyn (Sage)
on Aug 17, 2000 at 10:09 UTC ( #28270=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 merlyn

lhoward's grammar seems unnecessarily complicated. Let's simplify it a bit, as well as grabbing what is needed for the answer (the split items):
use Parse::RecDescent; my $teststr="a,b,op2(c,d),op3(e,op4(f,g))"; my $grammar = q { startrule: list list: <leftop: item ',' item> item: word '(' list ')' <commit> { "$item[1](".join(",",@{$item[3]})." +)" } | word word: /\w+/ }; my $parser = new Parse::RecDescent ($grammar) or die "Bad grammar!\n"; + defined (my $result = $parser->startrule($teststr)) or print "Bad text +!\n"; print map "<<< $_ >>>\n", @$result;
Yes, there it is. $result is an array ref of the split-apart items.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[virtualsue]: 1nickt thatch repair just finishing
[virtualsue]: they've done enough to get it through winter

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-11-21 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (299 votes). Check out past polls.

    Notices?