首页 > 解决方案 > Python正则表达式非贪婪

问题描述

正如我们所知的蟒蛇'?量词被认为是一个惰性量词,然后应该给我们最短的匹配,但在我的例子中,我得到的是第二个匹配(second_occurrence),而不是第一个匹配(first_occurrence)

content = "this is how we want that first_occurrence over there but that second_occurrence it is 
always wrong when "
match = re.search(r"^this .* that (?P<occurrence>.*?) ", content)
print(match.groupdict())

标签: regexpython-3.x

解决方案


在你的表达"^this .* that (?P<occurrence>.*?) "中,第一个.*是贪婪的,所以它会一直匹配到最后一个that

将您的示例更改为:

import re

content = "this is how we want that first_occurrence over there but that second_occurrence it is always wrong when "
match = re.search(r"^this .*? that (?P<occurrence>.*?) ", content)
print(match.groupdict())

这打印:

{'occurrence': 'first_occurrence'}

推荐阅读