Python正则表达式中的非贪婪 [英] Non-greedy in Python Regex

查看:230
本文介绍了Python正则表达式中的非贪婪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解python中的非贪婪正则表达式,但我不明白为什么以下示例具有此结果:

I try to understand the non-greedy regex in python, but I don't understand why the following examples have this results:

print(re.search('a??b','aaab').group())
ab
print(re.search('a*?b','aaab').group())
aaab

我认为第一个为'b',第二个为'ab'. 谁能解释一下?

I thought it would be 'b' for the first and 'ab' for the second. Can anyone explain that?

推荐答案

之所以会发生这种情况,是因为您要求匹配的是之后.如果您尝试遵循a??b的匹配从左到右的方式,您将看到类似这样的内容:

This happens because the matches you are asking match afterwards. If you try to follow how the matching for a??b happens from left to right you'll see something like this:

  • 尝试0 ab vs aaab:无匹配项(b != a)
  • 尝试1个ab vs aaab:不匹配(ab != aa)
  • 尝试0 ab vs aab:无匹配项(b != a)(匹配位置向右移动一位)
  • 尝试1个ab vs aab:不匹配(ab != aa)
  • 尝试0 ab vs ab:无匹配项(b != a)(匹配位置向右移动一位)
  • 尝试1个abab:匹配(ab == ab)
  • Try 0 a plus b vs aaab: no match (b != a)
  • Try 1 a plus b vs aaab : no match (ab != aa)
  • Try 0 a plus b vs aab: no match (b != a) (match position moved to the right by one)
  • Try 1 a plus b vs aab : no match (ab != aa)
  • Try 0 a plus b vs ab: no match (b != a) (match position moved to the right by one)
  • Try 1 a plus b vs ab : match (ab == ab)

*?类似.

事实是search函数返回最左匹配项.使用??*?只会更改行为,以偏爱最左边最短的匹配,但是 not 不会返回较短的匹配,该匹配从已找到的匹配的右边开始

The fact is that the search function returns the leftmost match. Using ?? and *? changes only the behaviour to prefer the shortest leftmost match but it will not return a shorter match that starts at the right of an already found match.

还请注意,re模块不会返回重叠的匹配项,因此即使使用findallfinditer,您也将找不到要查找的两个匹配项.

Also note that the re module doesn't return overlapping matches, so even using findall or finditer you will not be able to find the two matches you are looking for.

这篇关于Python正则表达式中的非贪婪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆