@dr.pogodin/react-native-static-server
Version:
Embedded HTTP server for React Native
1,136 lines (1,127 loc) • 186 kB
HTML
<html>
<head>
<title>pcre2pattern specification</title>
</head>
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
<h1>pcre2pattern man page</h1>
<p>
Return to the <a href="index.html">PCRE2 index page</a>.
</p>
<p>
This page is part of the PCRE2 HTML documentation. It was generated
automatically from the original man page. If there is any nonsense in it,
please consult the man page, in case the conversion went wrong.
<br>
<ul>
<li><a name="TOC1" href="#SEC1">PCRE2 REGULAR EXPRESSION DETAILS</a>
<li><a name="TOC2" href="#SEC2">EBCDIC CHARACTER CODES</a>
<li><a name="TOC3" href="#SEC3">SPECIAL START-OF-PATTERN ITEMS</a>
<li><a name="TOC4" href="#SEC4">CHARACTERS AND METACHARACTERS</a>
<li><a name="TOC5" href="#SEC5">BACKSLASH</a>
<li><a name="TOC6" href="#SEC6">CIRCUMFLEX AND DOLLAR</a>
<li><a name="TOC7" href="#SEC7">FULL STOP (PERIOD, DOT) AND \N</a>
<li><a name="TOC8" href="#SEC8">MATCHING A SINGLE CODE UNIT</a>
<li><a name="TOC9" href="#SEC9">SQUARE BRACKETS AND CHARACTER CLASSES</a>
<li><a name="TOC10" href="#SEC10">PERL EXTENDED CHARACTER CLASSES</a>
<li><a name="TOC11" href="#SEC11">UTS#18 EXTENDED CHARACTER CLASSES</a>
<li><a name="TOC12" href="#SEC12">POSIX CHARACTER CLASSES</a>
<li><a name="TOC13" href="#SEC13">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a>
<li><a name="TOC14" href="#SEC14">VERTICAL BAR</a>
<li><a name="TOC15" href="#SEC15">INTERNAL OPTION SETTING</a>
<li><a name="TOC16" href="#SEC16">GROUPS</a>
<li><a name="TOC17" href="#SEC17">DUPLICATE GROUP NUMBERS</a>
<li><a name="TOC18" href="#SEC18">NAMED CAPTURE GROUPS</a>
<li><a name="TOC19" href="#SEC19">REPETITION</a>
<li><a name="TOC20" href="#SEC20">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
<li><a name="TOC21" href="#SEC21">BACKREFERENCES</a>
<li><a name="TOC22" href="#SEC22">ASSERTIONS</a>
<li><a name="TOC23" href="#SEC23">NON-ATOMIC ASSERTIONS</a>
<li><a name="TOC24" href="#SEC24">SCAN SUBSTRING ASSERTIONS</a>
<li><a name="TOC25" href="#SEC25">SCRIPT RUNS</a>
<li><a name="TOC26" href="#SEC26">CONDITIONAL GROUPS</a>
<li><a name="TOC27" href="#SEC27">COMMENTS</a>
<li><a name="TOC28" href="#SEC28">RECURSIVE PATTERNS</a>
<li><a name="TOC29" href="#SEC29">GROUPS AS SUBROUTINES</a>
<li><a name="TOC30" href="#SEC30">ONIGURUMA SUBROUTINE SYNTAX</a>
<li><a name="TOC31" href="#SEC31">CALLOUTS</a>
<li><a name="TOC32" href="#SEC32">BACKTRACKING CONTROL</a>
<li><a name="TOC33" href="#SEC33">EBCDIC ENVIRONMENTS</a>
<li><a name="TOC34" href="#SEC34">SEE ALSO</a>
<li><a name="TOC35" href="#SEC35">AUTHOR</a>
<li><a name="TOC36" href="#SEC36">REVISION</a>
</ul>
<br><a name="SEC1" href="#TOC1">PCRE2 REGULAR EXPRESSION DETAILS</a><br>
<P>
The syntax and semantics of the regular expressions that are supported by PCRE2
are described in detail below. There is a quick-reference syntax summary in the
<a href="pcre2syntax.html"><b>pcre2syntax</b></a>
page. PCRE2 tries to match Perl syntax and semantics as closely as it can.
PCRE2 also supports some alternative regular expression syntax that does not
conflict with the Perl syntax in order to provide some compatibility with
regular expressions in Python, .NET, and Oniguruma. There are in addition some
options that enable alternative syntax and semantics that are not the same as
in Perl.
</P>
<P>
Perl's regular expressions are described in its own documentation, and regular
expressions in general are covered in a number of books, some of which have
copious examples. Jeffrey Friedl's "Mastering Regular Expressions", published
by O'Reilly, covers regular expressions in great detail. This description of
PCRE2's regular expressions is intended as reference material.
</P>
<P>
This document discusses the regular expression patterns that are supported by
PCRE2 when its main matching function, <b>pcre2_match()</b>, is used. PCRE2 also
has an alternative matching function, <b>pcre2_dfa_match()</b>, which matches
using a different algorithm that is not Perl-compatible. Some of the features
discussed below are not available when DFA matching is used. The advantages and
disadvantages of the alternative function, and how it differs from the normal
function, are discussed in the
<a href="pcre2matching.html"><b>pcre2matching</b></a>
page.
</P>
<br><a name="SEC2" href="#TOC1">EBCDIC CHARACTER CODES</a><br>
<P>
Most computers use ASCII or Unicode for encoding characters, and PCRE2 assumes
this by default. However, it can be compiled to run in an environment that uses
the EBCDIC code, which is the case for some IBM mainframe operating systems. In
the sections below, character code values are ASCII or Unicode; in an EBCDIC
environment these characters may have different code values, and there are no
code points greater than 255. Differences in behaviour when PCRE2 is running in
an EBCDIC environment are described in the section
<a href="#ebcdicenvironments">"EBCDIC environments"</a>
below, which you can ignore unless you really are in an EBCDIC environment.
</P>
<br><a name="SEC3" href="#TOC1">SPECIAL START-OF-PATTERN ITEMS</a><br>
<P>
A number of options that can be passed to <b>pcre2_compile()</b> can also be set
by special items at the start of a pattern. These are not Perl-compatible, but
are provided to make these options accessible to pattern writers who are not
able to change the program that processes the pattern. Any number of these
items may appear, but they must all be together right at the start of the
pattern string, and the letters must be in upper case.
</P>
<br><b>
UTF support
</b><br>
<P>
In the 8-bit and 16-bit PCRE2 libraries, characters may be coded either as
single code units, or as multiple UTF-8 or UTF-16 code units. UTF-32 can be
specified for the 32-bit library, in which case it constrains the character
values to valid Unicode code points. To process UTF strings, PCRE2 must be
built to include Unicode support (which is the default). When using UTF strings
you must either call the compiling function with one or both of the PCRE2_UTF
or PCRE2_MATCH_INVALID_UTF options, or the pattern must start with the special
sequence (*UTF), which is equivalent to setting the relevant PCRE2_UTF. How
setting a UTF mode affects pattern matching is mentioned in several places
below. There is also a summary of features in the
<a href="pcre2unicode.html"><b>pcre2unicode</b></a>
page.
</P>
<P>
Some applications that allow their users to supply patterns may wish to
restrict them to non-UTF data for security reasons. If the PCRE2_NEVER_UTF
option is passed to <b>pcre2_compile()</b>, (*UTF) is not allowed, and its
appearance in a pattern causes an error.
</P>
<br><b>
Unicode property support
</b><br>
<P>
Another special sequence that may appear at the start of a pattern is (*UCP).
This has the same effect as setting the PCRE2_UCP option: it causes sequences
such as \d and \w to use Unicode properties to determine character types,
instead of recognizing only characters with codes less than 256 via a lookup
table. If also causes upper/lower casing operations to use Unicode properties
for characters with code points greater than 127, even when UTF is not set.
These behaviours can be changed within the pattern; see the section entitled
<a href="#internaloptions">"Internal Option Setting"</a>
below.
</P>
<P>
Some applications that allow their users to supply patterns may wish to
restrict them for security reasons. If the PCRE2_NEVER_UCP option is passed to
<b>pcre2_compile()</b>, (*UCP) is not allowed, and its appearance in a pattern
causes an error.
</P>
<br><b>
Locking out empty string matching
</b><br>
<P>
Starting a pattern with (*NOTEMPTY) or (*NOTEMPTY_ATSTART) has the same effect
as passing the PCRE2_NOTEMPTY or PCRE2_NOTEMPTY_ATSTART option to whichever
matching function is subsequently called to match the pattern. These options
lock out the matching of empty strings, either entirely, or only at the start
of the subject.
</P>
<br><b>
Disabling auto-possessification
</b><br>
<P>
If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as setting
the PCRE2_NO_AUTO_POSSESS option, or calling <b>pcre2_set_optimize()</b> with
a PCRE2_AUTO_POSSESS_OFF directive. This stops PCRE2 from making quantifiers
possessive when what follows cannot match the repeated item. For example, by
default a+b is treated as a++b. For more details, see the
<a href="pcre2api.html"><b>pcre2api</b></a>
documentation.
</P>
<br><b>
Disabling start-up optimizations
</b><br>
<P>
If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
PCRE2_NO_START_OPTIMIZE option, or calling <b>pcre2_set_optimize()</b> with
a PCRE2_START_OPTIMIZE_OFF directive. This disables several optimizations for
quickly reaching "no match" results. For more details, see the
<a href="pcre2api.html"><b>pcre2api</b></a>
documentation.
</P>
<br><b>
Disabling automatic anchoring
</b><br>
<P>
If a pattern starts with (*NO_DOTSTAR_ANCHOR), it has the same effect as
setting the PCRE2_NO_DOTSTAR_ANCHOR option, or calling <b>pcre2_set_optimize()</b>
with a PCRE2_DOTSTAR_ANCHOR_OFF directive. This disables optimizations that
apply to patterns whose top-level branches all start with .* (match any number
of arbitrary characters). For more details, see the
<a href="pcre2api.html"><b>pcre2api</b></a>
documentation.
</P>
<br><b>
Disabling JIT compilation
</b><br>
<P>
If a pattern that starts with (*NO_JIT) is successfully compiled, an attempt by
the application to apply the JIT optimization by calling
<b>pcre2_jit_compile()</b> is ignored.
</P>
<br><b>
Setting match resource limits
</b><br>
<P>
The <b>pcre2_match()</b> function contains a counter that is incremented every
time it goes round its main loop. The caller of <b>pcre2_match()</b> can set a
limit on this counter, which therefore limits the amount of computing resource
used for a match. The maximum depth of nested backtracking can also be limited;
this indirectly restricts the amount of heap memory that is used, but there is
also an explicit memory limit that can be set.
</P>
<P>
These facilities are provided to catch runaway matches that are provoked by
patterns with huge matching trees. A common example is a pattern with nested
unlimited repeats applied to a long string that does not match. When one of
these limits is reached, <b>pcre2_match()</b> gives an error return. The limits
can also be set by items at the start of the pattern of the form
<pre>
(*LIMIT_HEAP=d)
(*LIMIT_MATCH=d)
(*LIMIT_DEPTH=d)
</pre>
where d is any number of decimal digits. However, the value of the setting must
be less than the value set (or defaulted) by the caller of <b>pcre2_match()</b>
for it to have any effect. In other words, the pattern writer can lower the
limits set by the programmer, but not raise them. If there is more than one
setting of one of these limits, the lower value is used. The heap limit is
specified in kibibytes (units of 1024 bytes).
</P>
<P>
Prior to release 10.30, LIMIT_DEPTH was called LIMIT_RECURSION. This name is
still recognized for backwards compatibility.
</P>
<P>
The heap limit applies only when the <b>pcre2_match()</b> or
<b>pcre2_dfa_match()</b> interpreters are used for matching. It does not apply
to JIT. The match limit is used (but in a different way) when JIT is being
used, or when <b>pcre2_dfa_match()</b> is called, to limit computing resource
usage by those matching functions. The depth limit is ignored by JIT but is
relevant for DFA matching, which uses function recursion for recursions within
the pattern and for lookaround assertions and atomic groups. In this case, the
depth limit controls the depth of such recursion.
<a name="newlines"></a></P>
<br><b>
Newline conventions
</b><br>
<P>
PCRE2 supports six different conventions for indicating line breaks in
strings: a single CR (carriage return) character, a single LF (linefeed)
character, the two-character sequence CRLF, any of the three preceding, any
Unicode newline sequence, or the NUL character (binary zero). The
<a href="pcre2api.html"><b>pcre2api</b></a>
page has
<a href="pcre2api.html#newlines">further discussion</a>
about newlines, and shows how to set the newline convention when calling
<b>pcre2_compile()</b>.
</P>
<P>
It is also possible to specify a newline convention by starting a pattern
string with one of the following sequences:
<pre>
(*CR) carriage return
(*LF) linefeed
(*CRLF) carriage return, followed by linefeed
(*ANYCRLF) any of the three above
(*ANY) all Unicode newline sequences
(*NUL) the NUL character (binary zero)
</pre>
These override the default and the options given to the compiling function. For
example, on a Unix system where LF is the default newline sequence, the pattern
<pre>
(*CR)a.b
</pre>
changes the convention to CR. That pattern matches "a\nb" because LF is no
longer a newline. If more than one of these settings is present, the last one
is used.
</P>
<P>
The newline convention affects where the circumflex and dollar assertions are
true. It also affects the interpretation of the dot metacharacter when
PCRE2_DOTALL is not set, and the behaviour of \N when not followed by an
opening brace. However, it does not affect what the \R escape sequence
matches. By default, this is any Unicode newline sequence, for Perl
compatibility. However, this can be changed; see the next section and the
description of \R in the section entitled
<a href="#newlineseq">"Newline sequences"</a>
below. A change of \R setting can be combined with a change of newline
convention.
</P>
<br><b>
Specifying what \R matches
</b><br>
<P>
It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
at compile time. This effect can also be achieved by starting a pattern with
(*BSR_ANYCRLF). For completeness, (*BSR_UNICODE) is also recognized,
corresponding to PCRE2_BSR_UNICODE.
</P>
<br><a name="SEC4" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
<P>
A regular expression is a pattern that is matched against a subject string from
left to right. Most characters stand for themselves in a pattern, and match the
corresponding characters in the subject. As a trivial example, the pattern
<pre>
The quick brown fox
</pre>
matches a portion of a subject string that is identical to itself. When
caseless matching is specified (the PCRE2_CASELESS option or (?i) within the
pattern), letters are matched independently of case. Note that there are two
ASCII characters, K and S, that, in addition to their lower case ASCII
equivalents, are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F
(long S) respectively when either PCRE2_UTF or PCRE2_UCP is set, unless the
PCRE2_EXTRA_CASELESS_RESTRICT option is in force (either passed to
<b>pcre2_compile()</b> or set by (*CASELESS_RESTRICT) or (?r) within the
pattern). If the PCRE2_EXTRA_TURKISH_CASING option is in force (either passed
to <b>pcre2_compile()</b> or set by (*TURKISH_CASING) within the pattern), then
the 'i' letters are matched according to Turkish and Azeri languages.
</P>
<P>
The power of regular expressions comes from the ability to include wild cards,
character classes, alternatives, and repetitions in the pattern. These are
encoded in the pattern by the use of <i>metacharacters</i>, which do not stand
for themselves but instead are interpreted in some special way.
</P>
<P>
There are two different sets of metacharacters: those that are recognized
anywhere in the pattern except within square brackets, and those that are
recognized within square brackets. Outside square brackets, the metacharacters
are as follows:
<pre>
\ general escape character with several uses
^ assert start of string (or line, in multiline mode)
$ assert end of string (or line, in multiline mode)
. match any character except newline (by default)
[ start character class definition
| start of alternative branch
( start group or control verb
) end group or control verb
* 0 or more quantifier
+ 1 or more quantifier; also "possessive quantifier"
? 0 or 1 quantifier; also quantifier minimizer
{ potential start of min/max quantifier
</pre>
Brace characters { and } are also used to enclose data for constructions such
as \g{2} or \k{name}. In almost all uses of braces, space and/or horizontal
tab characters that follow { or precede } are allowed and are ignored. In the
case of quantifiers, they may also appear before or after the comma. The
exception to this is \u{...} which is an ECMAScript compatibility feature
that is recognized only when the PCRE2_EXTRA_ALT_BSUX option is set. ECMAScript
does not ignore such white space; it causes the item to be interpreted as
literal.
</P>
<P>
Part of a pattern that is in square brackets is called a "character class". In
a character class the only metacharacters are:
<pre>
\ general escape character
^ negate the class, but only if the first character
- indicates character range
[ POSIX character class (if followed by POSIX syntax)
] terminates the character class
</pre>
If a pattern is compiled with the PCRE2_EXTENDED option, most white space in
the pattern, other than in a character class, within a \Q...\E sequence, or
between a # outside a character class and the next newline, inclusive, is
ignored. An escaping backslash can be used to include a white space or a #
character as part of the pattern. If the PCRE2_EXTENDED_MORE option is set, the
same applies, but in addition unescaped space and horizontal tab characters are
ignored inside a character class. Note: only these two characters are ignored,
not the full set of pattern white space characters that are ignored outside a
character class. Option settings can be changed within a pattern; see the
section entitled
<a href="#internaloptions">"Internal Option Setting"</a>
below.
</P>
<P>
The following sections describe the use of each of the metacharacters.
</P>
<br><a name="SEC5" href="#TOC1">BACKSLASH</a><br>
<P>
The backslash character has several uses. Firstly, if it is followed by a
character that is not a digit or a letter, it takes away any special meaning
that character may have. This use of backslash as an escape character applies
both inside and outside character classes.
</P>
<P>
For example, if you want to match a * character, you must write \* in the
pattern. This escaping action applies whether or not the following character
would otherwise be interpreted as a metacharacter, so it is always safe to
precede a non-alphanumeric with backslash to specify that it stands for itself.
In particular, if you want to match a backslash, you write \\.
</P>
<P>
Only ASCII digits and letters have any special meaning after a backslash. All
other characters (in particular, those whose code points are greater than 127)
are treated as literals.
</P>
<P>
If you want to treat all characters in a sequence as literals, you can do so by
putting them between \Q and \E. Note that this includes white space even when
the PCRE2_EXTENDED option is set so that most other white space is ignored. The
behaviour is different from Perl in that $ and @ are handled as literals in
\Q...\E sequences in PCRE2, whereas in Perl, $ and @ cause variable
interpolation. Also, Perl does "double-quotish backslash interpolation" on any
backslashes between \Q and \E which, its documentation says, "may lead to
confusing results". PCRE2 treats a backslash between \Q and \E just like any
other character. Note the following examples:
<pre>
Pattern PCRE2 matches Perl matches
\Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
\Qabc\$xyz\E abc\$xyz abc\$xyz
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz
\QA\B\E A\B A\B
\Q\\E \ \\E
</pre>
The \Q...\E sequence is recognized both inside and outside character classes.
An isolated \E that is not preceded by \Q is ignored. If \Q is not followed
by \E later in the pattern, the literal interpretation continues to the end of
the pattern (that is, \E is assumed at the end). If the isolated \Q is inside
a character class, this causes an error, because the character class is then
not terminated by a closing square bracket.
</P>
<P>
Another difference from Perl is that any appearance of \Q or \E inside what
might otherwise be a quantifier causes PCRE2 not to recognize the sequence as a
quantifier. Perl recognizes a quantifier if (redundantly) either of the numbers
is inside \Q...\E, but not if the separating comma is. When not recognized as
a quantifier a sequence such as {\Q1\E,2} is treated as the literal string
"{1,2}".
<a name="digitsafterbackslash"></a></P>
<br><b>
Non-printing characters
</b><br>
<P>
A second use of backslash provides a way of encoding non-printing characters
in patterns in a visible manner. There is no restriction on the appearance of
non-printing characters in a pattern, but when a pattern is being prepared by
text editing, it is often easier to use one of the following escape sequences
instead of the binary character it represents. In an ASCII or Unicode
environment, these escapes are as follows:
<pre>
\a alarm, that is, the BEL character (hex 07)
\cx "control-x", where x is a non-control ASCII character
\e escape (hex 1B)
\f form feed (hex 0C)
\n linefeed (hex 0A)
\r carriage return (hex 0D) (but see below)
\t tab (hex 09)
\0dd character with octal code 0dd
\ddd character with octal code ddd, or back reference
\o{ddd..} character with octal code ddd..
\xhh character with hex code hh
\x{hhh..} character with hex code hhh..
\N{U+hhh..} character with Unicode hex code point hhh..
</pre>
A description of how back references work is given
<a href="#backreferences">later,</a>
following the discussion of
<a href="#group">parenthesized groups.</a>
</P>
<P>
By default, after \x that is not followed by {, one or two hexadecimal
digits are read (letters can be in upper or lower case). If the character that
follows \x is neither { nor a hexadecimal digit, an error occurs. This is
different from Perl's default behaviour, which generates a NUL character, but
is in line with the behaviour of Perl's 'strict' mode in re.
</P>
<P>
Any number of hexadecimal digits may appear between \x{ and }. If a character
other than a hexadecimal digit appears between \x{ and }, or if there is no
terminating }, an error occurs.
</P>
<P>
Characters whose code points are less than 256 can be defined by either of the
two syntaxes for \x or by an octal sequence. There is no difference in the way
they are handled. For example, \xdc is exactly the same as \x{dc} or \334.
However, using the braced versions does make such sequences easier to read.
</P>
<P>
Support is available for some ECMAScript (aka JavaScript) escape sequences via
two compile-time options. If PCRE2_ALT_BSUX is set, the sequence \x followed
by { is not recognized. Only if \x is followed by two hexadecimal digits is it
recognized as a character escape. Otherwise it is interpreted as a literal "x"
character. In this mode, support for code points greater than 256 is provided
by \u, which must be followed by four hexadecimal digits; otherwise it is
interpreted as a literal "u" character.
</P>
<P>
PCRE2_EXTRA_ALT_BSUX has the same effect as PCRE2_ALT_BSUX and, in addition,
\u{hhh..} is recognized as the character specified by hexadecimal code point.
There may be any number of hexadecimal digits, but unlike other places that
also use curly brackets, spaces are not allowed and would result in the string
being interpreted as a literal. This syntax is from ECMAScript 6.
</P>
<P>
The \N{U+hhh..} escape sequence is recognized only when PCRE2 is operating in
UTF mode. Perl also uses \N{name} to specify characters by Unicode name; PCRE2
does not support this. Note that when \N is not followed by an opening brace
(curly bracket) it has an entirely different meaning, matching any character
that is not a newline.
</P>
<P>
There are some legacy applications where the escape sequence \r is expected to
match a newline. If the PCRE2_EXTRA_ESCAPED_CR_IS_LF option is set, \r in a
pattern is converted to \n so that it matches a LF (linefeed) instead of a CR
(carriage return) character.
</P>
<P>
An error occurs if \c is not followed by a character whose ASCII code point
is in the range 32 to 126. The precise effect of \cx is as follows: if x is a
lower case letter, it is converted to upper case. Then bit 6 of the character
(hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A (A is 41, Z is
5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes hex 7B (; is 3B). If
the code unit following \c has a code point less than 32 or greater than 126,
a compile-time error occurs.
</P>
<P>
For differences in the way some escapes behave in EBCDIC environments,
see section
<a href="#ebcdicenvironments">"EBCDIC environments"</a>
below.
</P>
<br><b>
Octal escapes and back references
</b><br>
<P>
The escape \o must be followed by a sequence of octal digits, enclosed in
braces. An error occurs if this is not the case. This escape provides a way of
specifying character code points as octal numbers greater than 0777, and it
also allows octal numbers and backreferences to be unambiguously distinguished.
</P>
<P>
If braces are not used, after \0 up to two further octal digits are read.
However, if the PCRE2_EXTRA_NO_BS0 option is set, at least one more octal digit
must follow \0 (use \00 to generate a NUL character). Make sure you supply
two digits after the initial zero if the pattern character that follows is
itself an octal digit.
</P>
<P>
Inside a character class, when a backslash is followed by any octal digit, up
to three octal digits are read to generate a code point. Any subsequent digits
stand for themselves. The sequences \8 and \9 are treated as the literal
characters "8" and "9".
</P>
<P>
Outside a character class, Perl's handling of a backslash followed by a digit
other than 0 is complicated by ambiguity, and Perl has changed over time,
causing PCRE2 also to change. From PCRE2 release 10.45 there is an option
called PCRE2_EXTRA_PYTHON_OCTAL that causes PCRE2 to use Python's unambiguous
rules. The next two subsections describe the two sets of rules.
</P>
<P>
For greater clarity and unambiguity, it is best to avoid following \ by a
digit greater than zero. Instead, use \o{...} or \x{...} to specify numerical
character code points, and \g{...} to specify backreferences.
</P>
<br><b>
Perl rules for non-class backslash 1-9
</b><br>
<P>
All the digits that follow the backslash are read as a decimal number. If the
number is less than 10, begins with the digit 8 or 9, or if there are at least
that many previous capture groups in the expression, the entire sequence is
taken as a back reference. Otherwise, up to three octal digits are read to form
a character code. For example:
<pre>
\040 is another way of writing an ASCII space
\40 is the same, provided there are fewer than 40 previous capture groups
\7 is always a backreference
\11 might be a backreference, or another way of writing a tab
\011 is always a tab
\0113 is a tab followed by the character "3"
\113 might be a backreference, otherwise the character with octal code 113
\377 might be a backreference, otherwise the value 255 (decimal)
\81 is always a backreference
</pre>
Note that octal values of 100 or greater that are specified using this syntax
must not be introduced by a leading zero, because no more than three octal
digits are ever read.
</P>
<br><b>
Python rules for non_class backslash 1-9
</b><br>
<P>
If there are at least three octal digits after the backslash, exactly three are
read as an octal code point number, but the value must be no greater than
\377, even in modes where higher code point values are supported. Any
subsequent digits stand for themselves. If there are fewer than three octal
digits, the sequence is taken as a decimal back reference. Thus, for example,
\12 is always a back reference, independent of how many captures there are in
the pattern. An error is generated for a reference to a non-existent capturing
group.
</P>
<br><b>
Constraints on character values
</b><br>
<P>
Characters that are specified using octal or hexadecimal numbers are
limited to certain values, as follows:
<pre>
8-bit non-UTF mode no greater than 0xff
16-bit non-UTF mode no greater than 0xffff
32-bit non-UTF mode no greater than 0xffffffff
All UTF modes no greater than 0x10ffff and a valid code point
</pre>
Invalid Unicode code points are all those in the range 0xd800 to 0xdfff (the
so-called "surrogate" code points). The check for these can be disabled by the
caller of <b>pcre2_compile()</b> by setting the option
PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES. However, this is possible only in UTF-8
and UTF-32 modes, because these values are not representable in UTF-16.
</P>
<br><b>
Escape sequences in character classes
</b><br>
<P>
All the sequences that define a single character value can be used both inside
and outside character classes. In addition, inside a character class, \b is
interpreted as the backspace character (hex 08).
</P>
<P>
When not followed by an opening brace, \N is not allowed in a character class.
\B, \R, and \X are not special inside a character class. Like other
unrecognized alphabetic escape sequences, they cause an error. Outside a
character class, these sequences have different meanings.
</P>
<br><b>
Unsupported escape sequences
</b><br>
<P>
In Perl, the sequences \F, \l, \L, \u, and \U are recognized by its string
handler and used to modify the case of following characters. By default, PCRE2
does not support these escape sequences in patterns. However, if either of the
PCRE2_ALT_BSUX or PCRE2_EXTRA_ALT_BSUX options is set, \U matches a "U"
character, and \u can be used to define a character by code point, as
described above.
</P>
<br><b>
Absolute and relative backreferences
</b><br>
<P>
The sequence \g followed by a signed or unsigned number, optionally enclosed
in braces, is an absolute or relative backreference. A named backreference
can be coded as \g{name}. Backreferences are discussed
<a href="#backreferences">later,</a>
following the discussion of
<a href="#group">parenthesized groups.</a>
</P>
<br><b>
Absolute and relative subroutine calls
</b><br>
<P>
For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
a number enclosed either in angle brackets or single quotes, is an alternative
syntax for referencing a capture group as a subroutine. Details are discussed
<a href="#onigurumasubroutines">later.</a>
Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i>
synonymous. The former is a backreference; the latter is a
<a href="#groupsassubroutines">subroutine</a>
call.
<a name="genericchartypes"></a></P>
<br><b>
Generic character types
</b><br>
<P>
Another use of backslash is for specifying generic character types:
<pre>
\d any decimal digit
\D any character that is not a decimal digit
\h any horizontal white space character
\H any character that is not a horizontal white space character
\N any character that is not a newline
\s any white space character
\S any character that is not a white space character
\v any vertical white space character
\V any character that is not a vertical white space character
\w any "word" character
\W any "non-word" character
</pre>
The \N escape sequence has the same meaning as
<a href="#fullstopdot">the "." metacharacter</a>
when PCRE2_DOTALL is not set, but setting PCRE2_DOTALL does not change the
meaning of \N. Note that when \N is followed by an opening brace it has a
different meaning. See the section entitled
<a href="#digitsafterbackslash">"Non-printing characters"</a>
above for details. Perl also uses \N{name} to specify characters by Unicode
name; PCRE2 does not support this.
</P>
<P>
Each pair of lower and upper case escape sequences partitions the complete set
of characters into two disjoint sets. Any given character matches one, and only
one, of each pair. The sequences can appear both inside and outside character
classes. They each match one character of the appropriate type. If the current
matching point is at the end of the subject string, all of them fail, because
there is no character to match.
</P>
<P>
The default \s characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
space (32), which are defined as white space in the "C" locale. This list may
vary if locale-specific matching is taking place. For example, in some locales
the "non-breaking space" character (\xA0) is recognized as white space, and in
others the VT character is not.
</P>
<P>
A "word" character is an underscore or any character that is a letter or digit.
By default, the definition of letters and digits is controlled by PCRE2's
low-valued character tables, and may vary if locale-specific matching is taking
place (see
<a href="pcre2api.html#localesupport">"Locale support"</a>
in the
<a href="pcre2api.html"><b>pcre2api</b></a>
page). For example, in a French locale such as "fr_FR" in Unix-like systems,
or "french" in Windows, some character codes greater than 127 are used for
accented letters, and these are then matched by \w. The use of locales with
Unicode is discouraged.
</P>
<P>
By default, characters whose code points are greater than 127 never match \d,
\s, or \w, and always match \D, \S, and \W, although this may be different
for characters in the range 128-255 when locale-specific matching is happening.
These escape sequences retain their original meanings from before Unicode
support was available, mainly for efficiency reasons. If the PCRE2_UCP option
is set, the behaviour is changed so that Unicode properties are used to
determine character types, as follows:
<pre>
\d any character that matches \p{Nd} (decimal digit)
\s any character that matches \p{Z} or \h or \v
\w any character that matches \p{L}, \p{N}, \p{Mn}, or \p{Pc}
</pre>
The addition of \p{Mn} (non-spacing mark) and the replacement of an explicit
test for underscore with a test for \p{Pc} (connector punctuation) happened in
PCRE2 release 10.43. This brings PCRE2 into line with Perl.
</P>
<P>
The upper case escapes match the inverse sets of characters. Note that \d
matches only decimal digits, whereas \w matches any Unicode digit, as well as
other character categories. Note also that PCRE2_UCP affects \b, and
\B because they are defined in terms of \w and \W. Matching these sequences
is noticeably slower when PCRE2_UCP is set.
</P>
<P>
The effect of PCRE2_UCP on any one of these escape sequences can be negated by
the options PCRE2_EXTRA_ASCII_BSD, PCRE2_EXTRA_ASCII_BSS, and
PCRE2_EXTRA_ASCII_BSW, respectively. These options can be set and reset within
a pattern by means of an internal option setting
<a href="#internaloptions">(see below).</a>
</P>
<P>
The sequences \h, \H, \v, and \V, in contrast to the other sequences, which
match only ASCII characters by default, always match a specific list of code
points, whether or not PCRE2_UCP is set. The horizontal space characters are:
<pre>
U+0009 Horizontal tab (HT)
U+0020 Space
U+00A0 Non-break space
U+1680 Ogham space mark
U+180E Mongolian vowel separator
U+2000 En quad
U+2001 Em quad
U+2002 En space
U+2003 Em space
U+2004 Three-per-em space
U+2005 Four-per-em space
U+2006 Six-per-em space
U+2007 Figure space
U+2008 Punctuation space
U+2009 Thin space
U+200A Hair space
U+202F Narrow no-break space
U+205F Medium mathematical space
U+3000 Ideographic space
</pre>
The vertical space characters are:
<pre>
U+000A Linefeed (LF)
U+000B Vertical tab (VT)
U+000C Form feed (FF)
U+000D Carriage return (CR)
U+0085 Next line (NEL)
U+2028 Line separator
U+2029 Paragraph separator
</pre>
In 8-bit, non-UTF-8 mode, only the characters with code points less than 256
are relevant.
<a name="newlineseq"></a></P>
<br><b>
Newline sequences
</b><br>
<P>
Outside a character class, by default, the escape sequence \R matches any
Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent to the
following:
<pre>
(?>\r\n|\n|\x0b|\f|\r|\x85)
</pre>
This is an example of an "atomic group", details of which are given
<a href="#atomicgroup">below.</a>
This particular group matches either the two-character sequence CR followed by
LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
line, U+0085). Because this is an atomic group, the two-character sequence is
treated as a single unit that cannot be split.
</P>
<P>
In other modes, two additional characters whose code points are greater than 255
are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
Unicode support is not needed for these characters to be recognized.
</P>
<P>
It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
at compile time. (BSR is an abbreviation for "backslash R".) This can be made
the default when PCRE2 is built; if this is the case, the other behaviour can
be requested via the PCRE2_BSR_UNICODE option. It is also possible to specify
these settings by starting a pattern string with one of the following
sequences:
<pre>
(*BSR_ANYCRLF) CR, LF, or CRLF only
(*BSR_UNICODE) any Unicode newline sequence
</pre>
These override the default and the options given to the compiling function.
Note that these special settings, which are not Perl-compatible, are recognized
only at the very start of a pattern, and that they must be in upper case. If
more than one of them is present, the last one is used. They can be combined
with a change of newline convention; for example, a pattern can start with:
<pre>
(*ANY)(*BSR_ANYCRLF)
</pre>
They can also be combined with the (*UTF) or (*UCP) special sequences. Inside a
character class, \R is treated as an unrecognized escape sequence, and causes
an error.
<a name="uniextseq"></a></P>
<br><b>
Unicode character properties
</b><br>
<P>
When PCRE2 is built with Unicode support (the default), three additional escape
sequences that match characters with specific properties are available. They
can be used in any mode, though in 8-bit and 16-bit non-UTF modes these
sequences are of course limited to testing characters whose code points are
less than U+0100 or U+10000, respectively. In 32-bit non-UTF mode, code points
greater than 0x10ffff (the Unicode limit) may be encountered. These are all
treated as being in the Unknown script and with an unassigned type.
</P>
<P>
Matching characters by Unicode property is not fast, because PCRE2 has to do a
multistage table lookup in order to find a character's property. That is why
the traditional escape sequences such as \d and \w do not use Unicode
properties in PCRE2 by default, though you can make them do so by setting the
PCRE2_UCP option or by starting the pattern with (*UCP).
</P>
<P>
The extra escape sequences that provide property support are:
<pre>
\p{<i>xx</i>} a character with the <i>xx</i> property
\P{<i>xx</i>} a character without the <i>xx</i> property
\X a Unicode extended grapheme cluster
</pre>
For compatibility with Perl, negation can be specified by including a
circumflex between the opening brace and the property. For example, \p{^Lu} is
the same as \P{Lu}.
</P>
<P>
In accordance with Unicode's "loose matching" rules, ASCII white space
characters, hyphens, and underscores are ignored in the properties represented
by <i>xx</i> above. As well as the space character, ASCII white space can be
tab, linefeed, vertical tab, formfeed, or carriage return.
</P>
<P>
Some properties are specified as a name only; others as a name and a value,
separated by a colon or an equals sign. The names and values consist of ASCII
letters and digits (with one Perl-specific exception, see below). They are not
case sensitive. Note, however, that the escapes themselves, \p and \P,
<i>are</i> case sensitive. There are abbreviations for many names. The following
examples are all equivalent:
<pre>
\p{bidiclass=al}
\p{BC=al}
\p{ Bidi_Class : AL }
\p{ Bi-di class = Al }
\P{ ^ Bi-di class = Al }
</pre>
There is support for Unicode script names, Unicode general category properties,
"Any", which matches any character (including newline), Bidi_Class, a number of
binary (yes/no) properties, and some special PCRE2 properties (described
<a href="#extraprops">below).</a>
Certain other Perl properties such as "InMusicalSymbols" are not supported by
PCRE2. Note that \P{Any} does not match any characters, so always causes a
match failure.
</P>
<br><b>
Script properties for \p and \P
</b><br>
<P>
There are three different syntax forms for matching a script. Each Unicode
character has a basic script and, optionally, a list of other scripts ("Script
Extensions") with which it is commonly used. Using the Adlam script as an
example, \p{sc:Adlam} matches characters whose basic script is Adlam, whereas
\p{scx:Adlam} matches, in addition, characters that have Adlam in their
extensions list. The full names "script" and "script extensions" for the
property types are recognized and, as for all property specifications, an
equals sign is an alternative to the colon. If a script name is given without a
property type, for example, \p{Adlam}, it is treated as \p{scx:Adlam}. Perl
changed to this interpretation at release 5.26 and PCRE2 changed at release
10.40.
</P>
<P>
Unassigned characters (and in non-UTF 32-bit mode, characters with code points
greater than 0x10FFFF) are assigned the "Unknown" script. Others that are not
part of an identified script are lumped together as "Common". The current list
of recognized script names and their 4-character abbreviations can be obtained
by running this command:
<pre>
pcre2test -LS
</PRE>
</P>
<br><b>
The general category property for \p and \P
</b><br>
<P>
Each character has exactly one Unicode general category property, specified by
a two-letter abbreviation. If only one letter is specified with \p or \P, it
includes all the general category properties that start with that letter. In
this case, in the absence of negation, the curly brackets in the escape
sequence are optional; these two examples have the same effect:
<pre>
\p{L}
\pL
</pre>
The following general category property codes are supported:
<pre>
C Other
Cc Control
Cf Format
Cn Unassigned
Co Private use
Cs Surrogate
L Letter
Lc Cased letter
Ll Lower case letter
Lm Modifier letter
Lo Other letter
Lt Title case letter
Lu Upper case letter
M Mark
Mc Spacing mark
Me Enclosing mark
Mn Non-spacing mark
N Number
Nd Decimal number
Nl Letter number
No Other number
P Punctuation
Pc Connector punctuation
Pd Dash punctuation
Pe Close punctuation
Pf Final punctuation
Pi Initial punctuation
Po Other punctuation
Ps Open punctuation
S Symbol
Sc Currency symbol
Sk Modifier symbol
Sm Mathematical symbol
So Other symbol
Z Separator
Zl Line separator
Zp Paragraph separator
Zs Space separator
</pre>
Perl originally used the name L& for the Lc property. This is still supported
by Perl, but discouraged. PCRE2 also still supports it. This property matches
any character that has the Lu, Ll, or Lt property, in other words, any letter
that is not classified as a modifier or "other". From release 10.45 of PCRE2
the properties Lu, Ll, and Lt are all treated as Lc when case-independent
matching is set by the PCRE2_CASELESS option or (?i) within the pattern. The
other properties are not affected by caseless matching.
</P>
<P>
The Cs (Surrogate) property applies only to characters whose code points are in
the range U+D800 to U+DFFF. These characters are no different to any other
character when PCRE2 is not in UTF mode (using the 16-bit or 32-bit library).
However, they are not valid in Unicode strings and so cannot be tested by PCRE2
in UTF mode, unless UTF validity checking has been turned off (see the
discussion of PCRE2_NO_UTF_CHECK in the
<a href="pcre2api.html"><b>pcre2api</b></a>
page).
</P>
<P>
The long synonyms for property names that Perl supports (such as \p{Letter})
are not supported by PCRE2, nor is it permitted to prefix any of these
properties with "Is".
</P>
<P>
No character that is in the Unicode table has the Cn (unassigned) property.
Instead, this property is assumed for any code point that is not in the
Unicode table.
</P>
<br><b>
Binary (yes/no) properties for \p and \P
</b><br>
<P>
Unicode defines a number of binary properties, that is, properties whose only
values are true or false. You can obtain a list of those that are recognized by
\p and \P, along with their abbreviations, by running this command:
<pre>
pcre2test -LP
</PRE>
</P>
<br><b>
The Bidi_Class property for \p and \P
</b><br>
<P>
<pre>
\p{Bidi_Class:<class>} matches a character with the given class
\p{BC:<class>} matches a character with the given class
</pre>
The recognized classes are:
<pre>
AL Arabic letter
AN Arabic number
B paragraph separator
BN boundary neutral
CS common separator
EN European number
ES European separator
ET European terminator
FSI first strong isolate
L left-to-right
LRE left-to-right embedding
LRI left-to-right isolate
LRO left-to-right override
NSM non-spacing mark
ON other neutral
PDF pop directional format
PDI pop directional isolate
R right-to-left
RLE right-to-left embedding
RLI right-to-left isolate
RLO right-to-left override
S segment separator
WS white space
</pre>
As in all property specifications, an equals sign may be used instead of a
colon and the class names are case-insensitive. Only the short names listed
above are recognized; PCRE2 does not at present support any long alternatives.
</P>
<br><b>
Extended grapheme clusters
</b><br>
<P>
The \X escape matches any number of Unicode characters that form an "extended
grapheme cluster", and treats the sequence as an atomic group
<a href="#atomicgroup">(see below).</a>
Unicode supports various kinds of composite character by giving each character
a grapheme breaking property, and having rules that use these properties to
define the boundaries of extended grapheme clusters. The rules are defined in
Unicode Standard Annex 29, "Unicode Text Segmentation". Unicode 11.0.0
abandoned the use of some previous properties that had been used for emojis.
Instead it introduced various emoji-specific properties. PCRE2 uses only the
Extended Pictographic property.
</P>
<P>
\X always matches at least one character. Then it decides whether to add
additional characters according to the following rules for ending a cluster:
</P>
<P>
1. End at the end of the subject string.
</P>
<P>
2. Do not end between CR and LF; otherwise end after any control character.
</P>
<P>
3. Do not break Hangul (a Korean script) syllable sequences. Hangul characters
are of five types: L, V, T, LV, and LVT. An L character may be followed by an
L, V, LV, or LVT character; an LV or V character may be followed by a V or T
character; an LVT or T character may be followed only by a T character.
</P>
<P>
4. Do not end before extending characters or spacing marks or the zero-width
joiner (ZWJ) character. Characters with the "mark" property always have the
"extend" grapheme breaking property.
</P>
<P>
5. Do not end after prepend characters.
</P>
<P>
6. Do not end within emoji modifier sequences or emoji ZWJ (zero-width
joiner) sequences. An emoji ZWJ sequence consists of a character with the
Extended_Pictographic property, optionally followed by one or more characters
with the Extend property, followed by the ZWJ character, followed by another
Extended_Pictographic character.
</P>
<P>
7. Do not break within emoji flag sequences. That is, do not break between
regional indicator (RI) characters if there are an odd number of RI characters
before the break point.
</P>
<P>
8. Otherwise, end the cluster.
<a name="extraprops"></a></P>
<br><b>
PCRE2's additional properties
</b><br>
<P>
As well as the standard Unicode properties described above, PCRE2 supports four
more that make it possible to convert traditional escape sequences such as \w
and \s to use Unicode properties. PCRE2 uses these non-standard, non-Perl
properties internally when PCRE2_UCP is set. However, they may also be used
explicitly. These properties are:
<pre>
Xan Any alphanumeric character
Xps Any POSIX space character
Xsp Any Perl space character
Xwd Any Perl "word" character
</pre>
Xan matches characters that have either the L (letter) or the N (number)
property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
carriage return, and any other character that has the Z (separator) property
(this includes the space character). Xsp is the same as Xps; in PCRE1 it used
to exclude vertical tab, for Perl compatibility, but Perl changed. Xwd matches
the same characters as Xan, plus those that match Mn (non-spacing mark) or Pc
(connector punctuation, which includes underscore).
</P>
<P>
There is another non-standard property, Xuc, which matches any character that
can be represented by a Universal Character Name in C++ and other programming
languages. These are the characters $, @, ` (grave accent), and al