Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Help modifying recursive regex

by hdb (Prior)
on May 07, 2013 at 19:11 UTC ( #1032525=note: print w/ replies, xml ) Need Help??


in reply to Help modifying recursive regex

Check the following Re^2: splitting data. Changing the code slightly will give you trees based on levels of parantheses.

use strict; use warnings; while(my $input = <DATA>){ print "input: $input\n"; my $level = 0; my $tab = "| "; my %action = ( '(' => sub { print "\n", $tab x ++$level, shift }, ')' => sub { print "\n", $tab x $level--, shift }, 'default' => sub { print shift }, ); ( $action{$_} // $action{'default'} )->($_) for $input =~ /./g; print "\n\n\n"; } __DATA__ A (B C D) A (B C D) (E F G) A B (C (D E) (F G)) (H I)

with the following output

input: A (B C D) A | (B C D | ) input: A (B C D) (E F G) A | (B C D | ) | (E F G | ) input: A B (C (D E) (F G)) (H I) A B | (C | | (D E | | ) | | (F G | | ) | ) | (H I | )

not based on regex though... but if you now replace parantheses on level 1 with quotes and put quotes around words on level 0 you should have what you want. Just change the subs in %action as required.


Comment on Re: Help modifying recursive regex
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-12-21 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (104 votes), past polls