请教一个正则问题,懒惰匹配如何匹配多个内容

2018-11-22 15:37:18 +08:00
 wonder1z
str = 'asd(hello)asd(world)'
s = re.search(r'(?<=\().*?(?=\))',str1)
print(s)
<re.Match object; span=(4, 9), match='hello'>

只能匹配出第一个 hello,如何匹配出所有()里的内容呢

1769 次点击
所在节点    Python
6 条回复
masker
2018-11-22 15:40:44 +08:00
\w+\((.*?)\){1,}
sess222
2018-11-22 15:41:55 +08:00
懒惰匹配是虾米?
re.findall('\((.*?)\)', str1)
wonder1z
2018-11-22 15:47:25 +08:00
@masker 老铁 你这个不对啊
wonder1z
2018-11-22 15:48:57 +08:00
@sess222 这个也不对的,懒惰就是尽可能少的重复
wonder1z
2018-11-22 15:51:55 +08:00
@sess222 sorry 搞错了,你这个可以的
Atukey
2018-11-22 16:14:11 +08:00
(?<=\()\w*(?=\))

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/510433

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX