http://www.perlmonks.org?node_id=1041617


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.