Regular expression for math expression

I need a regular expression for a mathematical expression that must satisfy the following conditions explained in this SO question that I asked.

He did a great job with this expression.

But now I need to add support for opening and closing parentheses along with the previous conditions. So my regular expression should check the expressions of these patterns

eg. *6+(7-9) or /6.25*(7-9.2) or +6 or *6/(7.5-9)

I tried to make changes to an existing regular expression, but could not succeed. He also accepts these *6+(7-9 patterns *6+(7-9 and *6+7-9) , which are not allowed because a single bracket may be present in the mathematical expression.

Here is the RegExr link. Please help.

0
source share
3 answers

@Bibhu, since math expressions can be nested arbitrarily, an actual parser is needed to test them. Regular expression will not work. Regular expressions are not powerful enough to deal with an arbitrarily deep recursive embedding.

If you limit the maximum level of nesting, you can write a (very large and ugly) regular expression that could test expressions. But in principle, regular expressions are the wrong tool to work with.

If you have a parser generator that you already know how to use, this will be the easiest way to create a parser for mathematical expressions. If you don't, just writing a simple recursive descending parser from top to bottom is still pretty simple.

+4
source

(with funny text it can be more difficult) the best way is a text recognizer, such as ANTLR .

0
source

I wonder if you offer generosity.

Simple regular expression recursion is gosub with pass / fail return, which turns out to be stackable.

The following is a Perl procedure that passes Perl's own parsing algorithm for the simple operators and simple syntax rules that you specified. This is done in one regex because your needs are extremely simple.

It looks weird, but solves the simple balanced text '()'. It seems Dot Net can do this. You just need to make a replacement (i.e. (? & Var)), do
Dot Net's balanced grouping requires ... instant verification.

I am posting this because nesting is not a problem. The problem is that as simple as parsing, the devil seems to be in the details.

 ^ (?: ^ (?&sign)? (?&number) | (?&operator) (?<! ^ (?:\/|\*) ) (?<! ^ [*]{2} ) (?&sign)? (?&number) | (?: (?&operator) (?<! ^ (?:\/|\*) ) (?<! ^ [*]{2} ) (?<! [(] (?:\/|\*) ) (?<! [(] [*]{2} ) (?&sign)? | (?<= [(] ) | ^ (?&sign)? ) (?<term> \( (?: (?> (?&sign)? (?&number) (?: (?&operator) (?&sign)? (?&number) )* ) | (?> (?: (?<= [(] ) | (?&operator) ) (?<! [(] (?:\/|\*) ) (?<! [(] [*]{2} ) (?&sign)? (?&term) ) )* \) (?! [(] ) (?> (?&operator) (?&sign)? (?&number) )* ) )* $ (?(DEFINE) (?<number> \d+(?:\.\d+)? ) (?<sign> [+-] ) (?<operator> (?: [*]{2} | [\/*] | (?<pm>[-+]) (?! \k<pm>) ) ) ) 

Exit

 passed '' passed '(6**-2**3)' passed '6-+2' passed '-(-(8*((2)/3)))' passed '-((8*((2)/3)))' passed '-((8*((2**4)/3)))' passed '-((8*((2**4)/3)))**((-1)*(8*((2**4/99)/3)))' passed '-((8*((2**4)/3)))**((-1)*(8*((2**-4/99)/3)))' passed '-((8*((2**4)/3)))**-((-1)*(8*((2**-4/99)/3)))' passed '((8*((2)/3)))' passed '((8*((2)/3)))' passed '+((8*((-2)/-3)))' passed '8-6*2' passed '-8-6*2' passed '((8*((2-(8*(8+6)/2))/3))-7*2/234)+8/2*1' passed '-(8*(8+6)/2)' passed '(9*9/9)' passed '(9*(9)/9*(9*(9)/9)*1)' passed '(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))' failed '(6--2)' failed '-(/(8*((2)/3)))' failed '-((8*((2(6))/3)))' failed '+((8*((+2)/--3)))' failed '+((8*((+2)/--3+)))' failed '+((8*((*2)/--3)))' failed '+((8*((*2)/-3)))' failed '-((8*((-2)/+-3)))' failed '+8/2(1)' failed '-(8)*(8/(' failed '-(8)*(8/()' failed '*(9*9/9)' failed '*(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))' failed '/(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))' 
0
source

Source: https://habr.com/ru/post/917939/


All Articles