Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in RegExp Reluctant quantifier? Java

Tags:

java

regex

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());
like image 684
Dusk Avatar asked May 23 '26 11:05

Dusk


2 Answers

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.

like image 122
Tomalak Avatar answered May 26 '26 02:05

Tomalak


*? 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.

like image 40
Konrad Rudolph Avatar answered May 26 '26 03:05

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!