Solved

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

Posted on 2014-11-03
2
123 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

895 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

13 Experts available now in Live!

Get 1:1 Help Now