I am making a simple numeric expression solver using regexes and right now I’m working at splitting polynomials into its terms. So this is what I got so far:
(.*?)([\+-](.*?))+
This doesn’t work when negative numbers are involved. Take 3*-2+1 as an example: the terms I get are 3*, -2 and +1, which is obviously wrong.
I thought I could get away with a negative look behind before the sign so that signs preceded by * or / are discarded:
(.*?)((?<![\*/])[\+-](.*?))+
But this doesn’t even work with positive numbers
Suggestions?
Hope you didn’t spend a lot of time creating your own parser 😉
I use this code to evaluate expressions:
Will output:
-5It is based on the
XPathNavigator.Evaluatemethod. The Regex adds some spaces to the input and then division and modulo symbol are replaced.