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: 147
  • Last Modified:

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

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
NevSoFly
Asked:
NevSoFly
1 Solution
 
ozoCommented:
^[^(]*?((((-?\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
 
NevSoFlyAuthor Commented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now