【发布时间】:2022-01-02 05:35:12
【问题描述】:
我有一些简单的方程式,您不知道结果的位置(在末尾或开头)。想出了这个正则表达式(下面的代码),但它也选择了一个等号,这是预期的。我可以将等号替换为空,但这绝对不是正确的方法。那么如何只选择匹配的一部分呢?
from re import compile,findall
regex = compile(r'(\d+=)?\d+\+\d+(=\d+)?')
print(findall(regex,'1+2=3'))
#Expected: [('', '3')]
#Actual: [('', '=3')]
print(findall(regex,'3=1+2'))
#Expected: [('', '3')]
#Actual: [('', '3=')]
【问题讨论】:
标签: python regex regex-group