Solved

Need pattern to match a polynomial (string) not enclosed in () and containing a multiplication or division operator.

Posted on 2014-11-04
9
182 Views
Last Modified: 2014-11-11
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.
0
Comment
Question by:NevSoFly
  • 5
  • 3
9 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40425735
And what's about  ((2m*5/2z*p/3n)-6m/3m+2x/3m*45*h) ?
0
 

Author Comment

by:NevSoFly
ID: 40426153
Thanks for your response, but I don't understand your question.

If need be I can simplify my needs to just identifying the stings where all or part of the expressions are not inside ().  Then I can use code to process the string.  example:

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

Author Comment

by:NevSoFly
ID: 40426218
I have gotten the opposite result using \(([^()]*)\).  I just don't know how to reverse this.

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

Expert Comment

by:sarabande
ID: 40426531
what is the purpose of the task? what do you intend to do with the matched expressions?

you now have a regex expression which is in no way verifiable and where you easily found some sample expressions where it doesn't work as expected. you would have the same situation if someone found a regex expression which would match correctly for the additional patterns you posted, but probably not the one ste5an has posted. so, you end with an overkill regex expression where you never will know whether it would fail or succeed.

in my opinion, regex is not suitable for to parse mathematical expressions of a non-trivial complexity.

math expressions normally were parsed by recursively dividing expressions into sub expressions and build an array of operations which contain of two operands and one operator.

for example the expression

p/3n-(6m/3m+2x)/3m*45

would be parsed to

1:   p   3n  /
2: 6m 3m /
3: @2 2x  +
4: @3 3m /
5: @4 45  * 
6: @1 @5 -

Open in new window


where @x points to the x-th element of the array. with the array and known input parameters the expression easily could be calculated. it also could be turned back to a string expression which has all parentheses:

(p/3n-((((6m/3m)+2x)/3m)*45))

Sara
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Accepted Solution

by:
NevSoFly earned 0 total points
ID: 40427038
Sara,
Thanks, you are absolutely right it was overkill and overly complicated.  I did however find a solution.  

1.

I used  \(([^()]*)\) to test the input string for expressions enclosed in ().

2.

I then replaced the match with "".

3.

I used code to check if the length of the resulting string was >2 since 3 characters would make the simplest expression (eg. 1*1).

4.

I then used ((?<!<)/)|(\*) to checked if the resulting string contained a multiplication sign or a division sign that wasn't part of an html tag.If the length was >2 and the resulting string contained a * or /(not in html tag) then the input string had to contain a polynomial not in ().
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40428171
hmm, I still don't get it..

Using your procedure, your (2m*5/2z*p/3n)-6m/3m+2x/3m*45*h will return  -6m/3m+2x/3m*45*h as rest.
My  ((2m*5/2z*p/3n)-6m/3m+2x/3m*45*h) has no rest term.
0
 

Author Comment

by:NevSoFly
ID: 40430832
ste5an, I basically only needed to know if the expression had a subexpression that was not in ().
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40431081
In this case counting parenthesis is faster.
0
 

Author Closing Comment

by:NevSoFly
ID: 40434591
This was the only solution I had.
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

Title # Comments Views Activity
how to use laptop or pad camera in vb.net windows application 2 54
Server Error 11 47
Regex - Ignoring Spaces 7 14
What namespace do I need to import? 2 8
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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

16 Experts available now in Live!

Get 1:1 Help Now