Matching math expression with regular expression?

Posted by Ethan on Stack Overflow See other posts from Stack Overflow or by Ethan
Published on 2010-04-07T19:23:10Z Indexed on 2010/04/07 19:43 UTC
Read the original article Hit count: 443

Filed under:

For example, these are valid math expressions:

a * b + c
-a * (b / 1.50)
(apple + (-0.5)) * (boy - 1)

And these are invalid math expressions:

--a *+ b @ 1.5.0  // two consecutive signs, two consecutive operators, invalid operator, invalid number
-a * b + 1)  // unmatched parentheses
a) * (b + c) / (d  // unmatched parentheses

I have no problem with matching float numbers, but have difficulty with parentheses matching. Any idea? If there is better solution than regular expression, I'll accept as well. But regex is preferred.

© Stack Overflow or respective owner

Related posts about regex