tags:

views:

464

answers:

1

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
+5  A: 

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.

Andrew Hare
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