Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Regex: first match that is not enclosed in parenthesis

by hdb (Prior)
on Jun 30, 2013 at 16:53 UTC ( #1041617=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Regex: first match that is not enclosed in parenthesis
in thread Regex: first match that is not enclosed in parenthesis

Something like this:

use strict; use warnings; my $input = "1*(2+3)*(3+4)+5*(6+7)"; my $level = 0; my $tab = "| "; my %action = ( '(' => sub { print "\n", $tab x ++$level, shift }, ')' => sub { print "\n", $tab x $level--, shift }, '+' => sub { print "This one>>>" unless $level; print +shift }, 'default' => sub { print shift }, ); ( $action{$_} // $action{'default'} )->($_) for $input =~ /./g;

The additional effort is only worthwhile if you have ambitions beyond your initial question.


Comment on Re^3: Regex: first match that is not enclosed in parenthesis
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-12-26 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (174 votes), past polls