how to speed up my regex by avoiding backtracking.

Posted on 2006-04-07
Last Modified: 2010-04-17
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.

Question by:adam8
    LVL 9

    Expert Comment

    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!).
    LVL 1

    Author Comment

    HI, could you please explain in more detail, what's a parser?
    LVL 1

    Author Comment

    ----------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...
    LVL 25

    Accepted Solution

    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:


    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.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This is an explanation of a simple data model to help parse a JSON feed
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now