7

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?

Owen
  • 38,836
  • 14
  • 95
  • 125

1 Answers1

7

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This sounds like a great answer but do you know of any documentation that would support it? – Owen Sep 11 '16 at 20:25
  • The question mark in all cases means *reluctant* match, which means "as few of the preceding quantity as possible but still match". In this case, the smallest quantity in the range of `{2,3}` that can still create a match overall is `2`. – Bohemian Sep 11 '16 at 22:14