javaregexreluctant-quantifiers

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?


Solution

  • 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: