Difference between exact greedy/reluctant X{n}?
Asked Answered
D

1

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?

Depth answered 20/3, 2016 at 4:41 Comment(1)
X{2} = X{2}?. The first is shorter - that is all the difference.Nkrumah
F
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 ?.

Freckly answered 20/3, 2016 at 5:26 Comment(2)
This sounds like a great answer but do you know of any documentation that would support it?Depth
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.Freckly

© 2022 - 2024 — McMap. All rights reserved.