Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Help modifying recursive regex

by hdb (Monsignor)
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032525]
[marto]: Corion if your device has an official Oreo ROM I'd do a nandroid backup and give it a shot
[marto]: I've been impressed with the Star Citizen thing, I'll likely never play it, but they've achieved something really special I think, in terms of what has been shown so far.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2017-12-14 10:47 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (389 votes). Check out past polls.