懒惰,贪婪和占有量词之间有什么区别? [英] What are the differences between lazy, greedy and possessive quantifiers?
本文介绍了懒惰,贪婪和占有量词之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下量词有何不同 - 在场景、速度等方面.
How do the following quantifiers differ - with respect of scenarios, speed, etc.
?
、??
和?+
都匹配 0 或 1 次.*
,*?和
*+` 都匹配0 次或多次.+
、+?
和++
都匹配1 次或多次.
?
,??
and?+
all match 0 or 1 times.*
,*? and
*+` all match 0 or more times.+
,+?
and++
all match 1 or more times.
?
、*
和+
是贪婪.??
、*?
和+?
是不情愿/懒惰.?+
、*+
和++
是所有物.
?
,*
and+
are greedy.??
,*?
and+?
are reluctant/lazy.?+
,*+
and++
are possessive.
谁能帮助我理解这些术语的含义?为什么同一份工作的每个量词有三种变体?
Can anyone help me to understand what these terms mean? Why are there three variations of each quantifier for the same job?
推荐答案
取字符串
aaaab
并查看以下正则表达式如何匹配它:
and see how the following regexes match it:
Regex Submatches
group 1 group 2 group3
(a?)(a*)(ab) a aa ab
(a??)(a*)(ab) aaa ab
(a?+)(a*)(ab) a aa ab
(a*)(a?)(ab) aaa ab
(a*?)(a?)(ab) aa a ab
(a*+)(a?)(ab) aaaa <Match fails!>
(a+)(a*)(ab) aaa ab
(a+?)(a*)(ab) a aa ab
(a++)(a*)(ab) aaaa <Match fails!>
说明:
a?
尝试匹配一个a
,但如果整个匹配成功有必要,它准备不匹配任何内容.a??
尝试不匹配任何内容,但它准备匹配一个a
,如果这是整个匹配成功所必需的.a?+
尝试匹配一个a
.如果它可以做到这一点,它就不会退缩以匹配任何东西,如果这对于整个匹配成功是必要的.如果它不能匹配a
,那么它很乐意什么都不匹配.a*
尝试匹配尽可能多的a
s,但它准备匹配更少的a
s,即使没有整个比赛取得成功所必需的.a*?
尝试不匹配任何内容,但它准备匹配尽可能多的a
以确保整个匹配成功,但不会更多.a*+
尝试匹配尽可能多的a
.如果它可以做到这一点,它就不会回退以匹配更少的a
,如果这是整个匹配成功所必需的.如果它连一个a
都不能匹配,那么它很乐意什么都不匹配.a+
尝试匹配尽可能多的a
s,但它准备匹配更少的a
s(但至少有一个)如果这对于整场比赛的成功来说是必要的.a+?
尝试只匹配一个a
,但它准备匹配尽可能多的a
整场比赛成功,但不会更多.a++
尝试匹配尽可能多的a
.如果它可以做到这一点,它就不会回退以匹配更少的a
,如果这是整个匹配成功所必需的.如果它甚至不能匹配单个a
,那么正则表达式就会失败.
a?
tries to match onea
, but it's prepared to match nothing if that's necessary for the whole match to succeed.a??
tries to match nothing, but it's prepared to match onea
if that's necessary for the whole match to succeed.a?+
tries to match onea
. If it can do that, it will not back down to match nothing if that were necessary for the overall match to succeed. If it can't match ana
, then it will gladly match nothing, though.a*
tries to match as manya
s as it can, but it's prepared to match fewera
s, even nothing if that's necessary for the whole match to succeed.a*?
tries to match nothing, but it's prepared to match just as manya
s as is absolutely necessary in order for the whole match to succeed, but not more.a*+
tries to match as manya
s as it can. If it can do that, it will not back down to match fewera
s if that were necessary for the overall match to succeed. If it can't match even a singlea
, then it will gladly match nothing, though.a+
tries to match as manya
s as it can, but it's prepared to match fewera
s (but at least one) if that's necessary for the whole match to succeed.a+?
tries to match only onea
, but it's prepared to match just as manya
s as is absolutely necessary in order for the whole match to succeed, but not more.a++
tries to match as manya
s as it can. If it can do that, it will not back down to match fewera
s if that were necessary for the overall match to succeed. If it can't match even a singlea
, then the regex fails.
这篇关于懒惰,贪婪和占有量词之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文