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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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'
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 '+'
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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}.
käµfm³d 👽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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.