问题描述
我正在 python3 中使用正则表达式并遇到了一些问题,正则表达式没有按我预期的方式工作(很可能是由于我的错误)。这是我所拥有的:
import re
pattern = re.compile('^cancel ([a-z]+)? timer [a-z\s]+$')
# there is a match for this
matches = pattern.match('cancel my timer for something')
# there are NO matches for this
matches = pattern.match('cancel timer for something')
为了简化(和调试)这个,我做了:
import re
pattern = re.compile('^cancel (my)? timer [a-z\s]+$')
# there is a match for this
matches = pattern.match('cancel my timer for something')
# there are NO matches for this
matches = pattern.match('cancel timer for something')
不匹配的第二个例子没有意义。 my
不应该是可选的,因此无法匹配吗?理论上,两种情况下的两个字符串都应该匹配。对此有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)