Download Dr.Web Enterprise Suite

Transcript
Appendices
After matching all 6 digits and then failing to match "foo", the normal
action of the matcher is to try again with only 5 digits matching the
\d+ item, and then with 4, and so on, before ultimately failing.
"Atomic grouping" (a term taken from Jeffrey Friedl's book) provides
the means for specifying that once a subpattern has matched, it is not
to be re-evaluated in this way.
If we use atomic grouping for the previous example, the matcher
would give up immediately on failing to match "foo" the first time. The
notation is a kind of special parenthesis, starting with (?> as in this
example:
(?>\d+)foo
This kind of parenthesis "locks up" the part of the pattern it contains
once it has matched, and a failure further into the pattern is prevented
from backtracking into it. Backtracking past it to previous items,
however, works as normal.
An alternative description is that a subpattern of this type matches the
string of characters that an identical standalone pattern would match,
if anchored at the current point in the subject string.
Atomic grouping subpatterns are not capturing subpatterns. Simple
cases such as the above example can be thought of as a maximizing
repeat that must swallow everything it can. So, while both \d+ and
\d+? are prepared to adjust the number of digits they match in order
to make the rest of the pattern match, (?>\d+) can only match an
entire sequence of digits.
Atomic groups in general can of course contain arbitrarily complicated
subpatterns, and can be nested. However, when the subpattern for an
atomic group is just a single repeated item, as in the example above, a
simpler notation, called a "possessive quantifier" can be used. This
consists of an additional + character following a quantifier. Using this
notation, the previous example can be rewritten as
\d++foo
Possessive quantifiers are always greedy; the setting of the
PCRE_UNGREEDY option is ignored. They are a convenient notation
for the simpler forms of atomic group. However, there is no difference
Administrator Manual
321