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

Finding in my case means to return the prefix and postfix substrings.

I also thought of this. This way one could just find the substring before and after the retrieved position.
Good Idea.

  • Comment on Re^2: Regex: first match that is not enclosed in parenthesis

Replies are listed 'Best First'.
Re^3: Regex: first match that is not enclosed in parenthesis
by hdb (Monsignor) on Jun 30, 2013 at 16:53 UTC

    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.

Re^3: Regex: first match that is not enclosed in parenthesis
by LanX (Saint) on Jun 30, 2013 at 16:23 UTC
    Sounds like what you really need is a lexer.

    hdb regularly posts such solutions based on dispatch tables, worth searching for...

    HTH! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)