regex - 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())
解决方案
在你的表达"^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'}
推荐阅读
- wordpress - 在 WordPress 中的所有联系表单上在哪里设置电子邮件 ID?
- kotlin - 启动我的应用程序时出现 kotlin.KotlinNullPointerException
- reactjs - 使用 React.js 循环音频列表
- postgresql - Postgres 触发函数动态列 Concat TG_TABLE_NAME
- apache-spark - 基于 spark 中列值的动态 regexp_extract
- canvas - 如何设置舞台背景图片
- java - java.lang.IllegalStateException:不是标准目录之一
- java - 将“2020-10-31T00:00:00Z”字符串日期转换为长
- perforce - Perforce P4Api.net add file get this Can't add filenames with wildcards [@#%*] in them
- java - 如果本地方法的包或类或方法名称被混淆,yGuard 现在会发出警告