Solved

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

Posted on 2014-01-22
11
207 Views
Last Modified: 2014-01-22
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
Comment
Question by:NevSoFly
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 14

Accepted Solution

by:
sentner earned 500 total points
ID: 39800729
This should work, though does not account for nested parentheses.

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

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39800733
Something like this? \([^)]*\+\)
0
 

Author Comment

by:NevSoFly
ID: 39800746
@Sentner

I tried your suggestion but it didn't find any matches.
0
 

Author Comment

by:NevSoFly
ID: 39800759
@ Marco Gasi

I tried your suggestion too but still no matches.
0
 
LVL 14

Expert Comment

by:sentner
ID: 39800796
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Expert Comment

by:ozo
ID: 39800800
What was the string in which you were trying to find the matches?
0
 

Author Comment

by:NevSoFly
ID: 39800847
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
 
LVL 84

Expert Comment

by:ozo
ID: 39800881
Sentner's suggestion seemed to work for me when I tested it there on that string.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39800943
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
 
LVL 14

Expert Comment

by:sentner
ID: 39800950
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
 

Author Comment

by:NevSoFly
ID: 39801449
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now