phinz
phinz

Reputation: 1459

Why is there a nongreedy version of {m} in PHP regex?

On page 107 of the book Programming PHP by Kevin Tatroe and Peter MacIntyre, 3rd edition, it is stated that in PHP regular expressions, there is a nongreedy version {m}? of the quantifier {m}. The latter quantifier means "exactly m times" and I am wondering how this can be greedy or nongreedy?

Upvotes: 3

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627101

A limiting quantifier can be both greedy and lazy (reluctant). The difference is only visible when you use both minimum and maximum values, e.g. {1,5} and {1,5}?.

When you use just the minimum threshold value, greedy and lazy versions yield the same behavior.

It seems that the {m}? is just there to keep consistency ({n} and {n,m} are both forms of the same limiting quantifier that can be lazy or greedy), but is really redundant.

Upvotes: 2

Related Questions