Regular expression for simple math expressions

后端 未结 4 1422
不思量自难忘°
不思量自难忘° 2021-01-21 23:40

As an exercise I was trying to come up with a regex to evaluate simple algebra like

q = \'23 * 345 - 123+65\'

From here I want to get \'23\', \

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 00:19

    I can only speak of regex in general, as I don't know python, but your problem is that in

    (\d+\s*[\*/+-]\s*)+(\d+\s*)
    

    This portion

    (\d+\s*[\*/+-]\s*)+
    

    Is being repeated and when it's completely done evaluating, you only see the final one.

提交回复
热议问题