0

What would be a good regex for RECOGNIZING math expressions ?

I'm not looking of parsing it, but just recognizing it is math expression. The critical feature is that it does not recognize non-math-exp as math-exp

including : ops, parentheses, ...

excluding : functions f.e exp,sqrt

probably starting point will be :

(^|\s+)[+\-*/()0-9 ]+(\s+|$)

example

sten
  • 7,028
  • 9
  • 41
  • 63
  • Math expressions can be quite varied and complex. Do you really mean literally any math expression, or just a particular class of them? – lurker Nov 03 '19 at 19:54
  • i mean basic math exp... excluding functions – sten Nov 03 '19 at 19:55
  • Still need more info. Roots? Exponents? Parentheses? – lurker Nov 03 '19 at 19:55
  • If you allow parentheses, no regex is going to do the job for you. https://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns – Mark Dickinson Nov 03 '19 at 20:00
  • it does not need to parse them just recognize them i.e. no need to be recursive .. detecting single parentheses will probably be enough – sten Nov 03 '19 at 20:03
  • 1
    If you want to solve the problem in general, then parsing should be the way to go, regex alone will not help here. You need some simple grammar with nested regexes (to support the parentheses). If parsing works, then it's a math expression. Check for instance the calculator example from lark: https://github.com/lark-parser/lark/blob/master/examples/calc.py. – akc Nov 03 '19 at 20:22
  • Parsing actually is a recognition process. What you really mean is that you don't need to tokenize as an output of the parsing. You just want to know that the parsing succeeded without errors. The front-end process of parsing is what you want. – lurker Nov 04 '19 at 13:07

0 Answers0