monkprentice has asked for the wisdom of the Perl Monks concerning the following question:
"1*(2+3)*(3+4)+5*(6+7)"
and I need to find the first occurrence of "+" that is not enclosed within parenthesis, i.e. the one before "5", how would I do that with a regular expression ?
Is it possible ?
I tried something like:
$term =~ /((?:[^\+]|(?:\(.*\)))*)$_(.*)/Which is supposed to mean:
Skip as many sub-patterns of the form ("no plus signs" OR "any subterm in parenthesis")
then match the rest in a group.
What it actually does, is to stop at the first "+" it comes across.
I had hoped that the greedy behaviour would make it match the second option (any subterm in parenthesis) instead and skip the (2+3) part entirely.
I solved it now in a loop, but I was wondering if there was some elegant way to do it in a regular expression.
thanks in advance,
mp
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: first match that is not enclosed in parenthesis
by james2vegas (Chaplain) on Jun 30, 2013 at 10:17 UTC | |
|
Re: Regex: first match that is not enclosed in parenthesis
by rjt (Curate) on Jun 30, 2013 at 11:31 UTC | |
|
Re: Regex: first match that is not enclosed in parenthesis
by LanX (Saint) on Jun 30, 2013 at 12:51 UTC | |
by monkprentice (Novice) on Jun 30, 2013 at 13:53 UTC | |
by hdb (Monsignor) on Jun 30, 2013 at 16:53 UTC | |
by LanX (Saint) on Jun 30, 2013 at 16:23 UTC | |
|
Re: Regex: first match that is not enclosed in parenthesis
by monkprentice (Novice) on Jun 30, 2013 at 17:04 UTC | |
|
Re: Regex: first match that is not enclosed in parenthesis
by monkprentice (Novice) on Jun 30, 2013 at 12:26 UTC |