Python正则表达式中的非贪婪 [英] Non-greedy in Python Regex
问题描述
我尝试了解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
a
加b
vsaaab
:无匹配项(b != a
) - 尝试1个
a
加b
vsaaab
:不匹配(ab != aa
) - 尝试0
a
加b
vsaab
:无匹配项(b != a
)(匹配位置向右移动一位) - 尝试1个
a
加b
vsaab
:不匹配(ab != aa
) - 尝试0
a
加b
vsab
:无匹配项(b != a
)(匹配位置向右移动一位) - 尝试1个
a
加b
与ab
:匹配(ab == ab
)
- Try 0
a
plusb
vsaaab
: no match (b != a
) - Try 1
a
plusb
vsaaab
: no match (ab != aa
) - Try 0
a
plusb
vsaab
: no match (b != a
) (match position moved to the right by one) - Try 1
a
plusb
vsaab
: no match (ab != aa
) - Try 0
a
plusb
vsab
: no match (b != a
) (match position moved to the right by one) - Try 1
a
plusb
vsab
: 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
模块不会返回重叠的匹配项,因此即使使用findall
或finditer
,您也将找不到要查找的两个匹配项.
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屋!