Repetition is specified by quantifiers, which can follow any of
the following items:
a literal data character
the dot metacharacter
the \C escape sequence
the \X escape sequence
the \R escape sequence
an escape such as \d or \pL that matches a single character
a character class
a back reference (see next section)
a parenthesized subpattern (including assertions)
a subroutine call to a subpattern (recursive or otherwise)
The general repetition quantifier specifies a minimum and maximum
number of permitted matches, by giving the two numbers in curly
brackets (braces), separated by a comma. The numbers must be less
than 65536, and the first must be less than or equal to the
second. For example:
z{2,4}
matches "zz", "zzz", or "zzzz". A closing brace on its own is not
a special character. If the second number is omitted, but the
comma is present, there is no upper limit; if the second number
and the comma are both omitted, the quantifier specifies an exact
number of required matches. Thus
[aeiou]{3,}
matches at least 3 successive vowels, but may match many more,
while
\d{8}
matches exactly 8 digits. An opening curly bracket that appears
in a position where a quantifier is not allowed, or one that does
not match the syntax of a quantifier, is taken as a literal
character. For example, {,6} is not a quantifier, but a literal
string of four characters.
In UTF modes, quantifiers apply to characters rather than to
individual data units. Thus, for example, \x{100}{2} matches two
characters, each of which is represented by a two-byte sequence
in a UTF-8 string. Similarly, \X{3} matches three Unicode
extended grapheme clusters, each of which may be several data
units long (and they may be of different lengths).
The quantifier {0} is permitted, causing the expression to behave
as if the previous item and the quantifier were not present. This
may be useful for subpatterns that are referenced as subroutines
from elsewhere in the pattern (but see also the section entitled
"Defining subpatterns for use by reference only" below). Items
other than subpatterns that have a {0} quantifier are omitted
from the compiled pattern.
For convenience, the three most common quantifiers have single-
character abbreviations:
* is equivalent to {0,}
+ is equivalent to {1,}
? is equivalent to {0,1}
It is possible to construct infinite loops by following a
subpattern that can match no characters with a quantifier that
has no upper limit, for example:
(a?)*
Earlier versions of Perl and PCRE used to give an error at
compile time for such patterns. However, because there are cases
where this can be useful, such patterns are now accepted, but if
any repetition of the subpattern does in fact match no
characters, the loop is forcibly broken.
By default, the quantifiers are "greedy", that is, they match as
much as possible (up to the maximum number of permitted times),
without causing the rest of the pattern to fail. The classic
example of where this gives problems is in trying to match
comments in C programs. These appear between /* and */ and within
the comment, individual * and / characters may appear. An attempt
to match C comments by applying the pattern
/\*.*\*/
to the string
/* first comment */ not comment /* second comment */
fails, because it matches the entire string owing to the
greediness of the .* item.
However, if a quantifier is followed by a question mark, it
ceases to be greedy, and instead matches the minimum number of
times possible, so the pattern
/\*.*?\*/
does the right thing with the C comments. The meaning of the
various quantifiers is not otherwise changed, just the preferred
number of matches. Do not confuse this use of question mark with
its use as a quantifier in its own right. Because it has two
uses, it can sometimes appear doubled, as in
\d??\d
which matches one digit by preference, but can match two if that
is the only way the rest of the pattern matches.
If the PCRE_UNGREEDY option is set (an option that is not
available in Perl), the quantifiers are not greedy by default,
but individual ones can be made greedy by following them with a
question mark. In other words, it inverts the default behaviour.
When a parenthesized subpattern is quantified with a minimum
repeat count that is greater than 1 or with a limited maximum,
more memory is required for the compiled pattern, in proportion
to the size of the minimum or maximum.
If a pattern starts with .* or .{0,} and the PCRE_DOTALL option
(equivalent to Perl's /s) is set, thus allowing the dot to match
newlines, the pattern is implicitly anchored, because whatever
follows will be tried against every character position in the
subject string, so there is no point in retrying the overall
match at any position after the first. PCRE normally treats such
a pattern as though it were preceded by \A.
In cases where it is known that the subject string contains no
newlines, it is worth setting PCRE_DOTALL in order to obtain this
optimization, or alternatively using ^ to indicate anchoring
explicitly.
However, there are some cases where the optimization cannot be
used. When .* is inside capturing parentheses that are the
subject of a back reference elsewhere in the pattern, a match at
the start may fail where a later one succeeds. Consider, for
example:
(.*)abc\1
If the subject is "xyz123abc123" the match point is the fourth
character. For this reason, such a pattern is not implicitly
anchored.
Another case where implicit anchoring is not applied is when the
leading .* is inside an atomic group. Once again, a match at the
start may fail where a later one succeeds. Consider this pattern:
(?>.*?a)b
It matches "ab" in the subject "aab". The use of the backtracking
control verbs (*PRUNE) and (*SKIP) also disable this
optimization.
When a capturing subpattern is repeated, the value captured is
the substring that matched the final iteration. For example,
after
(tweedle[dume]{3}\s*)+
has matched "tweedledum tweedledee" the value of the captured
substring is "tweedledee". However, if there are nested capturing
subpatterns, the corresponding captured values may have been set
in previous iterations. For example, after
/(a|(b))+/
matches "aba" the value of the second captured substring is "b".