how to speed up my regex by avoiding backtracking.

Hello,
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

(?:[+-]*+\(*[+-]*(?:sin|cos|tan|asin|acos|atan|log|exp|sqrt)(?:\([+-]*)+)*(?:\(*[+-]*(?:\d+(?:\.\d+)?|[a-zA-Z])\)*)(?:(?:[/*^%](?:\(*[+-]*)*|[+-]+\(*)(?:(?:sin|cos|tan|asin|acos|atan|log|exp|sqrt)(?:\([+-]*)+)*(?:\d+(?:\.\d+)?|[a-zA-Z])\)*)*

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,
x+9*7/54.54^67/29+(5/x)^(2*sin(x))

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.

Thanks
LVL 1
adam8Asked:
Who is Participating?
I wear a lot of hats...

"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.

gabesoSolution ArchitectCommented:
You shouldn't use a regular expression for programming language syntax: Use the regex for identifying terms and syntactic elements and a parser for the syntax - that will be much faster (and deterministic!).
0
adam8Author Commented:
HI, could you please explain in more detail, what's a parser?
0
adam8Author Commented:
----------You shouldn't use a regular expression for programming language syntax.

The regular expression is for checking if the input is a valid mathematical equation, it has nothing to do with programming language syntax checking...
0
InteractiveMindCommented:
What I think gabeso is suggesting is that you merely use a regex to check that the expression appears valid at first look; so, each term is seperated by a +, -, /, *, et al.
And that the expression doesn't contain _certain_ characters.

This shouldn't require as much as you have above—something like:

   ([a-zA-Z0-9\.]*[+/*-]?)+

And then use a parser to extract and process each term, etc, and to ensure that the actual data given makes sense.


Just my 2 cents.
0

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.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.