in reply to When would I want to use possessive quantifiers?

petdance,
I freely admit I have never used possessive quantifiers nor can I think of an example where I might want to. I was skeptical by your statement that perlre didn't have an example so I looked: For instance, the typical "match a double-quoted string" problem can be most efficiently performed when written as: /"(?:[^"\\]++|\\.)*+"/...

It goes on to say that it is just syntactic sugar and could be re-written as /"(?>(?:(?>[^"\\]+)|\\.)*)"/

Cheers - L~R

Replies are listed 'Best First'.
Re^2: When would I want to use possessive quantifiers?
by JavaFan (Canon) on Jul 07, 2010 at 17:36 UTC
    It goes on to say that it is just syntactic sugar
    They are indeed syntactic sugar. But I prefer to write: /a++/ over /(?>a+)/. 2 special characters instead of 5.

    As for the OPs question, the main use is performance - specially in cases of not matching. Using possessive quantifiers changes the meaning - unlike non-greedy matches, possessive quantifiers can change the matching/non-matching behaviour. That is, given a pattern, making quantifiers non-greedy (or making non-greedy quantifiers greedy) will not change the set of strings the pattern matches, but making quantifiers possessive can change the set (but only by reducing the set). But I would recommend using possessive quantifiers for that effect - that's just too subtle.