Solved
Need pattern to match a polynomial (string) not enclosed in () and containing a multiplication or division operator.
Posted on 2014-11-04
I have the following pattern that matches string expressions not enclosed in () starting at the beginning of the string:
^[^(]*?((((-?\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>)?)))[^)]*$
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) No Match
(2m*5/2z*p/3n)-6m/3m+2x/3m*45*h No Match
(2m*5/2z*p/3n)-6m/3m+2x/3m*45*h(2m*5/2z*p/3n) No Match
What I need is a pattern that matches the first part of a string expression that is not enclosed in ()
The following are some 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
I had prior assistance on this but I failed to realize that the examples I gave did not cover all the possible combinations. I think I've got them covered now. Any advice is welcome.