I'm reading PCRE documentation and I noticed that possessive quantifier +
and once-only subpatterns (?>)
, also known as atomic groups, are somewhat similar by concept. Is there any substantial difference?
(?>)
is actually atomic grouping.
From Atomic Grouping on regular-expressions.info:
An atomic group is a group that, when the regex engine exits from it, automatically throws away all backtracking positions remembered by any tokens inside the group. Atomic groups are non-capturing. The syntax is (?>group).
From Possessive Quantifiers on regular-expressions.info:
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.
From the same page:
Technically, possessive quantifiers are a notational convenience to place an atomic group around a single quantifier. All regex flavors that support possessive quantifiers also support atomic grouping. But not all regex flavors that support atomic grouping support possessive quantifiers. With those flavors, you can achieve the exact same results using an atomic group.
Basically, instead of
X*+
, write(?>X*)
. It is important to notice that both the quantified token X and the quantifier are inside the atomic group. Even if X is a group, you still need to put an extra atomic group around it to achieve the same effect.(?:a|b)*+
is equivalent to(?>(?:a|b)*)
but not to(?>a|b)*
. The latter is a valid regular expression, but it won't have the same effect when used as part of a larger regular expression.
If you have a look at this page of regular-expressions.info, you will notice in the table that "x++
is identical to (?>x+)
".
The only difference noted is:
Possessive quantifiers are a limited yet syntactically cleaner alternative to atomic grouping.
So, it's not as popular as atomic grouping, but it can be considered cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With