UNPKG

pcre2

Version:

A PCRE2 binding for node.js

1,250 lines (1,243 loc) 167 kB
<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">SPECIAL START-OF-PATTERN ITEMS</a> <li><a name="TOC3" href="#SEC3">EBCDIC CHARACTER CODES</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">POSIX CHARACTER CLASSES</a> <li><a name="TOC11" href="#SEC11">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a> <li><a name="TOC12" href="#SEC12">VERTICAL BAR</a> <li><a name="TOC13" href="#SEC13">INTERNAL OPTION SETTING</a> <li><a name="TOC14" href="#SEC14">GROUPS</a> <li><a name="TOC15" href="#SEC15">DUPLICATE GROUP NUMBERS</a> <li><a name="TOC16" href="#SEC16">NAMED CAPTURE GROUPS</a> <li><a name="TOC17" href="#SEC17">REPETITION</a> <li><a name="TOC18" href="#SEC18">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a> <li><a name="TOC19" href="#SEC19">BACKREFERENCES</a> <li><a name="TOC20" href="#SEC20">ASSERTIONS</a> <li><a name="TOC21" href="#SEC21">NON-ATOMIC ASSERTIONS</a> <li><a name="TOC22" href="#SEC22">SCRIPT RUNS</a> <li><a name="TOC23" href="#SEC23">CONDITIONAL GROUPS</a> <li><a name="TOC24" href="#SEC24">COMMENTS</a> <li><a name="TOC25" href="#SEC25">RECURSIVE PATTERNS</a> <li><a name="TOC26" href="#SEC26">GROUPS AS SUBROUTINES</a> <li><a name="TOC27" href="#SEC27">ONIGURUMA SUBROUTINE SYNTAX</a> <li><a name="TOC28" href="#SEC28">CALLOUTS</a> <li><a name="TOC29" href="#SEC29">BACKTRACKING CONTROL</a> <li><a name="TOC30" href="#SEC30">SEE ALSO</a> <li><a name="TOC31" href="#SEC31">AUTHOR</a> <li><a name="TOC32" href="#SEC32">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 (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, .NET, and Oniguruma. </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">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. </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. 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. 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. 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="SEC3" href="#TOC1">EBCDIC CHARACTER CODES</a><br> <P> PCRE2 can be compiled to run in an environment that uses EBCDIC as its character code instead of ASCII or Unicode (typically a mainframe system). 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. </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. </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 { start min/max quantifier </pre> 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, and characters between a # outside a character class and the next newline, inclusive, are 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. This 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 not terminated by a closing square bracket. <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 any printable 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 backreference \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> By default, after \x that is not followed by {, from zero to two hexadecimal digits are read (letters can be in upper or lower case). 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. 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> The precise effect of \cx on ASCII characters 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 value less than 32 or greater than 126, a compile-time error occurs. </P> <P> When PCRE2 is compiled in EBCDIC mode, \N{U+hhh..} is not supported. \a, \e, \f, \n, \r, and \t generate the appropriate EBCDIC code values. The \c escape is processed as specified for Perl in the <b>perlebcdic</b> document. The only characters that are allowed after \c are A-Z, a-z, or one of @, [, \, ], ^, _, or ?. Any other character provokes a compile-time error. The sequence \c@ encodes character code 0; after \c the letters (in either case) encode characters 1-26 (hex 01 to hex 1A); [, \, ], ^, and _ encode characters 27-31 (hex 1B to hex 1F), and \c? becomes either 255 (hex FF) or 95 (hex 5F). </P> <P> Thus, apart from \c?, these escapes generate the same character code values as they do in an ASCII environment, though the meanings of the values mostly differ. For example, \cG always generates code value 7, which is BEL in ASCII but DEL in EBCDIC. </P> <P> The sequence \c? generates DEL (127, hex 7F) in an ASCII environment, but because 127 is not a control character in EBCDIC, Perl makes it generate the APC character. Unfortunately, there are several variants of EBCDIC. In most of them the APC character has the value 255 (hex FF), but in the one Perl calls POSIX-BC its value is 95 (hex 5F). If certain other characters have POSIX-BC values, PCRE2 makes \c? generate 95; otherwise it generates 255. </P> <P> After \0 up to two further octal digits are read. If there are fewer than two digits, just those that are present are used. Thus the sequence \0\x\015 specifies two binary zeros followed by a CR character (code value 13). Make sure you supply two digits after the initial zero if the pattern character that follows is itself an octal digit. </P> <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 is a recent addition to Perl; it provides way of specifying character code points as octal numbers greater than 0777, and it also allows octal numbers and backreferences to be unambiguously specified. </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. The following paragraphs describe the old, ambiguous syntax. </P> <P> The handling of a backslash followed by a digit other than 0 is complicated, and Perl has changed over time, causing PCRE2 also to change. </P> <P> Outside a character class, PCRE2 reads the digit and any following digits 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 <i>backreference</i>. A description of how this works is given <a href="#backreferences">later,</a> following the discussion of <a href="#group">parenthesized groups.</a> Otherwise, up to three octal digits are read to form a character code. </P> <P> Inside a character class, PCRE2 handles \8 and \9 as the literal characters "8" and "9", and otherwise reads up to three octal digits following the backslash, using them to generate a data character. Any subsequent digits stand for themselves. For example, outside a character class: <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 .sp </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> 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&#60;...&#62; (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} or \p{N}, plus underscore </pre> 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 any Unicode letter, and underscore. 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 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> (?&#62;\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 abbrevation 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 and 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. The extra escape sequences 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> The property names represented by <i>xx</i> above are case-sensitive. There is support for Unicode script names, Unicode general category properties, "Any", which matches any character (including newline), and some special PCRE2 properties (described in the <a href="#extraprops">next section).</a> 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> <P> Sets of Unicode characters are defined as belonging to certain scripts. A character from one of these sets can be matched using a script name. For example: <pre> \p{Greek} \P{Han} </pre> 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 scripts is: </P> <P> Adlam, Ahom, Anatolian_Hieroglyphs, Arabic, Armenian, Avestan, Balinese, Bamum, Bassa_Vah, Batak, Bengali, Bhaiksuki, Bopomofo, Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Carian, Caucasian_Albanian, Chakma, Cham, Cherokee, Chorasmian, Common, Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Dives_Akuru, Dogra, Duployan, Egyptian_Hieroglyphs, Elbasan, Elymaic, Ethiopic, Georgian, Glagolitic, Gothic, Grantha, Greek, Gujarati, Gunjala_Gondi, Gurmukhi, Han, Hangul, Hanifi_Rohingya, Hanunoo, Hatran, Hebrew, Hiragana, Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscriptional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li, Kharoshthi, Khitan_Small_Script, Khmer, Khojki, Khudawadi, Lao, Latin, Lepcha, Limbu, Linear_A, Linear_B, Lisu, Lycian, Lydian, Mahajani, Makasar, Malayalam, Mandaic, Manichaean, Marchen, Masaram_Gondi, Medefaidrin, Meetei_Mayek, Mende_Kikakui, Meroitic_Cursive, Meroitic_Hieroglyphs, Miao, Modi, Mongolian, Mro, Multani, Myanmar, Nabataean, Nandinagari, New_Tai_Lue, Newa, Nko, Nushu, Nyakeng_Puachue_Hmong, Ogham, Ol_Chiki, Old_Hungarian, Old_Italic, Old_North_Arabian, Old_Permic, Old_Persian, Old_Sogdian, Old_South_Arabian, Old_Turkic, Oriya, Osage, Osmanya, Pahawh_Hmong, Palmyrene, Pau_Cin_Hau, Phags_Pa, Phoenician, Psalter_Pahlavi, Rejang, Runic, Samaritan, Saurashtra, Sharada, Shavian, Siddham, SignWriting, Sinhala, Sogdian, Sora_Sompeng, Soyombo, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, Takri, Tamil, Tangut, Telugu, Thaana, Thai, Tibetan, Tifinagh, Tirhuta, Ugaritic, Unknown, Vai, Wancho, Warang_Citi, Yezidi, Yi, Zanabazar_Square. </P> <P> Each character has exactly one Unicode general category property, specified by a two-letter abbreviation. For compatibility with Perl, negation can be specified by including a circumflex between the opening brace and the property name. For example, \p{^Lu} is the same as \P{Lu}. </P> <P> 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 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> The special property L& is also supported: it matches a character that has the Lu, Ll, or Lt property, in other words, a letter that is not classified as a modifier or "other". </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> <P> Specifying caseless matching does not affect these escape sequences. For example, \p{Lu} always matches only upper case letters. This is different from the behaviour of current versions of Perl. </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> <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 follwed only by a T character. </P> <P> 4. Do not end before extending characters or spacing marks or the "zero-width joiner" 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 break within emoji modifier sequences or emoji zwj sequences. That is, do not break between characters with the Extended_Pictographic property. Extend and ZWJ characters are allowed between the characters. </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. 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 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 all characters with Unicode code points greater than or equal to U+00A0, except for the surrogates U+D800 to U+DFFF. Note that most base (ASCII) characters are excluded. (Universal Character Names are of the form \uHHHH or \UHHHHHHHH where H is a hexadecimal digit. Note that the Xuc property does not match these sequences but the characters that they represent.) <a name="resetmatchstart"></a></P> <br><b> Resetting the match start </b><br> <P> In normal use, the escape sequence \K causes any previously matched characters not to be included in the final matched sequence that is returned. For example, the pattern: <pre> foo\Kbar </pre> matches "foobar", but reports that it has matched "bar". \K does not interact with anchoring in any way. The pattern: <pre> ^foo\Kbar </pre> matches only when the subject begins with "foobar" (in single line mode), though it again reports the matched string as "bar". This feature is similar to a lookbehind assertion <a href="#lookbehind">(described below).</a> However, in this case, the part of the subject before the real match does not have to be of fixed length, as lookbehind assertions do. The use of \K does not interfere with the setting of <a href="#group">captured substrings.</a> For example, when the pattern <pre> (foo)\Kbar </pre> matches "foobar", the first substring is still set to "foo". </P> <P> Perl used to document that the use of \K within lookaround assertions is "not well defined", but from version 5.32.0 Perl does not support this usage at all. In PCRE2, \K is acted upon when it occurs inside positive assertions, but is ignored in negative assertions. Note that when a pattern such as (?=ab\K) matches, the reported start of the match can be greater than the end of the match. Using \K in a lookbehind assertion at the start of a pattern can also lead to odd effects. For example, consider this pattern: <pre> (?&#60;=\Kfoo)bar </pre> If the subject is "foobar", a call to <b>pcre2_match()</b> with a starting offset of 3 succeeds and reports the matching string as "foobar", that is, the start of the reported match is earlier than where the match started. <a name="smallassertions"></a></P> <br><b> Simple assertions </b><br> <P> The final use of backslash is for certain simple assertions. An assertion specifies a condition that has to be met at a particular point in a match, without consuming any characters from the subject string. The use of groups for more complicated assertions is described <a href="#bigassertions">below.</a> The backslashed assertions are: <pre> \b matches at a word boundary \B matches when not at a word boundary \A matches at the start of the subject \Z matches at the end of the subject also matches before a newline at the end of the subject \z matches only at the end of the subject \G matches at the first matching position in the subject </pre> Inside a character class, \b has a different meaning; it matches the backspace character. If any other of these assertions appears in a character class, an "invalid escape sequence" error is generated. </P> <P> A word boundary is a position in the subject string where the current character and the previous character do not both match \w or \W (i.e. one matches \w and the other matches \W), or the start or end of the string if the first or last character matches \w, respectively. When PCRE2 is built with Unicode support, the meanings of \w and \W can be changed by setting the PCRE2_UCP option. When this is done, it also affects \b and \B. Neither PCRE2 nor Perl has a separate "start of word" or "end of word" metasequence. However, whatever follows \b normally determines which it is. For example, the fragment \ba matches "a" at the start of a word. </P> <P> The \A, \Z, and \z assertions differ from the traditional circumflex and dollar (described in the next section) in that they only ever match at the very start and end of the subject string, whatever options are set. Thus, they are independent of multiline mode. These three assertions are not affected by the PCRE2_NOTBOL or PCRE2_NOTEOL options, which affect only the behaviour of the circumflex and dollar metacharacters. However, if the <i>startoffset</i> argument of <b>pcre2_match()</b> is non-zero, indicating that matching is to start at a point other than the beginning of the subject, \A can never match. The difference between \Z and \z is that \Z matches before a newline at the end of the string as well as at the very end, whereas \z matches only at the end. </P> <P> The \G assertion is true only when the current matching position is at the start point of the matching process, as specified by the <i>startoffset</i> argument of <b>pcre2_match()</b>. It differs from \A when the value of <i>startoffset</i> is non-zero. By calling <b>pcre2_match()</b> multiple times with appropriate arguments, you can mimic Perl's /g option, and it is in this kind of implementation where \G can be useful. </P> <P> Note, however, that PCRE2's implementation of \G, being true at the starting character of the matching process, is subtly different from Perl's, which defines it as true at the end of the previous match. In