Solved

Problems modifying a pattern to not match when certain characters are present before and after a string.

Posted on 2014-11-03
2
120 Views
Last Modified: 2014-11-04
I have the following regex that matches polynomials.  (eg.. (2x/3m), 2x/3m, 2x/3m*45*h, (2m+5-2z*p/3n)).
(?<!\()((((-?\d+(?:\.\d*)?)(<sup>(-?\d+)</sup>)?(<sup>E[-+]\d+</sup>)?(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?))?|(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?)))[*|/|+|-])+)(?!\*\/)((-?\d+(?:\.\d*)?)(<sup>(-?\d+)</sup>)?(<sup>E[-+]\d+</sup>)?(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?))?|(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?)))(?!\))

Open in new window

It matches all expressions weather they are enclosed in parenthesis or not.
pPolynomial                          Match Found
(2x/3m)                                            2x/3m
2x/3m                                              2x/3m
2x/3m*45*h                                   2x/3m*45*h
(2m+5-2z*p/3n)                             2m+5-2z*p/3n

I want to modify it so that it only matches expressions not enclosed in parenthesis.
Polynomial                          Match Found
(2x/3m)                                            no match
2x/3m                                              2x/3m
2x/3m*45*h                                   2x/3m*45*h
(2m+5-2z*p/3n)                             no match

I attempted to do a negative look behind (?<!\() and a negative look ahead (?!\)) but when I did the following happened.
Polynomial                          Match Found
(2x/3m)                                            x/3
2x/3m                                              2x/3m
2x/3m*45*h                                   2x/3m*45*h
(2m+5-2z*p/3n)                             m+5-2z*p/3

Could someone please help me out with this?
0
Comment
Question by:NevSoFly
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40420828
^[^(]*?((((-?\d+(?:\.\d*)?)(<sup>(-?\d+)</sup>)?(<sup>E[-+]\d+</sup>)?(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?))?|(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?)))[*|/|+|-])+)(?!\*\/)((-?\d+(?:\.\d*)?)(<sup>(-?\d+)</sup>)?(<sup>E[-+]\d+</sup>)?(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?))?|(-?[a-z]((<sup>-?\d+</sup>)|(<sup>E[-+]\d+</sup>)?)))[^)]*$
0
 

Author Closing Comment

by:NevSoFly
ID: 40421606
Thank you.  You answered the question that I had asked.  Unfortunately after testing your solution I realized that there were instances that I did not originally think of and my question and examples were incomplete.  

What I actually need is a pattern that matches the first part of a string expression that is not enclosed in ()

The following are some but may not be all examples:

String Expressions                                                                           Matches
(2x/3m)                                                                                              No Match
2x/3m                                                                                                2x/3m
2x/3m*45*h                                                                                     2x/3m*45*h
(2m*5/2z*p/3n)                                                                               No Match
6m/3m+2x/3m*45*h-(2m*5/2z*p/3n)                                        6m/3m+2x/3m*45*h
(2m*5/2z*p/3n)-6m/3m+2x/3m*45*h                                        -6m/3m+2x/3m*45*h
(2m*5/2z*p/3n)-6m/3m+2x/3m*45*h(2m*5/2z*p/3n)            -6m/3m+2x/3m*45*h

If you like I can post another similarly titled question.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now