One last Regex problem I need help with. What I am trying to do is be able to parse a math expression, while still having my Regex recognize unary symbols. I am using the following to parse an integer:
[\+\-]?[0-9]+
Which works fine in these 2 scenarios:
myVar = -5
myVar = +5
Regex correctly identifies both -5 and +5 as integers. My problem is if I have a scenario like this:
myVar = 7-5
This, however, gets matched correctly:
myVar = 7*-5
Now what Regex is doing in the 7-5 scenario is its identifying 2 integers, 7 and -5. In reality, what I want is for it to be able to identify an integer (7), a minus sign (-) and then another integer (5). What Regex pattern do I need to do this?
Thanks in advance. This is .NET Regex, by the way.
Regex isn’t the best choice for parsing math expressions. Look at Recursive descent parser or Reverse Polish notation or other more appropriate algorithm.