Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 533
Owen Avatar asked Mar 20 '16 04:03

Owen


People also ask

What are reluctant quantifiers?

A reluctant quantifier indicates the search engine to start with the shortest possible piece of the string. Once match found, the engine continue; otherwise it adds one character to the section of the string being checked and search that, and so on.

What is the difference between lazy matching and greedy matching in regular expressions?

'Greedy' means match longest possible string. 'Lazy' means match shortest possible string.

What is greedy and non-greedy in regex?

So the difference between the greedy and the non-greedy match is the following: The greedy match will try to match as many repetitions of the quantified pattern as possible. The non-greedy match will try to match as few repetitions of the quantified pattern as possible.

What is greedy and non-greedy matching in Python?

It means the greedy quantifiers will match their preceding elements as much as possible to return to the biggest match possible. On the other hand, the non-greedy quantifiers will match as little as possible to return the smallest match possible. non-greedy quantifiers are the opposite of greedy ones.


1 Answers

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

like image 77
Bohemian Avatar answered Sep 28 '22 14:09

Bohemian