Search code examples
regexregex-greedyreluctant-quantifiers

Difference between exact greedy/reluctant X{n}?


In the documentation for the Java Pattern class, I see that the exact quantifier X{n} has both greedy and reluctant forms:

Greedy quantifiers

  • X{n} X, exactly n times
  • ...

Reluctant quantifiers

  • X{n}? X, exactly n times
  • ...

The documentation gives general examples of the difference between greedy and reluctant behavior, but doesn't give any examples for the exact quantifiers.

At first I thought, "Well, maybe the difference is that X itself could match in difference ways." But then X can have its own greedy/reluctant specifiers inside it, and sure enough I tested it and that's not a difference (greedy vs reluctant).

Given that, in either case, it is going to match exactly n times, is there any difference between the behavior of the two?


Solution

  • Reluctant vs greedy only makes sense when a variable length match is possible; a reluctant quantifier will match the minimum possible, and greedy the maximum.

    To distinguish behaviour of a limited quantity, it must have a range, ie the quantity must have a different minimum and maximum. To illustrate:

    Given the input 1234567, the groups captured are:

    (\d{2,3})(\d+)  -> (123)(4567)
    (\d{2,3}?)(\d+) -> (12)(34567)
    

    When there is just a fixed quantity, eg \d{2} there is no difference in behaviour by adding a ?.