Can someone explain Possessive Quantifiers to me? (Regular Expressions)

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.

Leave a Comment