clear understanding on possesive quantifiers
This question already has an answer here:
There are three types of quantifiers:
For instance, take this input:
The answer is 42
Now, take this regex:
.*(d+)
The question is, what will be captured by (d+)
according to which version of *
you use in .*
:
*
, what will be captured is 2
; *?
, what will be captured is 42
; *+
, the regex does not match. Why:
d+
to match; it will reluctantly backtrack until d+
is satisfied, and d+
is satisfied with 2
; 4
it lets d+
do its job, therefore 42
is captured; .*+
says "No...", therefore no match. The possessive quantifier implies that no backtracking is done.
What happens when you try to match mdfoo
against .*+foo
:
.*+
) matches the whole string mdfoo
foo
) is not found after the first match These possessive quantifiers are quite clearly explained here.
链接地址: http://www.djcxy.com/p/76906.html上一篇: 所有格量化符正则表达式的实际使用
下一篇: 清晰的认识量化值