How to check with regularies, the correctness of the input of a mathematical expression? Everything is not as I can not think of, a mathematical expression may include the following set of characters = + - * / () [az]. That is, this is a list of valid expressions:

x=x+1 (x+1)=sav*10/(2+1) a+b=c (r+w)/2 a=3+5/(3*10(-10/4)) +a--3+50 

And this is a list of incorrect:

 =2+r ((d+f) *e=f g=/3 3=4//5 x=5/ df= 

Those. in theory, you need to recursively open parentheses, and check the sequence of certain characters.

  • Use the method from www.stackoverflow.com/questions/448942/… . Clearly state the criteria for the correct mathematical expression and express these criteria in the language of regular expressions. - ReinRaus
  • one
    The brackets are generally not counted by regexps, it seems. - bipll
  • one
    @bipll I found examples of how the recursion of parentheses was checked, it seemed to work correctly) - Swagga
  • AND? Where is regexp, and where is recursion? Or "with the help of regulars" reads like "somehow, but so that the regulars were there too"? : - \ - bipll
  • This problem is solved by regular expressions, including the balance of parentheses can be respected. - ReinRaus

1 answer 1

Okay, the question has been decided, I will leave the regular season for posterity:

 ( ( ( (?<=^|\(|\=|\+|\-|\*|\/) ([a-zA-Z][a-zA-Z\d]*) (?=$|\)|\=|\+|\-|\*|\/) ) | ( (?<=^|\(|\=|\+|\-|\*|\/) (\d+) (?=$|\)|\+|\-|\*|\/) ) | ( (?<=[a-zA-Z\d]) (\=) (?=[a-zA-Z\d]|\(|\+|\-) ) | ( (?<=^|[a-zA-Z\d]|\=|\(|\)) (\+|\-) (?=[a-zA-Z\d]|\() ) | ( (?<=[a-zA-Z\d]|\)) (\*|\/) (?=[a-zA-Z\d]|\() ) | (?<level> (?<=^|\=|\(|\+|\-|\*|\/) (\() (?=[a-zA-Z\d]|\(|\+|\-) ) | (?<-level> (?<=[a-zA-Z\d]|\)) (\)) (?=$|\)|\+|\-|\*|\/) ) )+ (?(level)(?!)) )$ 
  • Can I use a working example on a thread of an online service like regex101.com ? - Lexx918