Why do I get the output ab for the following regular-expression code with a Relucutant quantifier?
Pattern p = Pattern.compile("abc*?");
Matcher m = p.matcher("abcfoo");
while(m.find())
System.out.println(m.group()); // ab
Similarly, why do I get empty indices for the following code?
Pattern p = Pattern.compile(".*?");
Matcher m = p.matcher("abcfoo");
while(m.find())
System.out.println(m.group());
In addition to Konrad Rudolph's answer:
abc*?
matches "ab" in any case and "c" only if it must. Since nothing follows the *?, the regex engine stops immediately. If you had:
abc*?f
then it would match "abcf" be cause the "c" must match in order to allow the "f" to match, too. The other expression:
.*?
matches nothing because this pattern is 100% optional.
.*?f
would match "abcf" again.
*? matches zero or more matches, but as few as possible (and by the way, that’s usually called “non-greedy”, not “reluctant”). So if zero matches is possible, that’s the optimal match.
What exactly do you want to achieve? Maybe non-greedy matching isn’t what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With