Use of reluctant quantifier in regex
I have not used the reluctant quantifier in regex so far and I have a look at some questions of the site to apprehend its function and its use. Although that the similar question is quite descriptive, I still cannot understand how it is associated with the example in the java tutorial. I have understood that the reluctant quantifier matches as few as possible cases. So, how does it explain that a regex
".*?foo"
on a String
"xfooxxxxxxfoo"
returns two matches: "xfoo"
and "xxxxxxfoo"
?
Moreover, since the explanation of the possessive quantifier is also insufficient, could somebody explain why the regex
".*+foo"
on the same String does not match anything?
The description on the tutorial page in the section "Differences Among Greedy, Reluctant, and Possessive Quantifiers" seems very clear to me. What part of it don't you understand?
To answer your specific questions:
For the pattern ".*?foo"
, the reluctant quantifier ( .*?
) first grabs nothing; the remainder of the pattern ( foo
) fails to match. Matching backs up to the reluctant quantifier which then grabs one character ( x
); the rest of the pattern then matches. The whole pattern then starts again (after the first "foo"
) and does not match until it has grabbed all the x
characters before the second foo
, at which point it has the second match.
For the pattern, ( ".*+foo"
), the possessive quantifier ( .*+
) grabs all the possible input (since "."
matches anything) and there isn't anything left over to match the "foo"
part of the pattern. Because ".*+"
is possessive, it won't give anything back; thus, unlike the greedy quantifier, when matching backs up to the possessive quantifier, it simply fails.
上一篇: 正则表达式处理零
下一篇: 在regex中使用不情愿的量词