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
     a+{8e-2s-h}+b=x matches {8e-2s-h} and produces a+8e-2s-h+b=x
     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?
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
.NET has very crude support for balanced matching. You can try something like the following article that describes how to make balancing matches, but I really think you'd be better off writing a crude parser. You could look into the source of the second article below to get an idea of how to parse expressions.
käµfm³d 👽Commented:
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'
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

How complex an equation do you need it to work for?
Do you need it to handle
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 '+'
NevSoFlyAuthor Commented:
Thanks for your responses.

rules: find complete sets of +{}+.  These sets may contain nested sets of {} or +{}+.  But I all sets of +{}+ to be replaced with ++.

I need it to work for any and all equations.  There  is no limit to how deep the sets are nested.
käµfm³d 👽Commented:
If those are the rules, then my previous assertion holds: Use a not-brace construct:



Open in new window

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.
NevSoFlyAuthor Commented:

I am using

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}.
NevSoFlyAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.