NevSoFly
asked on
Pattern not working on all test cases.
I am using the pattern (?<=\+)\{([^}+]*)\}(?=\+) with global flag set to find sets of +{}+ within an equation and replace them with ++. This works on simple equations like :
a+{8e-2s}+b=x matches {8e-2s} and produces a+8e-2s+b=x
and
a+{8e-2s-h}+b=x matches {8e-2s-h} and produces a+8e-2s-h+b=x
but
a+{8e-2s-h+9}+b=x matches nothing.
When I make the equation slightly more complex I get the following:
a+{8e-2s-{h}+9}+b=x matches {8e-2s-{h} and produces a+8e-2s-{h+9}+b=x.
I want it to match {8e-2s-{h}+9} and produce a+8e-2s-h+9+b=x
Does anyone have any tips for me?
a+{8e-2s}+b=x matches {8e-2s} and produces a+8e-2s+b=x
and
a+{8e-2s-h}+b=x matches {8e-2s-h} and produces a+8e-2s-h+b=x
but
a+{8e-2s-h+9}+b=x matches nothing.
When I make the equation slightly more complex I get the following:
a+{8e-2s-{h}+9}+b=x matches {8e-2s-{h} and produces a+8e-2s-{h+9}+b=x.
I want it to match {8e-2s-{h}+9} and produce a+8e-2s-h+9+b=x
Does anyone have any tips for me?
Your third does not match because you have a + inside of the braces, and your pattern is explicitly checking for not-plus ( [^}+] ). What are the rules for your searching? Perhaps searching for simply not-brace ( [^}] ) would suffice?
how about using sed
cat file name | sed s'/[\{\}]/ /g'
cat file name | sed s'/[\{\}]/ /g'
How complex an equation do you need it to work for?
Do you need it to handle
a+{8e-2s-{{h}}+9}+b=x
or
a+{8e-2s-{{8e-2s-{h}}+9}+b =x
or
a+{8e-2s-{{8e-2s-{{8e-2s}} }+9}+b=x
is there a limit to how deeply nested the {} can be that the expression needs to handle?
and even if it was able to parse nested {} I don't understand how
{8e-2s-{h}+9} can match, when it contains a '+'
Do you need it to handle
a+{8e-2s-{{h}}+9}+b=x
or
a+{8e-2s-{{8e-2s-{h}}+9}+b
or
a+{8e-2s-{{8e-2s-{{8e-2s}}
is there a limit to how deeply nested the {} can be that the expression needs to handle?
and even if it was able to parse nested {} I don't understand how
{8e-2s-{h}+9} can match, when it contains a '+'
ASKER
Thanks for your responses.
@kaufmed,
rules: find complete sets of +{}+. These sets may contain nested sets of {} or +{}+. But I all sets of +{}+ to be replaced with ++.
@ozo,
I need it to work for any and all equations. There is no limit to how deep the sets are nested.
@kaufmed,
rules: find complete sets of +{}+. These sets may contain nested sets of {} or +{}+. But I all sets of +{}+ to be replaced with ++.
@ozo,
I need it to work for any and all equations. There is no limit to how deep the sets are nested.
If those are the rules, then my previous assertion holds: Use a not-brace construct:
e.g.
That being said, your comment to ozo now complicates the issue. Regex is not good for balance matching--that is, trying to match a brace's (or another character) matching closing brace. Some regex implementations (like PHP) give you the option to use balancing groups, but not all engines do so. This is more of a parsing question than a regex question.
What programming language or text editor are you using to accomplish this task?
e.g.
(?<=\+)\{([^}]*)\}(?=\+)
That being said, your comment to ozo now complicates the issue. Regex is not good for balance matching--that is, trying to match a brace's (or another character) matching closing brace. Some regex implementations (like PHP) give you the option to use balancing groups, but not all engines do so. This is more of a parsing question than a regex question.
What programming language or text editor are you using to accomplish this task?
Using regular expression to handle arbitrarily nested sets of balanced {} can require certain tricks that can differ depending on what kinds of regular expressions you are using, with what tools you are using the regular expressions.
ASKER
Guys,
I am using VB.net.
I tried the pattern (?<=\+)\{([^}]*)\}(?=\+) and it worked on all but one of the test cases in my original post.
The test case it didn't work on was a+{8e-2s-{h}+9}+b=x it still only matched {8e-2s-{h} and I need it to match {8e-2s-{h}+9}.
I am using VB.net.
I tried the pattern (?<=\+)\{([^}]*)\}(?=\+) and it worked on all but one of the test cases in my original post.
The test case it didn't work on was a+{8e-2s-{h}+9}+b=x it still only matched {8e-2s-{h} and I need it to match {8e-2s-{h}+9}.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the delay my system was down. I am going to have to learn about balancing (as if regular expressions wasn't bad enough) or come up with some other method. This is definitely not as easy as I first thought.