Python 非贪婪正则表达式 [英] Python non-greedy regexes
本文介绍了Python 非贪婪正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何制作像 "(.*)"
这样的 python 正则表达式,这样,给定 "a (b) c (d) e"
python 匹配 "b"
而不是 "b) c (d"
?
How do I make a python regex like "(.*)"
such that, given "a (b) c (d) e"
python matches "b"
instead of "b) c (d"
?
我知道我可以使用 "[^)]"
而不是 "."
,但我正在寻找一个更通用的解决方案,使我的正则表达式保持不变小清洁工.有什么办法可以告诉python嘿,尽快匹配这个"?
I know that I can use "[^)]"
instead of "."
, but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possible"?
推荐答案
你寻求全能的*?
来自文档,贪婪与非贪婪
非贪婪限定符 *?
、+?
、??
或 {m,n}?代码> [...] 匹配为 little文本尽可能.
the non-greedy qualifiers
*?
,+?
,??
, or{m,n}?
[...] match as little text as possible.
这篇关于Python 非贪婪正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文