Python 非贪婪正则表达式

标签 python regex regex-greedy

我如何制作像 "(.*)" 这样的 python 正则表达式,这样,给定 "a (b) c (d) e" python 匹配 "b" 而不是 "b) c (d"?

我知道我可以使用 "[^)]" 而不是 ".",但我正在寻找一种更通用的解决方案,让我的正则表达式小清洁工。有没有办法告诉python“嘿,尽快匹配这个”?

最佳答案

你寻求全能的*?

来自文档,Greedy versus Non-Greedy

the non-greedy qualifiers *?, +?, ??, or {m,n}? [...] match as little text as possible.

关于Python 非贪婪正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/766372/

相关文章:

python - 这是 Python 中全局变量的合法使用吗?

python - Heroku 工作人员不记录或打印

regex - 区分 Haskell 中的空正则表达式匹配和无匹配

python - (Kivy Python) 实例后重置 ScreenManager 屏幕方向

c# - 高级 regex.replace 处理

javascript - 正则表达式如何替换推特链接

regex - 如何强制正则表达式捕获最大的匹配项?

数字和空格的正则表达式作为千位分隔符

regex - Grep 所有不以#(哈希)或贪心空格和#(哈希)开头的行

python - 更新矩形补丁的位置