17

I am reading the PCRE doc, and it refers to possessive quantifiers, but does not explicitly or specifically define them. I know what a greedy quantifier is, and I know what a lazy quantifer is. But possessive?

The PCRE man page seems to be cheating when it uses the term without defining it. The man page specifically states that the term possessive quantifiers was first defined in Friedl's book. Well, that's great, but I don't have Friedl's book, and in reading the man page, between the lines, I cannot figure out what distinguishes possessive quantifiers from greedy ones.

  • ? = zero or one, greedy
  • ?? = zero or one, lazy
  • ?+ = zero or one, possessive
  • '+' = one or more, greedy
  • +? = one or more, lazy
  • ++ = one or more, possessive
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • This question has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Quantifiers > More on the differences..." – aliteralmind Apr 10 '14 at 00:11

1 Answers1

14

Perhaps the best place to start is Regex Tutorial - Possessive Quantifiers:

When discussing the repetition operators or quantifiers, I explained the difference between greedy and lazy repetition. Greediness and laziness determine the order in which the regex engine tries the possible permutations of the regex pattern. A greedy quantifier will first try to repeat the token as many times as possible, and gradually give up matches as the engine backtracks to find an overall match. A lazy quantifier will first repeat the token as few times as required, and gradually expand the match as the engine backtracks through the regex to find an overall match.


Possessive quantifiers are a way to prevent the regex engine from trying all permutations. This is primarily useful for performance reasons. You can also use possessive quantifiers to eliminate certain matches.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 10
    Ahh, I see! After reading that tutorial, it seems to me lazy is not the opposite of greedy. Lazy should be called "generous" maybe, and the possessive form should actually be called lazy, because possessive quantifiers refuse to backtrack. They won't do any extra work, which is just darned lazy. – Cheeso Jul 13 '09 at 02:20
  • 6
    possessive quantifiers refuse to backtrack - this is the best explanation I read about it. thanks for summarizing it :) – vondip Jul 03 '13 at 11:00