Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-11-03
2
127 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…

791 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