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

Solved

Posted on 2006-04-07

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

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|co

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

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

4 Comments

The regular expression is for checking if the input is a valid mathematical equation, it has nothing to do with programming language syntax checking...

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.

Title | # Comments | Views | Activity |
---|---|---|---|

java continue statement | 10 | 53 | |

countAbc challenge | 9 | 35 | |

nestparen challenge | 4 | 37 | |

Regular Expression to find values of a certain lenght | 1 | 21 |

This is an explanation of a simple data model to help parse a JSON feed

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

Connect with top rated Experts

**22** Experts available now in Live!