How do the following quantifiers differ - with respect of scenarios, speed, etc.
?
,??
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?
Take the string
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!>
Explanation:
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.
来源:https://stackoverflow.com/questions/14345941/what-are-the-differences-between-lazy-greedy-and-possessive-quantifiers