Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Need pattern matching expressions within ()s that contain at least one "+".

This seems like it should be the simplest thing in the world.  I am looking for a pattern to match expressions within ()s that contain at least one "+".
0
NevSoFly
Asked:
NevSoFly
  • 4
  • 3
  • 2
  • +1
1 Solution
 
sentnerCommented:
This should work, though does not account for nested parentheses.

[(][^)]*[+][^)]*[)]

Open in new window

0
 
Marco GasiFreelancerCommented:
Something like this? \([^)]*\+\)
0
 
NevSoFlyAuthor Commented:
@Sentner

I tried your suggestion but it didn't find any matches.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NevSoFlyAuthor Commented:
@ Marco Gasi

I tried your suggestion too but still no matches.
0
 
sentnerCommented:
Are you using it inside of a script or such?  I tested with sed, and it worked fine:

> echo "this+ (is a test) (+match this) (But not this)"| sed -e 's/[(][^)]*[+][^)]*[)]/here/'
this+ (is a test) here (But not this)
0
 
ozoCommented:
What was the string in which you were trying to find the matches?
0
 
NevSoFlyAuthor Commented:
I am using it inside a VB.net function.
I am using http://www.regexr.com/ to test the patterns and my test string is "(2d)(4)(2a+2n)+(3xz)-(-4x)-(2a+2n)".
0
 
ozoCommented:
Sentner's suggestion seemed to work for me when I tested it there on that string.
0
 
Marco GasiFreelancerCommented:
I posted my (wrong) solution without seeing Sentner's one: now I have tested it and I agree with ozo, it works fine in RegexBuddy.
0
 
sentnerCommented:
Can you share the syntax you're using in your function?

I tested with your string using command line and it worked:

> echo "(2d)(4)(2a+2n)+(3xz)-(-4x)-(2a+2n)" |  sed -e 's/[(][^)]*[+][^)]*[)]/here/g'
(2d)(4)here+(3xz)-(-4x)-here
0
 
NevSoFlyAuthor Commented:
Sentner's suggestion did work after all.  When I copied and pasted it into regexr.com I didn't notice an empty space at the end.  Once I deleted it everything worked.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now