"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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

NevSoFlyAuthor Commented: 2014-11-06

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 ().

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.