how to speed up my regex by avoiding backtracking.
Posted on 2006-04-07
Here is a regex, it basically checks for a valid equation, which then goes into my RPN converter. I have very little experience with regular expressions so I'm fairly confident the regex is very inefficient, and I want some suggestions to improve the effeciency.
There is no matching bracket tests in the regex, this is done using stacks prior to the regex test.
Here's the regular expression
As you can see it's very long, but seems to do what I need it to. The equation can have one variable, accepts, sin, cos, tan, ......., allows brackets, etc. Probably fairly straightforward if you trace it out.
Anyway, I have noticed my regex seems quite inefficient, with a simple expression such as this,
regex buddy shows 171 steps in order to find the whole string is a match.
The main problem is when I put very long equations into the regex, it actually goes into a loop sometimes, and I have to end task to the program.