github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3855 lines
168 KiB

  1. <html>
  2. <head>
  3. <title>pcre2pattern specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcre2pattern man page</h1>
  7. <p>
  8. Return to the <a href="index.html">PCRE2 index page</a>.
  9. </p>
  10. <p>
  11. This page is part of the PCRE2 HTML documentation. It was generated
  12. automatically from the original man page. If there is any nonsense in it,
  13. please consult the man page, in case the conversion went wrong.
  14. <br>
  15. <ul>
  16. <li><a name="TOC1" href="#SEC1">PCRE2 REGULAR EXPRESSION DETAILS</a>
  17. <li><a name="TOC2" href="#SEC2">SPECIAL START-OF-PATTERN ITEMS</a>
  18. <li><a name="TOC3" href="#SEC3">EBCDIC CHARACTER CODES</a>
  19. <li><a name="TOC4" href="#SEC4">CHARACTERS AND METACHARACTERS</a>
  20. <li><a name="TOC5" href="#SEC5">BACKSLASH</a>
  21. <li><a name="TOC6" href="#SEC6">CIRCUMFLEX AND DOLLAR</a>
  22. <li><a name="TOC7" href="#SEC7">FULL STOP (PERIOD, DOT) AND \N</a>
  23. <li><a name="TOC8" href="#SEC8">MATCHING A SINGLE CODE UNIT</a>
  24. <li><a name="TOC9" href="#SEC9">SQUARE BRACKETS AND CHARACTER CLASSES</a>
  25. <li><a name="TOC10" href="#SEC10">POSIX CHARACTER CLASSES</a>
  26. <li><a name="TOC11" href="#SEC11">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a>
  27. <li><a name="TOC12" href="#SEC12">VERTICAL BAR</a>
  28. <li><a name="TOC13" href="#SEC13">INTERNAL OPTION SETTING</a>
  29. <li><a name="TOC14" href="#SEC14">GROUPS</a>
  30. <li><a name="TOC15" href="#SEC15">DUPLICATE GROUP NUMBERS</a>
  31. <li><a name="TOC16" href="#SEC16">NAMED CAPTURE GROUPS</a>
  32. <li><a name="TOC17" href="#SEC17">REPETITION</a>
  33. <li><a name="TOC18" href="#SEC18">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
  34. <li><a name="TOC19" href="#SEC19">BACKREFERENCES</a>
  35. <li><a name="TOC20" href="#SEC20">ASSERTIONS</a>
  36. <li><a name="TOC21" href="#SEC21">NON-ATOMIC ASSERTIONS</a>
  37. <li><a name="TOC22" href="#SEC22">SCRIPT RUNS</a>
  38. <li><a name="TOC23" href="#SEC23">CONDITIONAL GROUPS</a>
  39. <li><a name="TOC24" href="#SEC24">COMMENTS</a>
  40. <li><a name="TOC25" href="#SEC25">RECURSIVE PATTERNS</a>
  41. <li><a name="TOC26" href="#SEC26">GROUPS AS SUBROUTINES</a>
  42. <li><a name="TOC27" href="#SEC27">ONIGURUMA SUBROUTINE SYNTAX</a>
  43. <li><a name="TOC28" href="#SEC28">CALLOUTS</a>
  44. <li><a name="TOC29" href="#SEC29">BACKTRACKING CONTROL</a>
  45. <li><a name="TOC30" href="#SEC30">SEE ALSO</a>
  46. <li><a name="TOC31" href="#SEC31">AUTHOR</a>
  47. <li><a name="TOC32" href="#SEC32">REVISION</a>
  48. </ul>
  49. <br><a name="SEC1" href="#TOC1">PCRE2 REGULAR EXPRESSION DETAILS</a><br>
  50. <P>
  51. The syntax and semantics of the regular expressions that are supported by PCRE2
  52. are described in detail below. There is a quick-reference syntax summary in the
  53. <a href="pcre2syntax.html"><b>pcre2syntax</b></a>
  54. page. PCRE2 tries to match Perl syntax and semantics as closely as it can.
  55. PCRE2 also supports some alternative regular expression syntax (which does not
  56. conflict with the Perl syntax) in order to provide some compatibility with
  57. regular expressions in Python, .NET, and Oniguruma.
  58. </P>
  59. <P>
  60. Perl's regular expressions are described in its own documentation, and regular
  61. expressions in general are covered in a number of books, some of which have
  62. copious examples. Jeffrey Friedl's "Mastering Regular Expressions", published
  63. by O'Reilly, covers regular expressions in great detail. This description of
  64. PCRE2's regular expressions is intended as reference material.
  65. </P>
  66. <P>
  67. This document discusses the regular expression patterns that are supported by
  68. PCRE2 when its main matching function, <b>pcre2_match()</b>, is used. PCRE2 also
  69. has an alternative matching function, <b>pcre2_dfa_match()</b>, which matches
  70. using a different algorithm that is not Perl-compatible. Some of the features
  71. discussed below are not available when DFA matching is used. The advantages and
  72. disadvantages of the alternative function, and how it differs from the normal
  73. function, are discussed in the
  74. <a href="pcre2matching.html"><b>pcre2matching</b></a>
  75. page.
  76. </P>
  77. <br><a name="SEC2" href="#TOC1">SPECIAL START-OF-PATTERN ITEMS</a><br>
  78. <P>
  79. A number of options that can be passed to <b>pcre2_compile()</b> can also be set
  80. by special items at the start of a pattern. These are not Perl-compatible, but
  81. are provided to make these options accessible to pattern writers who are not
  82. able to change the program that processes the pattern. Any number of these
  83. items may appear, but they must all be together right at the start of the
  84. pattern string, and the letters must be in upper case.
  85. </P>
  86. <br><b>
  87. UTF support
  88. </b><br>
  89. <P>
  90. In the 8-bit and 16-bit PCRE2 libraries, characters may be coded either as
  91. single code units, or as multiple UTF-8 or UTF-16 code units. UTF-32 can be
  92. specified for the 32-bit library, in which case it constrains the character
  93. values to valid Unicode code points. To process UTF strings, PCRE2 must be
  94. built to include Unicode support (which is the default). When using UTF strings
  95. you must either call the compiling function with one or both of the PCRE2_UTF
  96. or PCRE2_MATCH_INVALID_UTF options, or the pattern must start with the special
  97. sequence (*UTF), which is equivalent to setting the relevant PCRE2_UTF. How
  98. setting a UTF mode affects pattern matching is mentioned in several places
  99. below. There is also a summary of features in the
  100. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  101. page.
  102. </P>
  103. <P>
  104. Some applications that allow their users to supply patterns may wish to
  105. restrict them to non-UTF data for security reasons. If the PCRE2_NEVER_UTF
  106. option is passed to <b>pcre2_compile()</b>, (*UTF) is not allowed, and its
  107. appearance in a pattern causes an error.
  108. </P>
  109. <br><b>
  110. Unicode property support
  111. </b><br>
  112. <P>
  113. Another special sequence that may appear at the start of a pattern is (*UCP).
  114. This has the same effect as setting the PCRE2_UCP option: it causes sequences
  115. such as \d and \w to use Unicode properties to determine character types,
  116. instead of recognizing only characters with codes less than 256 via a lookup
  117. table. If also causes upper/lower casing operations to use Unicode properties
  118. for characters with code points greater than 127, even when UTF is not set.
  119. These behaviours can be changed within the pattern; see the section entitled
  120. <a href="#internaloptions">"Internal Option Setting"</a>
  121. below.
  122. </P>
  123. <P>
  124. Some applications that allow their users to supply patterns may wish to
  125. restrict them for security reasons. If the PCRE2_NEVER_UCP option is passed to
  126. <b>pcre2_compile()</b>, (*UCP) is not allowed, and its appearance in a pattern
  127. causes an error.
  128. </P>
  129. <br><b>
  130. Locking out empty string matching
  131. </b><br>
  132. <P>
  133. Starting a pattern with (*NOTEMPTY) or (*NOTEMPTY_ATSTART) has the same effect
  134. as passing the PCRE2_NOTEMPTY or PCRE2_NOTEMPTY_ATSTART option to whichever
  135. matching function is subsequently called to match the pattern. These options
  136. lock out the matching of empty strings, either entirely, or only at the start
  137. of the subject.
  138. </P>
  139. <br><b>
  140. Disabling auto-possessification
  141. </b><br>
  142. <P>
  143. If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as setting
  144. the PCRE2_NO_AUTO_POSSESS option. This stops PCRE2 from making quantifiers
  145. possessive when what follows cannot match the repeated item. For example, by
  146. default a+b is treated as a++b. For more details, see the
  147. <a href="pcre2api.html"><b>pcre2api</b></a>
  148. documentation.
  149. </P>
  150. <br><b>
  151. Disabling start-up optimizations
  152. </b><br>
  153. <P>
  154. If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
  155. PCRE2_NO_START_OPTIMIZE option. This disables several optimizations for quickly
  156. reaching "no match" results. For more details, see the
  157. <a href="pcre2api.html"><b>pcre2api</b></a>
  158. documentation.
  159. </P>
  160. <br><b>
  161. Disabling automatic anchoring
  162. </b><br>
  163. <P>
  164. If a pattern starts with (*NO_DOTSTAR_ANCHOR), it has the same effect as
  165. setting the PCRE2_NO_DOTSTAR_ANCHOR option. This disables optimizations that
  166. apply to patterns whose top-level branches all start with .* (match any number
  167. of arbitrary characters). For more details, see the
  168. <a href="pcre2api.html"><b>pcre2api</b></a>
  169. documentation.
  170. </P>
  171. <br><b>
  172. Disabling JIT compilation
  173. </b><br>
  174. <P>
  175. If a pattern that starts with (*NO_JIT) is successfully compiled, an attempt by
  176. the application to apply the JIT optimization by calling
  177. <b>pcre2_jit_compile()</b> is ignored.
  178. </P>
  179. <br><b>
  180. Setting match resource limits
  181. </b><br>
  182. <P>
  183. The <b>pcre2_match()</b> function contains a counter that is incremented every
  184. time it goes round its main loop. The caller of <b>pcre2_match()</b> can set a
  185. limit on this counter, which therefore limits the amount of computing resource
  186. used for a match. The maximum depth of nested backtracking can also be limited;
  187. this indirectly restricts the amount of heap memory that is used, but there is
  188. also an explicit memory limit that can be set.
  189. </P>
  190. <P>
  191. These facilities are provided to catch runaway matches that are provoked by
  192. patterns with huge matching trees. A common example is a pattern with nested
  193. unlimited repeats applied to a long string that does not match. When one of
  194. these limits is reached, <b>pcre2_match()</b> gives an error return. The limits
  195. can also be set by items at the start of the pattern of the form
  196. <pre>
  197. (*LIMIT_HEAP=d)
  198. (*LIMIT_MATCH=d)
  199. (*LIMIT_DEPTH=d)
  200. </pre>
  201. where d is any number of decimal digits. However, the value of the setting must
  202. be less than the value set (or defaulted) by the caller of <b>pcre2_match()</b>
  203. for it to have any effect. In other words, the pattern writer can lower the
  204. limits set by the programmer, but not raise them. If there is more than one
  205. setting of one of these limits, the lower value is used. The heap limit is
  206. specified in kibibytes (units of 1024 bytes).
  207. </P>
  208. <P>
  209. Prior to release 10.30, LIMIT_DEPTH was called LIMIT_RECURSION. This name is
  210. still recognized for backwards compatibility.
  211. </P>
  212. <P>
  213. The heap limit applies only when the <b>pcre2_match()</b> or
  214. <b>pcre2_dfa_match()</b> interpreters are used for matching. It does not apply
  215. to JIT. The match limit is used (but in a different way) when JIT is being
  216. used, or when <b>pcre2_dfa_match()</b> is called, to limit computing resource
  217. usage by those matching functions. The depth limit is ignored by JIT but is
  218. relevant for DFA matching, which uses function recursion for recursions within
  219. the pattern and for lookaround assertions and atomic groups. In this case, the
  220. depth limit controls the depth of such recursion.
  221. <a name="newlines"></a></P>
  222. <br><b>
  223. Newline conventions
  224. </b><br>
  225. <P>
  226. PCRE2 supports six different conventions for indicating line breaks in
  227. strings: a single CR (carriage return) character, a single LF (linefeed)
  228. character, the two-character sequence CRLF, any of the three preceding, any
  229. Unicode newline sequence, or the NUL character (binary zero). The
  230. <a href="pcre2api.html"><b>pcre2api</b></a>
  231. page has
  232. <a href="pcre2api.html#newlines">further discussion</a>
  233. about newlines, and shows how to set the newline convention when calling
  234. <b>pcre2_compile()</b>.
  235. </P>
  236. <P>
  237. It is also possible to specify a newline convention by starting a pattern
  238. string with one of the following sequences:
  239. <pre>
  240. (*CR) carriage return
  241. (*LF) linefeed
  242. (*CRLF) carriage return, followed by linefeed
  243. (*ANYCRLF) any of the three above
  244. (*ANY) all Unicode newline sequences
  245. (*NUL) the NUL character (binary zero)
  246. </pre>
  247. These override the default and the options given to the compiling function. For
  248. example, on a Unix system where LF is the default newline sequence, the pattern
  249. <pre>
  250. (*CR)a.b
  251. </pre>
  252. changes the convention to CR. That pattern matches "a\nb" because LF is no
  253. longer a newline. If more than one of these settings is present, the last one
  254. is used.
  255. </P>
  256. <P>
  257. The newline convention affects where the circumflex and dollar assertions are
  258. true. It also affects the interpretation of the dot metacharacter when
  259. PCRE2_DOTALL is not set, and the behaviour of \N when not followed by an
  260. opening brace. However, it does not affect what the \R escape sequence
  261. matches. By default, this is any Unicode newline sequence, for Perl
  262. compatibility. However, this can be changed; see the next section and the
  263. description of \R in the section entitled
  264. <a href="#newlineseq">"Newline sequences"</a>
  265. below. A change of \R setting can be combined with a change of newline
  266. convention.
  267. </P>
  268. <br><b>
  269. Specifying what \R matches
  270. </b><br>
  271. <P>
  272. It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
  273. complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
  274. at compile time. This effect can also be achieved by starting a pattern with
  275. (*BSR_ANYCRLF). For completeness, (*BSR_UNICODE) is also recognized,
  276. corresponding to PCRE2_BSR_UNICODE.
  277. </P>
  278. <br><a name="SEC3" href="#TOC1">EBCDIC CHARACTER CODES</a><br>
  279. <P>
  280. PCRE2 can be compiled to run in an environment that uses EBCDIC as its
  281. character code instead of ASCII or Unicode (typically a mainframe system). In
  282. the sections below, character code values are ASCII or Unicode; in an EBCDIC
  283. environment these characters may have different code values, and there are no
  284. code points greater than 255.
  285. </P>
  286. <br><a name="SEC4" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
  287. <P>
  288. A regular expression is a pattern that is matched against a subject string from
  289. left to right. Most characters stand for themselves in a pattern, and match the
  290. corresponding characters in the subject. As a trivial example, the pattern
  291. <pre>
  292. The quick brown fox
  293. </pre>
  294. matches a portion of a subject string that is identical to itself. When
  295. caseless matching is specified (the PCRE2_CASELESS option or (?i) within the
  296. pattern), letters are matched independently of case. Note that there are two
  297. ASCII characters, K and S, that, in addition to their lower case ASCII
  298. equivalents, are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F
  299. (long S) respectively when either PCRE2_UTF or PCRE2_UCP is set, unless the
  300. PCRE2_EXTRA_CASELESS_RESTRICT option is in force (either passed to
  301. <b>pcre2_compile()</b> or set by (?r) within the pattern).
  302. </P>
  303. <P>
  304. The power of regular expressions comes from the ability to include wild cards,
  305. character classes, alternatives, and repetitions in the pattern. These are
  306. encoded in the pattern by the use of <i>metacharacters</i>, which do not stand
  307. for themselves but instead are interpreted in some special way.
  308. </P>
  309. <P>
  310. There are two different sets of metacharacters: those that are recognized
  311. anywhere in the pattern except within square brackets, and those that are
  312. recognized within square brackets. Outside square brackets, the metacharacters
  313. are as follows:
  314. <pre>
  315. \ general escape character with several uses
  316. ^ assert start of string (or line, in multiline mode)
  317. $ assert end of string (or line, in multiline mode)
  318. . match any character except newline (by default)
  319. [ start character class definition
  320. | start of alternative branch
  321. ( start group or control verb
  322. ) end group or control verb
  323. * 0 or more quantifier
  324. + 1 or more quantifier; also "possessive quantifier"
  325. ? 0 or 1 quantifier; also quantifier minimizer
  326. { potential start of min/max quantifier
  327. </pre>
  328. Brace characters { and } are also used to enclose data for constructions such
  329. as \g{2} or \k{name}. In almost all uses of braces, space and/or horizontal
  330. tab characters that follow { or precede } are allowed and are ignored. In the
  331. case of quantifiers, they may also appear before or after the comma. The
  332. exception to this is \u{...} which is an ECMAScript compatibility feature
  333. that is recognized only when the PCRE2_EXTRA_ALT_BSUX option is set. ECMAScript
  334. does not ignore such white space; it causes the item to be interpreted as
  335. literal.
  336. </P>
  337. <P>
  338. Part of a pattern that is in square brackets is called a "character class". In
  339. a character class the only metacharacters are:
  340. <pre>
  341. \ general escape character
  342. ^ negate the class, but only if the first character
  343. - indicates character range
  344. [ POSIX character class (if followed by POSIX syntax)
  345. ] terminates the character class
  346. </pre>
  347. If a pattern is compiled with the PCRE2_EXTENDED option, most white space in
  348. the pattern, other than in a character class, within a \Q...\E sequence, or
  349. between a # outside a character class and the next newline, inclusive, are
  350. ignored. An escaping backslash can be used to include a white space or a #
  351. character as part of the pattern. If the PCRE2_EXTENDED_MORE option is set, the
  352. same applies, but in addition unescaped space and horizontal tab characters are
  353. ignored inside a character class. Note: only these two characters are ignored,
  354. not the full set of pattern white space characters that are ignored outside a
  355. character class. Option settings can be changed within a pattern; see the
  356. section entitled
  357. <a href="#internaloptions">"Internal Option Setting"</a>
  358. below.
  359. </P>
  360. <P>
  361. The following sections describe the use of each of the metacharacters.
  362. </P>
  363. <br><a name="SEC5" href="#TOC1">BACKSLASH</a><br>
  364. <P>
  365. The backslash character has several uses. Firstly, if it is followed by a
  366. character that is not a digit or a letter, it takes away any special meaning
  367. that character may have. This use of backslash as an escape character applies
  368. both inside and outside character classes.
  369. </P>
  370. <P>
  371. For example, if you want to match a * character, you must write \* in the
  372. pattern. This escaping action applies whether or not the following character
  373. would otherwise be interpreted as a metacharacter, so it is always safe to
  374. precede a non-alphanumeric with backslash to specify that it stands for itself.
  375. In particular, if you want to match a backslash, you write \\.
  376. </P>
  377. <P>
  378. Only ASCII digits and letters have any special meaning after a backslash. All
  379. other characters (in particular, those whose code points are greater than 127)
  380. are treated as literals.
  381. </P>
  382. <P>
  383. If you want to treat all characters in a sequence as literals, you can do so by
  384. putting them between \Q and \E. Note that this includes white space even when
  385. the PCRE2_EXTENDED option is set so that most other white space is ignored. The
  386. behaviour is different from Perl in that $ and @ are handled as literals in
  387. \Q...\E sequences in PCRE2, whereas in Perl, $ and @ cause variable
  388. interpolation. Also, Perl does "double-quotish backslash interpolation" on any
  389. backslashes between \Q and \E which, its documentation says, "may lead to
  390. confusing results". PCRE2 treats a backslash between \Q and \E just like any
  391. other character. Note the following examples:
  392. <pre>
  393. Pattern PCRE2 matches Perl matches
  394. \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
  395. \Qabc\$xyz\E abc\$xyz abc\$xyz
  396. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  397. \QA\B\E A\B A\B
  398. \Q\\E \ \\E
  399. </pre>
  400. The \Q...\E sequence is recognized both inside and outside character classes.
  401. An isolated \E that is not preceded by \Q is ignored. If \Q is not followed
  402. by \E later in the pattern, the literal interpretation continues to the end of
  403. the pattern (that is, \E is assumed at the end). If the isolated \Q is inside
  404. a character class, this causes an error, because the character class is then
  405. not terminated by a closing square bracket.
  406. <a name="digitsafterbackslash"></a></P>
  407. <br><b>
  408. Non-printing characters
  409. </b><br>
  410. <P>
  411. A second use of backslash provides a way of encoding non-printing characters
  412. in patterns in a visible manner. There is no restriction on the appearance of
  413. non-printing characters in a pattern, but when a pattern is being prepared by
  414. text editing, it is often easier to use one of the following escape sequences
  415. instead of the binary character it represents. In an ASCII or Unicode
  416. environment, these escapes are as follows:
  417. <pre>
  418. \a alarm, that is, the BEL character (hex 07)
  419. \cx "control-x", where x is a non-control ASCII character
  420. \e escape (hex 1B)
  421. \f form feed (hex 0C)
  422. \n linefeed (hex 0A)
  423. \r carriage return (hex 0D) (but see below)
  424. \t tab (hex 09)
  425. \0dd character with octal code 0dd
  426. \ddd character with octal code ddd, or backreference
  427. \o{ddd..} character with octal code ddd..
  428. \xhh character with hex code hh
  429. \x{hhh..} character with hex code hhh..
  430. \N{U+hhh..} character with Unicode hex code point hhh..
  431. </pre>
  432. By default, after \x that is not followed by {, from zero to two hexadecimal
  433. digits are read (letters can be in upper or lower case). Any number of
  434. hexadecimal digits may appear between \x{ and }. If a character other than a
  435. hexadecimal digit appears between \x{ and }, or if there is no terminating },
  436. an error occurs.
  437. </P>
  438. <P>
  439. Characters whose code points are less than 256 can be defined by either of the
  440. two syntaxes for \x or by an octal sequence. There is no difference in the way
  441. they are handled. For example, \xdc is exactly the same as \x{dc} or \334.
  442. However, using the braced versions does make such sequences easier to read.
  443. </P>
  444. <P>
  445. Support is available for some ECMAScript (aka JavaScript) escape sequences via
  446. two compile-time options. If PCRE2_ALT_BSUX is set, the sequence \x followed
  447. by { is not recognized. Only if \x is followed by two hexadecimal digits is it
  448. recognized as a character escape. Otherwise it is interpreted as a literal "x"
  449. character. In this mode, support for code points greater than 256 is provided
  450. by \u, which must be followed by four hexadecimal digits; otherwise it is
  451. interpreted as a literal "u" character.
  452. </P>
  453. <P>
  454. PCRE2_EXTRA_ALT_BSUX has the same effect as PCRE2_ALT_BSUX and, in addition,
  455. \u{hhh..} is recognized as the character specified by hexadecimal code point.
  456. There may be any number of hexadecimal digits, but unlike other places that
  457. also use curly brackets, spaces are not allowed and would result in the string
  458. being interpreted as a literal. This syntax is from ECMAScript 6.
  459. </P>
  460. <P>
  461. The \N{U+hhh..} escape sequence is recognized only when PCRE2 is operating in
  462. UTF mode. Perl also uses \N{name} to specify characters by Unicode name; PCRE2
  463. does not support this. Note that when \N is not followed by an opening brace
  464. (curly bracket) it has an entirely different meaning, matching any character
  465. that is not a newline.
  466. </P>
  467. <P>
  468. There are some legacy applications where the escape sequence \r is expected to
  469. match a newline. If the PCRE2_EXTRA_ESCAPED_CR_IS_LF option is set, \r in a
  470. pattern is converted to \n so that it matches a LF (linefeed) instead of a CR
  471. (carriage return) character.
  472. </P>
  473. <P>
  474. An error occurs if \c is not followed by a character whose ASCII code point
  475. is in the range 32 to 126. The precise effect of \cx is as follows: if x is a
  476. lower case letter, it is converted to upper case. Then bit 6 of the character
  477. (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A (A is 41, Z is
  478. 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes hex 7B (; is 3B). If
  479. the code unit following \c has a code point less than 32 or greater than 126,
  480. a compile-time error occurs.
  481. </P>
  482. <P>
  483. When PCRE2 is compiled in EBCDIC mode, \N{U+hhh..} is not supported. \a, \e,
  484. \f, \n, \r, and \t generate the appropriate EBCDIC code values. The \c
  485. escape is processed as specified for Perl in the <b>perlebcdic</b> document. The
  486. only characters that are allowed after \c are A-Z, a-z, or one of @, [, \, ],
  487. ^, _, or ?. Any other character provokes a compile-time error. The sequence
  488. \c@ encodes character code 0; after \c the letters (in either case) encode
  489. characters 1-26 (hex 01 to hex 1A); [, \, ], ^, and _ encode characters 27-31
  490. (hex 1B to hex 1F), and \c? becomes either 255 (hex FF) or 95 (hex 5F).
  491. </P>
  492. <P>
  493. Thus, apart from \c?, these escapes generate the same character code values as
  494. they do in an ASCII environment, though the meanings of the values mostly
  495. differ. For example, \cG always generates code value 7, which is BEL in ASCII
  496. but DEL in EBCDIC.
  497. </P>
  498. <P>
  499. The sequence \c? generates DEL (127, hex 7F) in an ASCII environment, but
  500. because 127 is not a control character in EBCDIC, Perl makes it generate the
  501. APC character. Unfortunately, there are several variants of EBCDIC. In most of
  502. them the APC character has the value 255 (hex FF), but in the one Perl calls
  503. POSIX-BC its value is 95 (hex 5F). If certain other characters have POSIX-BC
  504. values, PCRE2 makes \c? generate 95; otherwise it generates 255.
  505. </P>
  506. <P>
  507. After \0 up to two further octal digits are read. If there are fewer than two
  508. digits, just those that are present are used. Thus the sequence \0\x\015
  509. specifies two binary zeros followed by a CR character (code value 13). Make
  510. sure you supply two digits after the initial zero if the pattern character that
  511. follows is itself an octal digit.
  512. </P>
  513. <P>
  514. The escape \o must be followed by a sequence of octal digits, enclosed in
  515. braces. An error occurs if this is not the case. This escape is a recent
  516. addition to Perl; it provides way of specifying character code points as octal
  517. numbers greater than 0777, and it also allows octal numbers and backreferences
  518. to be unambiguously specified.
  519. </P>
  520. <P>
  521. For greater clarity and unambiguity, it is best to avoid following \ by a
  522. digit greater than zero. Instead, use \o{...} or \x{...} to specify numerical
  523. character code points, and \g{...} to specify backreferences. The following
  524. paragraphs describe the old, ambiguous syntax.
  525. </P>
  526. <P>
  527. The handling of a backslash followed by a digit other than 0 is complicated,
  528. and Perl has changed over time, causing PCRE2 also to change.
  529. </P>
  530. <P>
  531. Outside a character class, PCRE2 reads the digit and any following digits as a
  532. decimal number. If the number is less than 10, begins with the digit 8 or 9, or
  533. if there are at least that many previous capture groups in the expression, the
  534. entire sequence is taken as a <i>backreference</i>. A description of how this
  535. works is given
  536. <a href="#backreferences">later,</a>
  537. following the discussion of
  538. <a href="#group">parenthesized groups.</a>
  539. Otherwise, up to three octal digits are read to form a character code.
  540. </P>
  541. <P>
  542. Inside a character class, PCRE2 handles \8 and \9 as the literal characters
  543. "8" and "9", and otherwise reads up to three octal digits following the
  544. backslash, using them to generate a data character. Any subsequent digits stand
  545. for themselves. For example, outside a character class:
  546. <pre>
  547. \040 is another way of writing an ASCII space
  548. \40 is the same, provided there are fewer than 40 previous capture groups
  549. \7 is always a backreference
  550. \11 might be a backreference, or another way of writing a tab
  551. \011 is always a tab
  552. \0113 is a tab followed by the character "3"
  553. \113 might be a backreference, otherwise the character with octal code 113
  554. \377 might be a backreference, otherwise the value 255 (decimal)
  555. \81 is always a backreference
  556. </pre>
  557. Note that octal values of 100 or greater that are specified using this syntax
  558. must not be introduced by a leading zero, because no more than three octal
  559. digits are ever read.
  560. </P>
  561. <br><b>
  562. Constraints on character values
  563. </b><br>
  564. <P>
  565. Characters that are specified using octal or hexadecimal numbers are
  566. limited to certain values, as follows:
  567. <pre>
  568. 8-bit non-UTF mode no greater than 0xff
  569. 16-bit non-UTF mode no greater than 0xffff
  570. 32-bit non-UTF mode no greater than 0xffffffff
  571. All UTF modes no greater than 0x10ffff and a valid code point
  572. </pre>
  573. Invalid Unicode code points are all those in the range 0xd800 to 0xdfff (the
  574. so-called "surrogate" code points). The check for these can be disabled by the
  575. caller of <b>pcre2_compile()</b> by setting the option
  576. PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES. However, this is possible only in UTF-8
  577. and UTF-32 modes, because these values are not representable in UTF-16.
  578. </P>
  579. <br><b>
  580. Escape sequences in character classes
  581. </b><br>
  582. <P>
  583. All the sequences that define a single character value can be used both inside
  584. and outside character classes. In addition, inside a character class, \b is
  585. interpreted as the backspace character (hex 08).
  586. </P>
  587. <P>
  588. When not followed by an opening brace, \N is not allowed in a character class.
  589. \B, \R, and \X are not special inside a character class. Like other
  590. unrecognized alphabetic escape sequences, they cause an error. Outside a
  591. character class, these sequences have different meanings.
  592. </P>
  593. <br><b>
  594. Unsupported escape sequences
  595. </b><br>
  596. <P>
  597. In Perl, the sequences \F, \l, \L, \u, and \U are recognized by its string
  598. handler and used to modify the case of following characters. By default, PCRE2
  599. does not support these escape sequences in patterns. However, if either of the
  600. PCRE2_ALT_BSUX or PCRE2_EXTRA_ALT_BSUX options is set, \U matches a "U"
  601. character, and \u can be used to define a character by code point, as
  602. described above.
  603. </P>
  604. <br><b>
  605. Absolute and relative backreferences
  606. </b><br>
  607. <P>
  608. The sequence \g followed by a signed or unsigned number, optionally enclosed
  609. in braces, is an absolute or relative backreference. A named backreference
  610. can be coded as \g{name}. Backreferences are discussed
  611. <a href="#backreferences">later,</a>
  612. following the discussion of
  613. <a href="#group">parenthesized groups.</a>
  614. </P>
  615. <br><b>
  616. Absolute and relative subroutine calls
  617. </b><br>
  618. <P>
  619. For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
  620. a number enclosed either in angle brackets or single quotes, is an alternative
  621. syntax for referencing a capture group as a subroutine. Details are discussed
  622. <a href="#onigurumasubroutines">later.</a>
  623. Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
  624. synonymous. The former is a backreference; the latter is a
  625. <a href="#groupsassubroutines">subroutine</a>
  626. call.
  627. <a name="genericchartypes"></a></P>
  628. <br><b>
  629. Generic character types
  630. </b><br>
  631. <P>
  632. Another use of backslash is for specifying generic character types:
  633. <pre>
  634. \d any decimal digit
  635. \D any character that is not a decimal digit
  636. \h any horizontal white space character
  637. \H any character that is not a horizontal white space character
  638. \N any character that is not a newline
  639. \s any white space character
  640. \S any character that is not a white space character
  641. \v any vertical white space character
  642. \V any character that is not a vertical white space character
  643. \w any "word" character
  644. \W any "non-word" character
  645. </pre>
  646. The \N escape sequence has the same meaning as
  647. <a href="#fullstopdot">the "." metacharacter</a>
  648. when PCRE2_DOTALL is not set, but setting PCRE2_DOTALL does not change the
  649. meaning of \N. Note that when \N is followed by an opening brace it has a
  650. different meaning. See the section entitled
  651. <a href="#digitsafterbackslash">"Non-printing characters"</a>
  652. above for details. Perl also uses \N{name} to specify characters by Unicode
  653. name; PCRE2 does not support this.
  654. </P>
  655. <P>
  656. Each pair of lower and upper case escape sequences partitions the complete set
  657. of characters into two disjoint sets. Any given character matches one, and only
  658. one, of each pair. The sequences can appear both inside and outside character
  659. classes. They each match one character of the appropriate type. If the current
  660. matching point is at the end of the subject string, all of them fail, because
  661. there is no character to match.
  662. </P>
  663. <P>
  664. The default \s characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
  665. space (32), which are defined as white space in the "C" locale. This list may
  666. vary if locale-specific matching is taking place. For example, in some locales
  667. the "non-breaking space" character (\xA0) is recognized as white space, and in
  668. others the VT character is not.
  669. </P>
  670. <P>
  671. A "word" character is an underscore or any character that is a letter or digit.
  672. By default, the definition of letters and digits is controlled by PCRE2's
  673. low-valued character tables, and may vary if locale-specific matching is taking
  674. place (see
  675. <a href="pcre2api.html#localesupport">"Locale support"</a>
  676. in the
  677. <a href="pcre2api.html"><b>pcre2api</b></a>
  678. page). For example, in a French locale such as "fr_FR" in Unix-like systems,
  679. or "french" in Windows, some character codes greater than 127 are used for
  680. accented letters, and these are then matched by \w. The use of locales with
  681. Unicode is discouraged.
  682. </P>
  683. <P>
  684. By default, characters whose code points are greater than 127 never match \d,
  685. \s, or \w, and always match \D, \S, and \W, although this may be different
  686. for characters in the range 128-255 when locale-specific matching is happening.
  687. These escape sequences retain their original meanings from before Unicode
  688. support was available, mainly for efficiency reasons. If the PCRE2_UCP option
  689. is set, the behaviour is changed so that Unicode properties are used to
  690. determine character types, as follows:
  691. <pre>
  692. \d any character that matches \p{Nd} (decimal digit)
  693. \s any character that matches \p{Z} or \h or \v
  694. \w any character that matches \p{L}, \p{N}, \p{Mn}, or \p{Pc}
  695. </pre>
  696. The addition of \p{Mn} (non-spacing mark) and the replacement of an explicit
  697. test for underscore with a test for \p{Pc} (connector punctuation) happened in
  698. PCRE2 release 10.43. This brings PCRE2 into line with Perl.
  699. </P>
  700. <P>
  701. The upper case escapes match the inverse sets of characters. Note that \d
  702. matches only decimal digits, whereas \w matches any Unicode digit, as well as
  703. other character categories. Note also that PCRE2_UCP affects \b, and
  704. \B because they are defined in terms of \w and \W. Matching these sequences
  705. is noticeably slower when PCRE2_UCP is set.
  706. </P>
  707. <P>
  708. The effect of PCRE2_UCP on any one of these escape sequences can be negated by
  709. the options PCRE2_EXTRA_ASCII_BSD, PCRE2_EXTRA_ASCII_BSS, and
  710. PCRE2_EXTRA_ASCII_BSW, respectively. These options can be set and reset within
  711. a pattern by means of an internal option setting
  712. <a href="#internaloptions">(see below).</a>
  713. </P>
  714. <P>
  715. The sequences \h, \H, \v, and \V, in contrast to the other sequences, which
  716. match only ASCII characters by default, always match a specific list of code
  717. points, whether or not PCRE2_UCP is set. The horizontal space characters are:
  718. <pre>
  719. U+0009 Horizontal tab (HT)
  720. U+0020 Space
  721. U+00A0 Non-break space
  722. U+1680 Ogham space mark
  723. U+180E Mongolian vowel separator
  724. U+2000 En quad
  725. U+2001 Em quad
  726. U+2002 En space
  727. U+2003 Em space
  728. U+2004 Three-per-em space
  729. U+2005 Four-per-em space
  730. U+2006 Six-per-em space
  731. U+2007 Figure space
  732. U+2008 Punctuation space
  733. U+2009 Thin space
  734. U+200A Hair space
  735. U+202F Narrow no-break space
  736. U+205F Medium mathematical space
  737. U+3000 Ideographic space
  738. </pre>
  739. The vertical space characters are:
  740. <pre>
  741. U+000A Linefeed (LF)
  742. U+000B Vertical tab (VT)
  743. U+000C Form feed (FF)
  744. U+000D Carriage return (CR)
  745. U+0085 Next line (NEL)
  746. U+2028 Line separator
  747. U+2029 Paragraph separator
  748. </pre>
  749. In 8-bit, non-UTF-8 mode, only the characters with code points less than 256
  750. are relevant.
  751. <a name="newlineseq"></a></P>
  752. <br><b>
  753. Newline sequences
  754. </b><br>
  755. <P>
  756. Outside a character class, by default, the escape sequence \R matches any
  757. Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent to the
  758. following:
  759. <pre>
  760. (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
  761. </pre>
  762. This is an example of an "atomic group", details of which are given
  763. <a href="#atomicgroup">below.</a>
  764. This particular group matches either the two-character sequence CR followed by
  765. LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
  766. U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
  767. line, U+0085). Because this is an atomic group, the two-character sequence is
  768. treated as a single unit that cannot be split.
  769. </P>
  770. <P>
  771. In other modes, two additional characters whose code points are greater than 255
  772. are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
  773. Unicode support is not needed for these characters to be recognized.
  774. </P>
  775. <P>
  776. It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
  777. complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
  778. at compile time. (BSR is an abbreviation for "backslash R".) This can be made
  779. the default when PCRE2 is built; if this is the case, the other behaviour can
  780. be requested via the PCRE2_BSR_UNICODE option. It is also possible to specify
  781. these settings by starting a pattern string with one of the following
  782. sequences:
  783. <pre>
  784. (*BSR_ANYCRLF) CR, LF, or CRLF only
  785. (*BSR_UNICODE) any Unicode newline sequence
  786. </pre>
  787. These override the default and the options given to the compiling function.
  788. Note that these special settings, which are not Perl-compatible, are recognized
  789. only at the very start of a pattern, and that they must be in upper case. If
  790. more than one of them is present, the last one is used. They can be combined
  791. with a change of newline convention; for example, a pattern can start with:
  792. <pre>
  793. (*ANY)(*BSR_ANYCRLF)
  794. </pre>
  795. They can also be combined with the (*UTF) or (*UCP) special sequences. Inside a
  796. character class, \R is treated as an unrecognized escape sequence, and causes
  797. an error.
  798. <a name="uniextseq"></a></P>
  799. <br><b>
  800. Unicode character properties
  801. </b><br>
  802. <P>
  803. When PCRE2 is built with Unicode support (the default), three additional escape
  804. sequences that match characters with specific properties are available. They
  805. can be used in any mode, though in 8-bit and 16-bit non-UTF modes these
  806. sequences are of course limited to testing characters whose code points are
  807. less than U+0100 and U+10000, respectively. In 32-bit non-UTF mode, code points
  808. greater than 0x10ffff (the Unicode limit) may be encountered. These are all
  809. treated as being in the Unknown script and with an unassigned type.
  810. </P>
  811. <P>
  812. Matching characters by Unicode property is not fast, because PCRE2 has to do a
  813. multistage table lookup in order to find a character's property. That is why
  814. the traditional escape sequences such as \d and \w do not use Unicode
  815. properties in PCRE2 by default, though you can make them do so by setting the
  816. PCRE2_UCP option or by starting the pattern with (*UCP).
  817. </P>
  818. <P>
  819. The extra escape sequences that provide property support are:
  820. <pre>
  821. \p{<i>xx</i>} a character with the <i>xx</i> property
  822. \P{<i>xx</i>} a character without the <i>xx</i> property
  823. \X a Unicode extended grapheme cluster
  824. </pre>
  825. The property names represented by <i>xx</i> above are not case-sensitive, and in
  826. accordance with Unicode's "loose matching" rules, spaces, hyphens, and
  827. underscores are ignored. There is support for Unicode script names, Unicode
  828. general category properties, "Any", which matches any character (including
  829. newline), Bidi_Class, a number of binary (yes/no) properties, and some special
  830. PCRE2 properties (described
  831. <a href="#extraprops">below).</a>
  832. Certain other Perl properties such as "InMusicalSymbols" are not supported by
  833. PCRE2. Note that \P{Any} does not match any characters, so always causes a
  834. match failure.
  835. </P>
  836. <br><b>
  837. Script properties for \p and \P
  838. </b><br>
  839. <P>
  840. There are three different syntax forms for matching a script. Each Unicode
  841. character has a basic script and, optionally, a list of other scripts ("Script
  842. Extensions") with which it is commonly used. Using the Adlam script as an
  843. example, \p{sc:Adlam} matches characters whose basic script is Adlam, whereas
  844. \p{scx:Adlam} matches, in addition, characters that have Adlam in their
  845. extensions list. The full names "script" and "script extensions" for the
  846. property types are recognized, and a equals sign is an alternative to the
  847. colon. If a script name is given without a property type, for example,
  848. \p{Adlam}, it is treated as \p{scx:Adlam}. Perl changed to this
  849. interpretation at release 5.26 and PCRE2 changed at release 10.40.
  850. </P>
  851. <P>
  852. Unassigned characters (and in non-UTF 32-bit mode, characters with code points
  853. greater than 0x10FFFF) are assigned the "Unknown" script. Others that are not
  854. part of an identified script are lumped together as "Common". The current list
  855. of recognized script names and their 4-character abbreviations can be obtained
  856. by running this command:
  857. <pre>
  858. pcre2test -LS
  859. </PRE>
  860. </P>
  861. <br><b>
  862. The general category property for \p and \P
  863. </b><br>
  864. <P>
  865. Each character has exactly one Unicode general category property, specified by
  866. a two-letter abbreviation. For compatibility with Perl, negation can be
  867. specified by including a circumflex between the opening brace and the property
  868. name. For example, \p{^Lu} is the same as \P{Lu}.
  869. </P>
  870. <P>
  871. If only one letter is specified with \p or \P, it includes all the general
  872. category properties that start with that letter. In this case, in the absence
  873. of negation, the curly brackets in the escape sequence are optional; these two
  874. examples have the same effect:
  875. <pre>
  876. \p{L}
  877. \pL
  878. </pre>
  879. The following general category property codes are supported:
  880. <pre>
  881. C Other
  882. Cc Control
  883. Cf Format
  884. Cn Unassigned
  885. Co Private use
  886. Cs Surrogate
  887. L Letter
  888. Ll Lower case letter
  889. Lm Modifier letter
  890. Lo Other letter
  891. Lt Title case letter
  892. Lu Upper case letter
  893. M Mark
  894. Mc Spacing mark
  895. Me Enclosing mark
  896. Mn Non-spacing mark
  897. N Number
  898. Nd Decimal number
  899. Nl Letter number
  900. No Other number
  901. P Punctuation
  902. Pc Connector punctuation
  903. Pd Dash punctuation
  904. Pe Close punctuation
  905. Pf Final punctuation
  906. Pi Initial punctuation
  907. Po Other punctuation
  908. Ps Open punctuation
  909. S Symbol
  910. Sc Currency symbol
  911. Sk Modifier symbol
  912. Sm Mathematical symbol
  913. So Other symbol
  914. Z Separator
  915. Zl Line separator
  916. Zp Paragraph separator
  917. Zs Space separator
  918. </pre>
  919. The special property LC, which has the synonym L&, is also supported: it
  920. matches a character that has the Lu, Ll, or Lt property, in other words, a
  921. letter that is not classified as a modifier or "other".
  922. </P>
  923. <P>
  924. The Cs (Surrogate) property applies only to characters whose code points are in
  925. the range U+D800 to U+DFFF. These characters are no different to any other
  926. character when PCRE2 is not in UTF mode (using the 16-bit or 32-bit library).
  927. However, they are not valid in Unicode strings and so cannot be tested by PCRE2
  928. in UTF mode, unless UTF validity checking has been turned off (see the
  929. discussion of PCRE2_NO_UTF_CHECK in the
  930. <a href="pcre2api.html"><b>pcre2api</b></a>
  931. page).
  932. </P>
  933. <P>
  934. The long synonyms for property names that Perl supports (such as \p{Letter})
  935. are not supported by PCRE2, nor is it permitted to prefix any of these
  936. properties with "Is".
  937. </P>
  938. <P>
  939. No character that is in the Unicode table has the Cn (unassigned) property.
  940. Instead, this property is assumed for any code point that is not in the
  941. Unicode table.
  942. </P>
  943. <P>
  944. Specifying caseless matching does not affect these escape sequences. For
  945. example, \p{Lu} always matches only upper case letters. This is different from
  946. the behaviour of current versions of Perl.
  947. </P>
  948. <br><b>
  949. Binary (yes/no) properties for \p and \P
  950. </b><br>
  951. <P>
  952. Unicode defines a number of binary properties, that is, properties whose only
  953. values are true or false. You can obtain a list of those that are recognized by
  954. \p and \P, along with their abbreviations, by running this command:
  955. <pre>
  956. pcre2test -LP
  957. </PRE>
  958. </P>
  959. <br><b>
  960. The Bidi_Class property for \p and \P
  961. </b><br>
  962. <P>
  963. <pre>
  964. \p{Bidi_Class:&#60;class&#62;} matches a character with the given class
  965. \p{BC:&#60;class&#62;} matches a character with the given class
  966. </pre>
  967. The recognized classes are:
  968. <pre>
  969. AL Arabic letter
  970. AN Arabic number
  971. B paragraph separator
  972. BN boundary neutral
  973. CS common separator
  974. EN European number
  975. ES European separator
  976. ET European terminator
  977. FSI first strong isolate
  978. L left-to-right
  979. LRE left-to-right embedding
  980. LRI left-to-right isolate
  981. LRO left-to-right override
  982. NSM non-spacing mark
  983. ON other neutral
  984. PDF pop directional format
  985. PDI pop directional isolate
  986. R right-to-left
  987. RLE right-to-left embedding
  988. RLI right-to-left isolate
  989. RLO right-to-left override
  990. S segment separator
  991. WS which space
  992. </pre>
  993. An equals sign may be used instead of a colon. The class names are
  994. case-insensitive; only the short names listed above are recognized.
  995. </P>
  996. <br><b>
  997. Extended grapheme clusters
  998. </b><br>
  999. <P>
  1000. The \X escape matches any number of Unicode characters that form an "extended
  1001. grapheme cluster", and treats the sequence as an atomic group
  1002. <a href="#atomicgroup">(see below).</a>
  1003. Unicode supports various kinds of composite character by giving each character
  1004. a grapheme breaking property, and having rules that use these properties to
  1005. define the boundaries of extended grapheme clusters. The rules are defined in
  1006. Unicode Standard Annex 29, "Unicode Text Segmentation". Unicode 11.0.0
  1007. abandoned the use of some previous properties that had been used for emojis.
  1008. Instead it introduced various emoji-specific properties. PCRE2 uses only the
  1009. Extended Pictographic property.
  1010. </P>
  1011. <P>
  1012. \X always matches at least one character. Then it decides whether to add
  1013. additional characters according to the following rules for ending a cluster:
  1014. </P>
  1015. <P>
  1016. 1. End at the end of the subject string.
  1017. </P>
  1018. <P>
  1019. 2. Do not end between CR and LF; otherwise end after any control character.
  1020. </P>
  1021. <P>
  1022. 3. Do not break Hangul (a Korean script) syllable sequences. Hangul characters
  1023. are of five types: L, V, T, LV, and LVT. An L character may be followed by an
  1024. L, V, LV, or LVT character; an LV or V character may be followed by a V or T
  1025. character; an LVT or T character may be followed only by a T character.
  1026. </P>
  1027. <P>
  1028. 4. Do not end before extending characters or spacing marks or the zero-width
  1029. joiner (ZWJ) character. Characters with the "mark" property always have the
  1030. "extend" grapheme breaking property.
  1031. </P>
  1032. <P>
  1033. 5. Do not end after prepend characters.
  1034. </P>
  1035. <P>
  1036. 6. Do not end within emoji modifier sequences or emoji ZWJ (zero-width
  1037. joiner) sequences. An emoji ZWJ sequence consists of a character with the
  1038. Extended_Pictographic property, optionally followed by one or more characters
  1039. with the Extend property, followed by the ZWJ character, followed by another
  1040. Extended_Pictographic character.
  1041. </P>
  1042. <P>
  1043. 7. Do not break within emoji flag sequences. That is, do not break between
  1044. regional indicator (RI) characters if there are an odd number of RI characters
  1045. before the break point.
  1046. </P>
  1047. <P>
  1048. 8. Otherwise, end the cluster.
  1049. <a name="extraprops"></a></P>
  1050. <br><b>
  1051. PCRE2's additional properties
  1052. </b><br>
  1053. <P>
  1054. As well as the standard Unicode properties described above, PCRE2 supports four
  1055. more that make it possible to convert traditional escape sequences such as \w
  1056. and \s to use Unicode properties. PCRE2 uses these non-standard, non-Perl
  1057. properties internally when PCRE2_UCP is set. However, they may also be used
  1058. explicitly. These properties are:
  1059. <pre>
  1060. Xan Any alphanumeric character
  1061. Xps Any POSIX space character
  1062. Xsp Any Perl space character
  1063. Xwd Any Perl "word" character
  1064. </pre>
  1065. Xan matches characters that have either the L (letter) or the N (number)
  1066. property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
  1067. carriage return, and any other character that has the Z (separator) property.
  1068. Xsp is the same as Xps; in PCRE1 it used to exclude vertical tab, for Perl
  1069. compatibility, but Perl changed. Xwd matches the same characters as Xan, plus
  1070. those that match Mn (non-spacing mark) or Pc (connector punctuation, which
  1071. includes underscore).
  1072. </P>
  1073. <P>
  1074. There is another non-standard property, Xuc, which matches any character that
  1075. can be represented by a Universal Character Name in C++ and other programming
  1076. languages. These are the characters $, @, ` (grave accent), and all characters
  1077. with Unicode code points greater than or equal to U+00A0, except for the
  1078. surrogates U+D800 to U+DFFF. Note that most base (ASCII) characters are
  1079. excluded. (Universal Character Names are of the form \uHHHH or \UHHHHHHHH
  1080. where H is a hexadecimal digit. Note that the Xuc property does not match these
  1081. sequences but the characters that they represent.)
  1082. <a name="resetmatchstart"></a></P>
  1083. <br><b>
  1084. Resetting the match start
  1085. </b><br>
  1086. <P>
  1087. In normal use, the escape sequence \K causes any previously matched characters
  1088. not to be included in the final matched sequence that is returned. For example,
  1089. the pattern:
  1090. <pre>
  1091. foo\Kbar
  1092. </pre>
  1093. matches "foobar", but reports that it has matched "bar". \K does not interact
  1094. with anchoring in any way. The pattern:
  1095. <pre>
  1096. ^foo\Kbar
  1097. </pre>
  1098. matches only when the subject begins with "foobar" (in single line mode),
  1099. though it again reports the matched string as "bar". This feature is similar to
  1100. a lookbehind assertion
  1101. <a href="#lookbehind">(described below),</a>
  1102. but the part of the pattern that precedes \K is not constrained to match a
  1103. limited number of characters, as is required for a lookbehind assertion. The
  1104. use of \K does not interfere with the setting of
  1105. <a href="#group">captured substrings.</a>
  1106. For example, when the pattern
  1107. <pre>
  1108. (foo)\Kbar
  1109. </pre>
  1110. matches "foobar", the first substring is still set to "foo".
  1111. </P>
  1112. <P>
  1113. From version 5.32.0 Perl forbids the use of \K in lookaround assertions. From
  1114. release 10.38 PCRE2 also forbids this by default. However, the
  1115. PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK option can be used when calling
  1116. <b>pcre2_compile()</b> to re-enable the previous behaviour. When this option is
  1117. set, \K is acted upon when it occurs inside positive assertions, but is
  1118. ignored in negative assertions. Note that when a pattern such as (?=ab\K)
  1119. matches, the reported start of the match can be greater than the end of the
  1120. match. Using \K in a lookbehind assertion at the start of a pattern can also
  1121. lead to odd effects. For example, consider this pattern:
  1122. <pre>
  1123. (?&#60;=\Kfoo)bar
  1124. </pre>
  1125. If the subject is "foobar", a call to <b>pcre2_match()</b> with a starting
  1126. offset of 3 succeeds and reports the matching string as "foobar", that is, the
  1127. start of the reported match is earlier than where the match started.
  1128. <a name="smallassertions"></a></P>
  1129. <br><b>
  1130. Simple assertions
  1131. </b><br>
  1132. <P>
  1133. The final use of backslash is for certain simple assertions. An assertion
  1134. specifies a condition that has to be met at a particular point in a match,
  1135. without consuming any characters from the subject string. The use of
  1136. groups for more complicated assertions is described
  1137. <a href="#bigassertions">below.</a>
  1138. The backslashed assertions are:
  1139. <pre>
  1140. \b matches at a word boundary
  1141. \B matches when not at a word boundary
  1142. \A matches at the start of the subject
  1143. \Z matches at the end of the subject
  1144. also matches before a newline at the end of the subject
  1145. \z matches only at the end of the subject
  1146. \G matches at the first matching position in the subject
  1147. </pre>
  1148. Inside a character class, \b has a different meaning; it matches the backspace
  1149. character. If any other of these assertions appears in a character class, an
  1150. "invalid escape sequence" error is generated.
  1151. </P>
  1152. <P>
  1153. A word boundary is a position in the subject string where the current character
  1154. and the previous character do not both match \w or \W (i.e. one matches
  1155. \w and the other matches \W), or the start or end of the string if the
  1156. first or last character matches \w, respectively. When PCRE2 is built with
  1157. Unicode support, the meanings of \w and \W can be changed by setting the
  1158. PCRE2_UCP option. When this is done, it also affects \b and \B. Neither PCRE2
  1159. nor Perl has a separate "start of word" or "end of word" metasequence. However,
  1160. whatever follows \b normally determines which it is. For example, the fragment
  1161. \ba matches "a" at the start of a word.
  1162. </P>
  1163. <P>
  1164. The \A, \Z, and \z assertions differ from the traditional circumflex and
  1165. dollar (described in the next section) in that they only ever match at the very
  1166. start and end of the subject string, whatever options are set. Thus, they are
  1167. independent of multiline mode. These three assertions are not affected by the
  1168. PCRE2_NOTBOL or PCRE2_NOTEOL options, which affect only the behaviour of the
  1169. circumflex and dollar metacharacters. However, if the <i>startoffset</i>
  1170. argument of <b>pcre2_match()</b> is non-zero, indicating that matching is to
  1171. start at a point other than the beginning of the subject, \A can never match.
  1172. The difference between \Z and \z is that \Z matches before a newline at the
  1173. end of the string as well as at the very end, whereas \z matches only at the
  1174. end.
  1175. </P>
  1176. <P>
  1177. The \G assertion is true only when the current matching position is at the
  1178. start point of the matching process, as specified by the <i>startoffset</i>
  1179. argument of <b>pcre2_match()</b>. It differs from \A when the value of
  1180. <i>startoffset</i> is non-zero. By calling <b>pcre2_match()</b> multiple times
  1181. with appropriate arguments, you can mimic Perl's /g option, and it is in this
  1182. kind of implementation where \G can be useful.
  1183. </P>
  1184. <P>
  1185. Note, however, that PCRE2's implementation of \G, being true at the starting
  1186. character of the matching process, is subtly different from Perl's, which
  1187. defines it as true at the end of the previous match. In Perl, these can be
  1188. different when the previously matched string was empty. Because PCRE2 does just
  1189. one match at a time, it cannot reproduce this behaviour.
  1190. </P>
  1191. <P>
  1192. If all the alternatives of a pattern begin with \G, the expression is anchored
  1193. to the starting match position, and the "anchored" flag is set in the compiled
  1194. regular expression.
  1195. </P>
  1196. <br><a name="SEC6" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
  1197. <P>
  1198. The circumflex and dollar metacharacters are zero-width assertions. That is,
  1199. they test for a particular condition being true without consuming any
  1200. characters from the subject string. These two metacharacters are concerned with
  1201. matching the starts and ends of lines. If the newline convention is set so that
  1202. only the two-character sequence CRLF is recognized as a newline, isolated CR
  1203. and LF characters are treated as ordinary data characters, and are not
  1204. recognized as newlines.
  1205. </P>
  1206. <P>
  1207. Outside a character class, in the default matching mode, the circumflex
  1208. character is an assertion that is true only if the current matching point is at
  1209. the start of the subject string. If the <i>startoffset</i> argument of
  1210. <b>pcre2_match()</b> is non-zero, or if PCRE2_NOTBOL is set, circumflex can
  1211. never match if the PCRE2_MULTILINE option is unset. Inside a character class,
  1212. circumflex has an entirely different meaning
  1213. <a href="#characterclass">(see below).</a>
  1214. </P>
  1215. <P>
  1216. Circumflex need not be the first character of the pattern if a number of
  1217. alternatives are involved, but it should be the first thing in each alternative
  1218. in which it appears if the pattern is ever to match that branch. If all
  1219. possible alternatives start with a circumflex, that is, if the pattern is
  1220. constrained to match only at the start of the subject, it is said to be an
  1221. "anchored" pattern. (There are also other constructs that can cause a pattern
  1222. to be anchored.)
  1223. </P>
  1224. <P>
  1225. The dollar character is an assertion that is true only if the current matching
  1226. point is at the end of the subject string, or immediately before a newline at
  1227. the end of the string (by default), unless PCRE2_NOTEOL is set. Note, however,
  1228. that it does not actually match the newline. Dollar need not be the last
  1229. character of the pattern if a number of alternatives are involved, but it
  1230. should be the last item in any branch in which it appears. Dollar has no
  1231. special meaning in a character class.
  1232. </P>
  1233. <P>
  1234. The meaning of dollar can be changed so that it matches only at the very end of
  1235. the string, by setting the PCRE2_DOLLAR_ENDONLY option at compile time. This
  1236. does not affect the \Z assertion.
  1237. </P>
  1238. <P>
  1239. The meanings of the circumflex and dollar metacharacters are changed if the
  1240. PCRE2_MULTILINE option is set. When this is the case, a dollar character
  1241. matches before any newlines in the string, as well as at the very end, and a
  1242. circumflex matches immediately after internal newlines as well as at the start
  1243. of the subject string. It does not match after a newline that ends the string,
  1244. for compatibility with Perl. However, this can be changed by setting the
  1245. PCRE2_ALT_CIRCUMFLEX option.
  1246. </P>
  1247. <P>
  1248. For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
  1249. \n represents a newline) in multiline mode, but not otherwise. Consequently,
  1250. patterns that are anchored in single line mode because all branches start with
  1251. ^ are not anchored in multiline mode, and a match for circumflex is possible
  1252. when the <i>startoffset</i> argument of <b>pcre2_match()</b> is non-zero. The
  1253. PCRE2_DOLLAR_ENDONLY option is ignored if PCRE2_MULTILINE is set.
  1254. </P>
  1255. <P>
  1256. When the newline convention (see
  1257. <a href="#newlines">"Newline conventions"</a>
  1258. below) recognizes the two-character sequence CRLF as a newline, this is
  1259. preferred, even if the single characters CR and LF are also recognized as
  1260. newlines. For example, if the newline convention is "any", a multiline mode
  1261. circumflex matches before "xyz" in the string "abc\r\nxyz" rather than after
  1262. CR, even though CR on its own is a valid newline. (It also matches at the very
  1263. start of the string, of course.)
  1264. </P>
  1265. <P>
  1266. Note that the sequences \A, \Z, and \z can be used to match the start and
  1267. end of the subject in both modes, and if all branches of a pattern start with
  1268. \A it is always anchored, whether or not PCRE2_MULTILINE is set.
  1269. <a name="fullstopdot"></a></P>
  1270. <br><a name="SEC7" href="#TOC1">FULL STOP (PERIOD, DOT) AND \N</a><br>
  1271. <P>
  1272. Outside a character class, a dot in the pattern matches any one character in
  1273. the subject string except (by default) a character that signifies the end of a
  1274. line. One or more characters may be specified as line terminators (see
  1275. <a href="#newlines">"Newline conventions"</a>
  1276. above).
  1277. </P>
  1278. <P>
  1279. Dot never matches a single line-ending character. When the two-character
  1280. sequence CRLF is the only line ending, dot does not match CR if it is
  1281. immediately followed by LF, but otherwise it matches all characters (including
  1282. isolated CRs and LFs). When ANYCRLF is selected for line endings, no occurrences
  1283. of CR of LF match dot. When all Unicode line endings are being recognized, dot
  1284. does not match CR or LF or any of the other line ending characters.
  1285. </P>
  1286. <P>
  1287. The behaviour of dot with regard to newlines can be changed. If the
  1288. PCRE2_DOTALL option is set, a dot matches any one character, without exception.
  1289. If the two-character sequence CRLF is present in the subject string, it takes
  1290. two dots to match it.
  1291. </P>
  1292. <P>
  1293. The handling of dot is entirely independent of the handling of circumflex and
  1294. dollar, the only relationship being that they both involve newlines. Dot has no
  1295. special meaning in a character class.
  1296. </P>
  1297. <P>
  1298. The escape sequence \N when not followed by an opening brace behaves like a
  1299. dot, except that it is not affected by the PCRE2_DOTALL option. In other words,
  1300. it matches any character except one that signifies the end of a line.
  1301. </P>
  1302. <P>
  1303. When \N is followed by an opening brace it has a different meaning. See the
  1304. section entitled
  1305. <a href="digitsafterbackslash">"Non-printing characters"</a>
  1306. above for details. Perl also uses \N{name} to specify characters by Unicode
  1307. name; PCRE2 does not support this.
  1308. </P>
  1309. <br><a name="SEC8" href="#TOC1">MATCHING A SINGLE CODE UNIT</a><br>
  1310. <P>
  1311. Outside a character class, the escape sequence \C matches any one code unit,
  1312. whether or not a UTF mode is set. In the 8-bit library, one code unit is one
  1313. byte; in the 16-bit library it is a 16-bit unit; in the 32-bit library it is a
  1314. 32-bit unit. Unlike a dot, \C always matches line-ending characters. The
  1315. feature is provided in Perl in order to match individual bytes in UTF-8 mode,
  1316. but it is unclear how it can usefully be used.
  1317. </P>
  1318. <P>
  1319. Because \C breaks up characters into individual code units, matching one unit
  1320. with \C in UTF-8 or UTF-16 mode means that the rest of the string may start
  1321. with a malformed UTF character. This has undefined results, because PCRE2
  1322. assumes that it is matching character by character in a valid UTF string (by
  1323. default it checks the subject string's validity at the start of processing
  1324. unless the PCRE2_NO_UTF_CHECK or PCRE2_MATCH_INVALID_UTF option is used).
  1325. </P>
  1326. <P>
  1327. An application can lock out the use of \C by setting the
  1328. PCRE2_NEVER_BACKSLASH_C option when compiling a pattern. It is also possible to
  1329. build PCRE2 with the use of \C permanently disabled.
  1330. </P>
  1331. <P>
  1332. PCRE2 does not allow \C to appear in lookbehind assertions
  1333. <a href="#lookbehind">(described below)</a>
  1334. in UTF-8 or UTF-16 modes, because this would make it impossible to calculate
  1335. the length of the lookbehind. Neither the alternative matching function
  1336. <b>pcre2_dfa_match()</b> nor the JIT optimizer support \C in these UTF modes.
  1337. The former gives a match-time error; the latter fails to optimize and so the
  1338. match is always run using the interpreter.
  1339. </P>
  1340. <P>
  1341. In the 32-bit library, however, \C is always supported (when not explicitly
  1342. locked out) because it always matches a single code unit, whether or not UTF-32
  1343. is specified.
  1344. </P>
  1345. <P>
  1346. In general, the \C escape sequence is best avoided. However, one way of using
  1347. it that avoids the problem of malformed UTF-8 or UTF-16 characters is to use a
  1348. lookahead to check the length of the next character, as in this pattern, which
  1349. could be used with a UTF-8 string (ignore white space and line breaks):
  1350. <pre>
  1351. (?| (?=[\x00-\x7f])(\C) |
  1352. (?=[\x80-\x{7ff}])(\C)(\C) |
  1353. (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
  1354. (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
  1355. </pre>
  1356. In this example, a group that starts with (?| resets the capturing parentheses
  1357. numbers in each alternative (see
  1358. <a href="#dupgroupnumber">"Duplicate Group Numbers"</a>
  1359. below). The assertions at the start of each branch check the next UTF-8
  1360. character for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
  1361. character's individual bytes are then captured by the appropriate number of
  1362. \C groups.
  1363. <a name="characterclass"></a></P>
  1364. <br><a name="SEC9" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
  1365. <P>
  1366. An opening square bracket introduces a character class, terminated by a closing
  1367. square bracket. A closing square bracket on its own is not special by default.
  1368. If a closing square bracket is required as a member of the class, it should be
  1369. the first data character in the class (after an initial circumflex, if present)
  1370. or escaped with a backslash. This means that, by default, an empty class cannot
  1371. be defined. However, if the PCRE2_ALLOW_EMPTY_CLASS option is set, a closing
  1372. square bracket at the start does end the (empty) class.
  1373. </P>
  1374. <P>
  1375. A character class matches a single character in the subject. A matched
  1376. character must be in the set of characters defined by the class, unless the
  1377. first character in the class definition is a circumflex, in which case the
  1378. subject character must not be in the set defined by the class. If a circumflex
  1379. is actually required as a member of the class, ensure it is not the first
  1380. character, or escape it with a backslash.
  1381. </P>
  1382. <P>
  1383. For example, the character class [aeiou] matches any lower case vowel, while
  1384. [^aeiou] matches any character that is not a lower case vowel. Note that a
  1385. circumflex is just a convenient notation for specifying the characters that
  1386. are in the class by enumerating those that are not. A class that starts with a
  1387. circumflex is not an assertion; it still consumes a character from the subject
  1388. string, and therefore it fails if the current pointer is at the end of the
  1389. string.
  1390. </P>
  1391. <P>
  1392. Characters in a class may be specified by their code points using \o, \x, or
  1393. \N{U+hh..} in the usual way. When caseless matching is set, any letters in a
  1394. class represent both their upper case and lower case versions, so for example,
  1395. a caseless [aeiou] matches "A" as well as "a", and a caseless [^aeiou] does not
  1396. match "A", whereas a caseful version would. Note that there are two ASCII
  1397. characters, K and S, that, in addition to their lower case ASCII equivalents,
  1398. are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F (long S)
  1399. respectively when either PCRE2_UTF or PCRE2_UCP is set.
  1400. </P>
  1401. <P>
  1402. Characters that might indicate line breaks are never treated in any special way
  1403. when matching character classes, whatever line-ending sequence is in use, and
  1404. whatever setting of the PCRE2_DOTALL and PCRE2_MULTILINE options is used. A
  1405. class such as [^a] always matches one of these characters.
  1406. </P>
  1407. <P>
  1408. The generic character type escape sequences \d, \D, \h, \H, \p, \P, \s,
  1409. \S, \v, \V, \w, and \W may appear in a character class, and add the
  1410. characters that they match to the class. For example, [\dABCDEF] matches any
  1411. hexadecimal digit. In UTF modes, the PCRE2_UCP option affects the meanings of
  1412. \d, \s, \w and their upper case partners, just as it does when they appear
  1413. outside a character class, as described in the section entitled
  1414. <a href="#genericchartypes">"Generic character types"</a>
  1415. above. The escape sequence \b has a different meaning inside a character
  1416. class; it matches the backspace character. The sequences \B, \R, and \X are
  1417. not special inside a character class. Like any other unrecognized escape
  1418. sequences, they cause an error. The same is true for \N when not followed by
  1419. an opening brace.
  1420. </P>
  1421. <P>
  1422. The minus (hyphen) character can be used to specify a range of characters in a
  1423. character class. For example, [d-m] matches any letter between d and m,
  1424. inclusive. If a minus character is required in a class, it must be escaped with
  1425. a backslash or appear in a position where it cannot be interpreted as
  1426. indicating a range, typically as the first or last character in the class,
  1427. or immediately after a range. For example, [b-d-z] matches letters in the range
  1428. b to d, a hyphen character, or z.
  1429. </P>
  1430. <P>
  1431. Perl treats a hyphen as a literal if it appears before or after a POSIX class
  1432. (see below) or before or after a character type escape such as \d or \H.
  1433. However, unless the hyphen is the last character in the class, Perl outputs a
  1434. warning in its warning mode, as this is most likely a user error. As PCRE2 has
  1435. no facility for warning, an error is given in these cases.
  1436. </P>
  1437. <P>
  1438. It is not possible to have the literal character "]" as the end character of a
  1439. range. A pattern such as [W-]46] is interpreted as a class of two characters
  1440. ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
  1441. "-46]". However, if the "]" is escaped with a backslash it is interpreted as
  1442. the end of range, so [W-\]46] is interpreted as a class containing a range
  1443. followed by two other characters. The octal or hexadecimal representation of
  1444. "]" can also be used to end a range.
  1445. </P>
  1446. <P>
  1447. Ranges normally include all code points between the start and end characters,
  1448. inclusive. They can also be used for code points specified numerically, for
  1449. example [\000-\037]. Ranges can include any characters that are valid for the
  1450. current mode. In any UTF mode, the so-called "surrogate" characters (those
  1451. whose code points lie between 0xd800 and 0xdfff inclusive) may not be specified
  1452. explicitly by default (the PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES option disables
  1453. this check). However, ranges such as [\x{d7ff}-\x{e000}], which include the
  1454. surrogates, are always permitted.
  1455. </P>
  1456. <P>
  1457. There is a special case in EBCDIC environments for ranges whose end points are
  1458. both specified as literal letters in the same case. For compatibility with
  1459. Perl, EBCDIC code points within the range that are not letters are omitted. For
  1460. example, [h-k] matches only four characters, even though the codes for h and k
  1461. are 0x88 and 0x92, a range of 11 code points. However, if the range is
  1462. specified numerically, for example, [\x88-\x92] or [h-\x92], all code points
  1463. are included.
  1464. </P>
  1465. <P>
  1466. If a range that includes letters is used when caseless matching is set, it
  1467. matches the letters in either case. For example, [W-c] is equivalent to
  1468. [][\\^_`wxyzabc], matched caselessly, and in a non-UTF mode, if character
  1469. tables for a French locale are in use, [\xc8-\xcb] matches accented E
  1470. characters in both cases.
  1471. </P>
  1472. <P>
  1473. A circumflex can conveniently be used with the upper case character types to
  1474. specify a more restricted set of characters than the matching lower case type.
  1475. For example, the class [^\W_] matches any letter or digit, but not underscore,
  1476. whereas [\w] includes underscore. A positive character class should be read as
  1477. "something OR something OR ..." and a negative class as "NOT something AND NOT
  1478. something AND NOT ...".
  1479. </P>
  1480. <P>
  1481. The only metacharacters that are recognized in character classes are backslash,
  1482. hyphen (only where it can be interpreted as specifying a range), circumflex
  1483. (only at the start), opening square bracket (only when it can be interpreted as
  1484. introducing a POSIX class name, or for a special compatibility feature - see
  1485. the next two sections), and the terminating closing square bracket. However,
  1486. escaping other non-alphanumeric characters does no harm.
  1487. </P>
  1488. <br><a name="SEC10" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
  1489. <P>
  1490. Perl supports the POSIX notation for character classes. This uses names
  1491. enclosed by [: and :] within the enclosing square brackets. PCRE2 also supports
  1492. this notation. For example,
  1493. <pre>
  1494. [01[:alpha:]%]
  1495. </pre>
  1496. matches "0", "1", any alphabetic character, or "%". The supported class names
  1497. are:
  1498. <pre>
  1499. alnum letters and digits
  1500. alpha letters
  1501. ascii character codes 0 - 127
  1502. blank space or tab only
  1503. cntrl control characters
  1504. digit decimal digits (same as \d)
  1505. graph printing characters, excluding space
  1506. lower lower case letters
  1507. print printing characters, including space
  1508. punct printing characters, excluding letters and digits and space
  1509. space white space (the same as \s from PCRE2 8.34)
  1510. upper upper case letters
  1511. word "word" characters (same as \w)
  1512. xdigit hexadecimal digits
  1513. </pre>
  1514. The default "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13),
  1515. and space (32). If locale-specific matching is taking place, the list of space
  1516. characters may be different; there may be fewer or more of them. "Space" and
  1517. \s match the same set of characters, as do "word" and \w.
  1518. </P>
  1519. <P>
  1520. The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
  1521. 5.8. Another Perl extension is negation, which is indicated by a ^ character
  1522. after the colon. For example,
  1523. <pre>
  1524. [12[:^digit:]]
  1525. </pre>
  1526. matches "1", "2", or any non-digit. PCRE2 (and Perl) also recognize the POSIX
  1527. syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
  1528. supported, and an error is given if they are encountered.
  1529. </P>
  1530. <P>
  1531. By default, characters with values greater than 127 do not match any of the
  1532. POSIX character classes, although this may be different for characters in the
  1533. range 128-255 when locale-specific matching is happening. However, in UCP mode,
  1534. unless certain options are set (see below), some of the classes are changed so
  1535. that Unicode character properties are used. This is achieved by replacing
  1536. POSIX classes with other sequences, as follows:
  1537. <pre>
  1538. [:alnum:] becomes \p{Xan}
  1539. [:alpha:] becomes \p{L}
  1540. [:blank:] becomes \h
  1541. [:cntrl:] becomes \p{Cc}
  1542. [:digit:] becomes \p{Nd}
  1543. [:lower:] becomes \p{Ll}
  1544. [:space:] becomes \p{Xps}
  1545. [:upper:] becomes \p{Lu}
  1546. [:word:] becomes \p{Xwd}
  1547. </pre>
  1548. Negated versions, such as [:^alpha:] use \P instead of \p. Four other POSIX
  1549. classes are handled specially in UCP mode:
  1550. </P>
  1551. <P>
  1552. [:graph:]
  1553. This matches characters that have glyphs that mark the page when printed. In
  1554. Unicode property terms, it matches all characters with the L, M, N, P, S, or Cf
  1555. properties, except for:
  1556. <pre>
  1557. U+061C Arabic Letter Mark
  1558. U+180E Mongolian Vowel Separator
  1559. U+2066 - U+2069 Various "isolate"s
  1560. </PRE>
  1561. </P>
  1562. <P>
  1563. [:print:]
  1564. This matches the same characters as [:graph:] plus space characters that are
  1565. not controls, that is, characters with the Zs property.
  1566. </P>
  1567. <P>
  1568. [:punct:]
  1569. This matches all characters that have the Unicode P (punctuation) property,
  1570. plus those characters with code points less than 256 that have the S (Symbol)
  1571. property.
  1572. </P>
  1573. <P>
  1574. [:xdigit:]
  1575. In addition to the ASCII hexadecimal digits, this also matches the "fullwidth"
  1576. versions of those characters, whose Unicode code points start at U+FF10. This
  1577. is a change that was made in PCRE release 10.43 for Perl compatibility.
  1578. </P>
  1579. <P>
  1580. The other POSIX classes are unchanged by PCRE2_UCP, and match only characters
  1581. with code points less than 256.
  1582. </P>
  1583. <P>
  1584. There are two options that can be used to restrict the POSIX classes to ASCII
  1585. characters when PCRE2_UCP is set. The option PCRE2_EXTRA_ASCII_DIGIT affects
  1586. just [:digit:] and [:xdigit:]. Within a pattern, this can be set and unset by
  1587. (?aT) and (?-aT). The PCRE2_EXTRA_ASCII_POSIX option disables UCP processing
  1588. for all POSIX classes, including [:digit:] and [:xdigit:]. Within a pattern,
  1589. (?aP) and (?-aP) set and unset both these options for consistency.
  1590. </P>
  1591. <br><a name="SEC11" href="#TOC1">COMPATIBILITY FEATURE FOR WORD BOUNDARIES</a><br>
  1592. <P>
  1593. In the POSIX.2 compliant library that was included in 4.4BSD Unix, the ugly
  1594. syntax [[:&#60;:]] and [[:&#62;:]] is used for matching "start of word" and "end of
  1595. word". PCRE2 treats these items as follows:
  1596. <pre>
  1597. [[:&#60;:]] is converted to \b(?=\w)
  1598. [[:&#62;:]] is converted to \b(?&#60;=\w)
  1599. </pre>
  1600. Only these exact character sequences are recognized. A sequence such as
  1601. [a[:&#60;:]b] provokes error for an unrecognized POSIX class name. This support is
  1602. not compatible with Perl. It is provided to help migrations from other
  1603. environments, and is best not used in any new patterns. Note that \b matches
  1604. at the start and the end of a word (see
  1605. <a href="#smallassertions">"Simple assertions"</a>
  1606. above), and in a Perl-style pattern the preceding or following character
  1607. normally shows which is wanted, without the need for the assertions that are
  1608. used above in order to give exactly the POSIX behaviour. Note also that the
  1609. PCRE2_UCP option changes the meaning of \w (and therefore \b) by default, so
  1610. it also affects these POSIX sequences.
  1611. </P>
  1612. <br><a name="SEC12" href="#TOC1">VERTICAL BAR</a><br>
  1613. <P>
  1614. Vertical bar characters are used to separate alternative patterns. For example,
  1615. the pattern
  1616. <pre>
  1617. gilbert|sullivan
  1618. </pre>
  1619. matches either "gilbert" or "sullivan". Any number of alternatives may appear,
  1620. and an empty alternative is permitted (matching the empty string). The matching
  1621. process tries each alternative in turn, from left to right, and the first one
  1622. that succeeds is used. If the alternatives are within a group
  1623. <a href="#group">(defined below),</a>
  1624. "succeeds" means matching the rest of the main pattern as well as the
  1625. alternative in the group.
  1626. <a name="internaloptions"></a></P>
  1627. <br><a name="SEC13" href="#TOC1">INTERNAL OPTION SETTING</a><br>
  1628. <P>
  1629. The settings of several options can be changed within a pattern by a sequence
  1630. of letters enclosed between "(?" and ")". The following are Perl-compatible,
  1631. and are described in detail in the
  1632. <a href="pcre2api.html"><b>pcre2api</b></a>
  1633. documentation. The option letters are:
  1634. <pre>
  1635. i for PCRE2_CASELESS
  1636. m for PCRE2_MULTILINE
  1637. n for PCRE2_NO_AUTO_CAPTURE
  1638. s for PCRE2_DOTALL
  1639. x for PCRE2_EXTENDED
  1640. xx for PCRE2_EXTENDED_MORE
  1641. </pre>
  1642. For example, (?im) sets caseless, multiline matching. It is also possible to
  1643. unset these options by preceding the relevant letters with a hyphen, for
  1644. example (?-im). The two "extended" options are not independent; unsetting
  1645. either one cancels the effects of both of them.
  1646. </P>
  1647. <P>
  1648. A combined setting and unsetting such as (?im-sx), which sets PCRE2_CASELESS
  1649. and PCRE2_MULTILINE while unsetting PCRE2_DOTALL and PCRE2_EXTENDED, is also
  1650. permitted. Only one hyphen may appear in the options string. If a letter
  1651. appears both before and after the hyphen, the option is unset. An empty options
  1652. setting "(?)" is allowed. Needless to say, it has no effect.
  1653. </P>
  1654. <P>
  1655. If the first character following (? is a circumflex, it causes all of the above
  1656. options to be unset. Letters may follow the circumflex to cause some options to
  1657. be re-instated, but a hyphen may not appear.
  1658. </P>
  1659. <P>
  1660. Some PCRE2-specific options can be changed by the same mechanism using these
  1661. pairs or individual letters:
  1662. <pre>
  1663. aD for PCRE2_EXTRA_ASCII_BSD
  1664. aS for PCRE2_EXTRA_ASCII_BSS
  1665. aW for PCRE2_EXTRA_ASCII_BSW
  1666. aP for PCRE2_EXTRA_ASCII_POSIX and PCRE2_EXTRA_ASCII_DIGIT
  1667. aT for PCRE2_EXTRA_ASCII_DIGIT
  1668. r for PCRE2_EXTRA_CASELESS_RESTRICT
  1669. J for PCRE2_DUPNAMES
  1670. U for PCRE2_UNGREEDY
  1671. </pre>
  1672. However, except for 'r', these are not unset by (?^), which is equivalent to
  1673. (?-imnrsx). If 'a' is not followed by any of the upper case letters shown
  1674. above, it sets (or unsets) all the ASCII options.
  1675. </P>
  1676. <P>
  1677. PCRE2_EXTRA_ASCII_DIGIT has no additional effect when PCRE2_EXTRA_ASCII_POSIX
  1678. is set, but including it in (?aP) means that (?-aP) suppresses all ASCII
  1679. restrictions for POSIX classes.
  1680. </P>
  1681. <P>
  1682. When one of these option changes occurs at top level (that is, not inside group
  1683. parentheses), the change applies until a subsequent change, or the end of the
  1684. pattern. An option change within a group (see below for a description of
  1685. groups) affects only that part of the group that follows it. At the end of the
  1686. group these options are reset to the state they were before the group. For
  1687. example,
  1688. <pre>
  1689. (a(?i)b)c
  1690. </pre>
  1691. matches abc and aBc and no other strings (assuming PCRE2_CASELESS is not set
  1692. externally). Any changes made in one alternative do carry on into subsequent
  1693. branches within the same group. For example,
  1694. <pre>
  1695. (a(?i)b|c)
  1696. </pre>
  1697. matches "ab", "aB", "c", and "C", even though when matching "C" the first
  1698. branch is abandoned before the option setting. This is because the effects of
  1699. option settings happen at compile time. There would be some very weird
  1700. behaviour otherwise.
  1701. </P>
  1702. <P>
  1703. As a convenient shorthand, if any option settings are required at the start of
  1704. a non-capturing group (see the next section), the option letters may
  1705. appear between the "?" and the ":". Thus the two patterns
  1706. <pre>
  1707. (?i:saturday|sunday)
  1708. (?:(?i)saturday|sunday)
  1709. </pre>
  1710. match exactly the same set of strings.
  1711. </P>
  1712. <P>
  1713. <b>Note:</b> There are other PCRE2-specific options, applying to the whole
  1714. pattern, which can be set by the application when the compiling function is
  1715. called. In addition, the pattern can contain special leading sequences such as
  1716. (*CRLF) to override what the application has set or what has been defaulted.
  1717. Details are given in the section entitled
  1718. <a href="#newlineseq">"Newline sequences"</a>
  1719. above. There are also the (*UTF) and (*UCP) leading sequences that can be used
  1720. to set UTF and Unicode property modes; they are equivalent to setting the
  1721. PCRE2_UTF and PCRE2_UCP options, respectively. However, the application can set
  1722. the PCRE2_NEVER_UTF or PCRE2_NEVER_UCP options, which lock out the use of the
  1723. (*UTF) and (*UCP) sequences.
  1724. <a name="group"></a></P>
  1725. <br><a name="SEC14" href="#TOC1">GROUPS</a><br>
  1726. <P>
  1727. Groups are delimited by parentheses (round brackets), which can be nested.
  1728. Turning part of a pattern into a group does two things:
  1729. <br>
  1730. <br>
  1731. 1. It localizes a set of alternatives. For example, the pattern
  1732. <pre>
  1733. cat(aract|erpillar|)
  1734. </pre>
  1735. matches "cataract", "caterpillar", or "cat". Without the parentheses, it would
  1736. match "cataract", "erpillar" or an empty string.
  1737. <br>
  1738. <br>
  1739. 2. It creates a "capture group". This means that, when the whole pattern
  1740. matches, the portion of the subject string that matched the group is passed
  1741. back to the caller, separately from the portion that matched the whole pattern.
  1742. (This applies only to the traditional matching function; the DFA matching
  1743. function does not support capturing.)
  1744. </P>
  1745. <P>
  1746. Opening parentheses are counted from left to right (starting from 1) to obtain
  1747. numbers for capture groups. For example, if the string "the red king" is
  1748. matched against the pattern
  1749. <pre>
  1750. the ((red|white) (king|queen))
  1751. </pre>
  1752. the captured substrings are "red king", "red", and "king", and are numbered 1,
  1753. 2, and 3, respectively.
  1754. </P>
  1755. <P>
  1756. The fact that plain parentheses fulfil two functions is not always helpful.
  1757. There are often times when grouping is required without capturing. If an
  1758. opening parenthesis is followed by a question mark and a colon, the group
  1759. does not do any capturing, and is not counted when computing the number of any
  1760. subsequent capture groups. For example, if the string "the white queen"
  1761. is matched against the pattern
  1762. <pre>
  1763. the ((?:red|white) (king|queen))
  1764. </pre>
  1765. the captured substrings are "white queen" and "queen", and are numbered 1 and
  1766. 2. The maximum number of capture groups is 65535.
  1767. </P>
  1768. <P>
  1769. As a convenient shorthand, if any option settings are required at the start of
  1770. a non-capturing group, the option letters may appear between the "?" and the
  1771. ":". Thus the two patterns
  1772. <pre>
  1773. (?i:saturday|sunday)
  1774. (?:(?i)saturday|sunday)
  1775. </pre>
  1776. match exactly the same set of strings. Because alternative branches are tried
  1777. from left to right, and options are not reset until the end of the group is
  1778. reached, an option setting in one branch does affect subsequent branches, so
  1779. the above patterns match "SUNDAY" as well as "Saturday".
  1780. <a name="dupgroupnumber"></a></P>
  1781. <br><a name="SEC15" href="#TOC1">DUPLICATE GROUP NUMBERS</a><br>
  1782. <P>
  1783. Perl 5.10 introduced a feature whereby each alternative in a group uses the
  1784. same numbers for its capturing parentheses. Such a group starts with (?| and is
  1785. itself a non-capturing group. For example, consider this pattern:
  1786. <pre>
  1787. (?|(Sat)ur|(Sun))day
  1788. </pre>
  1789. Because the two alternatives are inside a (?| group, both sets of capturing
  1790. parentheses are numbered one. Thus, when the pattern matches, you can look
  1791. at captured substring number one, whichever alternative matched. This construct
  1792. is useful when you want to capture part, but not all, of one of a number of
  1793. alternatives. Inside a (?| group, parentheses are numbered as usual, but the
  1794. number is reset at the start of each branch. The numbers of any capturing
  1795. parentheses that follow the whole group start after the highest number used in
  1796. any branch. The following example is taken from the Perl documentation. The
  1797. numbers underneath show in which buffer the captured content will be stored.
  1798. <pre>
  1799. # before ---------------branch-reset----------- after
  1800. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  1801. # 1 2 2 3 2 3 4
  1802. </pre>
  1803. A backreference to a capture group uses the most recent value that is set for
  1804. the group. The following pattern matches "abcabc" or "defdef":
  1805. <pre>
  1806. /(?|(abc)|(def))\1/
  1807. </pre>
  1808. In contrast, a subroutine call to a capture group always refers to the
  1809. first one in the pattern with the given number. The following pattern matches
  1810. "abcabc" or "defabc":
  1811. <pre>
  1812. /(?|(abc)|(def))(?1)/
  1813. </pre>
  1814. A relative reference such as (?-1) is no different: it is just a convenient way
  1815. of computing an absolute group number.
  1816. </P>
  1817. <P>
  1818. If a
  1819. <a href="#conditions">condition test</a>
  1820. for a group's having matched refers to a non-unique number, the test is
  1821. true if any group with that number has matched.
  1822. </P>
  1823. <P>
  1824. An alternative approach to using this "branch reset" feature is to use
  1825. duplicate named groups, as described in the next section.
  1826. </P>
  1827. <br><a name="SEC16" href="#TOC1">NAMED CAPTURE GROUPS</a><br>
  1828. <P>
  1829. Identifying capture groups by number is simple, but it can be very hard to keep
  1830. track of the numbers in complicated patterns. Furthermore, if an expression is
  1831. modified, the numbers may change. To help with this difficulty, PCRE2 supports
  1832. the naming of capture groups. This feature was not added to Perl until release
  1833. 5.10. Python had the feature earlier, and PCRE1 introduced it at release 4.0,
  1834. using the Python syntax. PCRE2 supports both the Perl and the Python syntax.
  1835. </P>
  1836. <P>
  1837. In PCRE2, a capture group can be named in one of three ways: (?&#60;name&#62;...) or
  1838. (?'name'...) as in Perl, or (?P&#60;name&#62;...) as in Python. Names may be up to 128
  1839. code units long. When PCRE2_UTF is not set, they may contain only ASCII
  1840. alphanumeric characters and underscores, but must start with a non-digit. When
  1841. PCRE2_UTF is set, the syntax of group names is extended to allow any Unicode
  1842. letter or Unicode decimal digit. In other words, group names must match one of
  1843. these patterns:
  1844. <pre>
  1845. ^[_A-Za-z][_A-Za-z0-9]*\z when PCRE2_UTF is not set
  1846. ^[_\p{L}][_\p{L}\p{Nd}]*\z when PCRE2_UTF is set
  1847. </pre>
  1848. References to capture groups from other parts of the pattern, such as
  1849. <a href="#backreferences">backreferences,</a>
  1850. <a href="#recursion">recursion,</a>
  1851. and
  1852. <a href="#conditions">conditions,</a>
  1853. can all be made by name as well as by number.
  1854. </P>
  1855. <P>
  1856. Named capture groups are allocated numbers as well as names, exactly as
  1857. if the names were not present. In both PCRE2 and Perl, capture groups
  1858. are primarily identified by numbers; any names are just aliases for these
  1859. numbers. The PCRE2 API provides function calls for extracting the complete
  1860. name-to-number translation table from a compiled pattern, as well as
  1861. convenience functions for extracting captured substrings by name.
  1862. </P>
  1863. <P>
  1864. <b>Warning:</b> When more than one capture group has the same number, as
  1865. described in the previous section, a name given to one of them applies to all
  1866. of them. Perl allows identically numbered groups to have different names.
  1867. Consider this pattern, where there are two capture groups, both numbered 1:
  1868. <pre>
  1869. (?|(?&#60;AA&#62;aa)|(?&#60;BB&#62;bb))
  1870. </pre>
  1871. Perl allows this, with both names AA and BB as aliases of group 1. Thus, after
  1872. a successful match, both names yield the same value (either "aa" or "bb").
  1873. </P>
  1874. <P>
  1875. In an attempt to reduce confusion, PCRE2 does not allow the same group number
  1876. to be associated with more than one name. The example above provokes a
  1877. compile-time error. However, there is still scope for confusion. Consider this
  1878. pattern:
  1879. <pre>
  1880. (?|(?&#60;AA&#62;aa)|(bb))
  1881. </pre>
  1882. Although the second group number 1 is not explicitly named, the name AA is
  1883. still an alias for any group 1. Whether the pattern matches "aa" or "bb", a
  1884. reference by name to group AA yields the matched string.
  1885. </P>
  1886. <P>
  1887. By default, a name must be unique within a pattern, except that duplicate names
  1888. are permitted for groups with the same number, for example:
  1889. <pre>
  1890. (?|(?&#60;AA&#62;aa)|(?&#60;AA&#62;bb))
  1891. </pre>
  1892. The duplicate name constraint can be disabled by setting the PCRE2_DUPNAMES
  1893. option at compile time, or by the use of (?J) within the pattern, as described
  1894. in the section entitled
  1895. <a href="#internaloptions">"Internal Option Setting"</a>
  1896. above.
  1897. </P>
  1898. <P>
  1899. Duplicate names can be useful for patterns where only one instance of the named
  1900. capture group can match. Suppose you want to match the name of a weekday,
  1901. either as a 3-letter abbreviation or as the full name, and in both cases you
  1902. want to extract the abbreviation. This pattern (ignoring the line breaks) does
  1903. the job:
  1904. <pre>
  1905. (?J)
  1906. (?&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
  1907. (?&#60;DN&#62;Tue)(?:sday)?|
  1908. (?&#60;DN&#62;Wed)(?:nesday)?|
  1909. (?&#60;DN&#62;Thu)(?:rsday)?|
  1910. (?&#60;DN&#62;Sat)(?:urday)?
  1911. </pre>
  1912. There are five capture groups, but only one is ever set after a match. The
  1913. convenience functions for extracting the data by name returns the substring for
  1914. the first (and in this example, the only) group of that name that matched. This
  1915. saves searching to find which numbered group it was. (An alternative way of
  1916. solving this problem is to use a "branch reset" group, as described in the
  1917. previous section.)
  1918. </P>
  1919. <P>
  1920. If you make a backreference to a non-unique named group from elsewhere in the
  1921. pattern, the groups to which the name refers are checked in the order in which
  1922. they appear in the overall pattern. The first one that is set is used for the
  1923. reference. For example, this pattern matches both "foofoo" and "barbar" but not
  1924. "foobar" or "barfoo":
  1925. <pre>
  1926. (?J)(?:(?&#60;n&#62;foo)|(?&#60;n&#62;bar))\k&#60;n&#62;
  1927. </PRE>
  1928. </P>
  1929. <P>
  1930. If you make a subroutine call to a non-unique named group, the one that
  1931. corresponds to the first occurrence of the name is used. In the absence of
  1932. duplicate numbers this is the one with the lowest number.
  1933. </P>
  1934. <P>
  1935. If you use a named reference in a condition
  1936. test (see the
  1937. <a href="#conditions">section about conditions</a>
  1938. below), either to check whether a capture group has matched, or to check for
  1939. recursion, all groups with the same name are tested. If the condition is true
  1940. for any one of them, the overall condition is true. This is the same behaviour
  1941. as testing by number. For further details of the interfaces for handling named
  1942. capture groups, see the
  1943. <a href="pcre2api.html"><b>pcre2api</b></a>
  1944. documentation.
  1945. </P>
  1946. <br><a name="SEC17" href="#TOC1">REPETITION</a><br>
  1947. <P>
  1948. Repetition is specified by quantifiers, which may follow any one of these
  1949. items:
  1950. <pre>
  1951. a literal data character
  1952. the dot metacharacter
  1953. the \C escape sequence
  1954. the \R escape sequence
  1955. the \X escape sequence
  1956. any escape sequence that matches a single character
  1957. a character class
  1958. a backreference
  1959. a parenthesized group (including lookaround assertions)
  1960. a subroutine call (recursive or otherwise)
  1961. </pre>
  1962. If a quantifier does not follow a repeatable item, an error occurs. The
  1963. general repetition quantifier specifies a minimum and maximum number of
  1964. permitted matches by giving two numbers in curly brackets (braces), separated
  1965. by a comma. The numbers must be less than 65536, and the first must be less
  1966. than or equal to the second. For example,
  1967. <pre>
  1968. z{2,4}
  1969. </pre>
  1970. matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
  1971. character. If the second number is omitted, but the comma is present, there is
  1972. no upper limit; if the second number and the comma are both omitted, the
  1973. quantifier specifies an exact number of required matches. Thus
  1974. <pre>
  1975. [aeiou]{3,}
  1976. </pre>
  1977. matches at least 3 successive vowels, but may match many more, whereas
  1978. <pre>
  1979. \d{8}
  1980. </pre>
  1981. matches exactly 8 digits. If the first number is omitted, the lower limit is
  1982. taken as zero; in this case the upper limit must be present.
  1983. <pre>
  1984. X{,4} is interpreted as X{0,4}
  1985. </pre>
  1986. This is a change in behaviour that happened in Perl 5.34.0 and PCRE2 10.43. In
  1987. earlier versions such a sequence was not interpreted as a quantifier. Other
  1988. regular expression engines may behave either way.
  1989. </P>
  1990. <P>
  1991. If the characters that follow an opening brace do not match the syntax of a
  1992. quantifier, the brace is taken as a literal character. In particular, this
  1993. means that {,} is a literal string of three characters.
  1994. </P>
  1995. <P>
  1996. Note that not every opening brace is potentially the start of a quantifier
  1997. because braces are used in other items such as \N{U+345} or \k{name}.
  1998. </P>
  1999. <P>
  2000. In UTF modes, quantifiers apply to characters rather than to individual code
  2001. units. Thus, for example, \x{100}{2} matches two characters, each of
  2002. which is represented by a two-byte sequence in a UTF-8 string. Similarly,
  2003. \X{3} matches three Unicode extended grapheme clusters, each of which may be
  2004. several code units long (and they may be of different lengths).
  2005. </P>
  2006. <P>
  2007. The quantifier {0} is permitted, causing the expression to behave as if the
  2008. previous item and the quantifier were not present. This may be useful for
  2009. capture groups that are referenced as
  2010. <a href="#groupsassubroutines">subroutines</a>
  2011. from elsewhere in the pattern (but see also the section entitled
  2012. <a href="#subdefine">"Defining capture groups for use by reference only"</a>
  2013. below). Except for parenthesized groups, items that have a {0} quantifier are
  2014. omitted from the compiled pattern.
  2015. </P>
  2016. <P>
  2017. For convenience, the three most common quantifiers have single-character
  2018. abbreviations:
  2019. <pre>
  2020. * is equivalent to {0,}
  2021. + is equivalent to {1,}
  2022. ? is equivalent to {0,1}
  2023. </pre>
  2024. It is possible to construct infinite loops by following a group that can match
  2025. no characters with a quantifier that has no upper limit, for example:
  2026. <pre>
  2027. (a?)*
  2028. </pre>
  2029. Earlier versions of Perl and PCRE1 used to give an error at compile time for
  2030. such patterns. However, because there are cases where this can be useful, such
  2031. patterns are now accepted, but whenever an iteration of such a group matches no
  2032. characters, matching moves on to the next item in the pattern instead of
  2033. repeatedly matching an empty string. This does not prevent backtracking into
  2034. any of the iterations if a subsequent item fails to match.
  2035. </P>
  2036. <P>
  2037. By default, quantifiers are "greedy", that is, they match as much as possible
  2038. (up to the maximum number of permitted repetitions), without causing the rest
  2039. of the pattern to fail. The classic example of where this gives problems is in
  2040. trying to match comments in C programs. These appear between /* and */ and
  2041. within the comment, individual * and / characters may appear. An attempt to
  2042. match C comments by applying the pattern
  2043. <pre>
  2044. /\*.*\*/
  2045. </pre>
  2046. to the string
  2047. <pre>
  2048. /* first comment */ not comment /* second comment */
  2049. </pre>
  2050. fails, because it matches the entire string owing to the greediness of the .*
  2051. item. However, if a quantifier is followed by a question mark, it ceases to be
  2052. greedy, and instead matches the minimum number of times possible, so the
  2053. pattern
  2054. <pre>
  2055. /\*.*?\*/
  2056. </pre>
  2057. does the right thing with C comments. The meaning of the various quantifiers is
  2058. not otherwise changed, just the preferred number of matches. Do not confuse
  2059. this use of question mark with its use as a quantifier in its own right.
  2060. Because it has two uses, it can sometimes appear doubled, as in
  2061. <pre>
  2062. \d??\d
  2063. </pre>
  2064. which matches one digit by preference, but can match two if that is the only
  2065. way the rest of the pattern matches.
  2066. </P>
  2067. <P>
  2068. If the PCRE2_UNGREEDY option is set (an option that is not available in Perl),
  2069. the quantifiers are not greedy by default, but individual ones can be made
  2070. greedy by following them with a question mark. In other words, it inverts the
  2071. default behaviour.
  2072. </P>
  2073. <P>
  2074. When a parenthesized group is quantified with a minimum repeat count that
  2075. is greater than 1 or with a limited maximum, more memory is required for the
  2076. compiled pattern, in proportion to the size of the minimum or maximum.
  2077. </P>
  2078. <P>
  2079. If a pattern starts with .* or .{0,} and the PCRE2_DOTALL option (equivalent
  2080. to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
  2081. implicitly anchored, because whatever follows will be tried against every
  2082. character position in the subject string, so there is no point in retrying the
  2083. overall match at any position after the first. PCRE2 normally treats such a
  2084. pattern as though it were preceded by \A.
  2085. </P>
  2086. <P>
  2087. In cases where it is known that the subject string contains no newlines, it is
  2088. worth setting PCRE2_DOTALL in order to obtain this optimization, or
  2089. alternatively, using ^ to indicate anchoring explicitly.
  2090. </P>
  2091. <P>
  2092. However, there are some cases where the optimization cannot be used. When .*
  2093. is inside capturing parentheses that are the subject of a backreference
  2094. elsewhere in the pattern, a match at the start may fail where a later one
  2095. succeeds. Consider, for example:
  2096. <pre>
  2097. (.*)abc\1
  2098. </pre>
  2099. If the subject is "xyz123abc123" the match point is the fourth character. For
  2100. this reason, such a pattern is not implicitly anchored.
  2101. </P>
  2102. <P>
  2103. Another case where implicit anchoring is not applied is when the leading .* is
  2104. inside an atomic group. Once again, a match at the start may fail where a later
  2105. one succeeds. Consider this pattern:
  2106. <pre>
  2107. (?&#62;.*?a)b
  2108. </pre>
  2109. It matches "ab" in the subject "aab". The use of the backtracking control verbs
  2110. (*PRUNE) and (*SKIP) also disable this optimization, and there is an option,
  2111. PCRE2_NO_DOTSTAR_ANCHOR, to do so explicitly.
  2112. </P>
  2113. <P>
  2114. When a capture group is repeated, the value captured is the substring that
  2115. matched the final iteration. For example, after
  2116. <pre>
  2117. (tweedle[dume]{3}\s*)+
  2118. </pre>
  2119. has matched "tweedledum tweedledee" the value of the captured substring is
  2120. "tweedledee". However, if there are nested capture groups, the corresponding
  2121. captured values may have been set in previous iterations. For example, after
  2122. <pre>
  2123. (a|(b))+
  2124. </pre>
  2125. matches "aba" the value of the second captured substring is "b".
  2126. <a name="atomicgroup"></a></P>
  2127. <br><a name="SEC18" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
  2128. <P>
  2129. With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
  2130. repetition, failure of what follows normally causes the repeated item to be
  2131. re-evaluated to see if a different number of repeats allows the rest of the
  2132. pattern to match. Sometimes it is useful to prevent this, either to change the
  2133. nature of the match, or to cause it fail earlier than it otherwise might, when
  2134. the author of the pattern knows there is no point in carrying on.
  2135. </P>
  2136. <P>
  2137. Consider, for example, the pattern \d+foo when applied to the subject line
  2138. <pre>
  2139. 123456bar
  2140. </pre>
  2141. After matching all 6 digits and then failing to match "foo", the normal
  2142. action of the matcher is to try again with only 5 digits matching the \d+
  2143. item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
  2144. (a term taken from Jeffrey Friedl's book) provides the means for specifying
  2145. that once a group has matched, it is not to be re-evaluated in this way.
  2146. </P>
  2147. <P>
  2148. If we use atomic grouping for the previous example, the matcher gives up
  2149. immediately on failing to match "foo" the first time. The notation is a kind of
  2150. special parenthesis, starting with (?&#62; as in this example:
  2151. <pre>
  2152. (?&#62;\d+)foo
  2153. </pre>
  2154. Perl 5.28 introduced an experimental alphabetic form starting with (* which may
  2155. be easier to remember:
  2156. <pre>
  2157. (*atomic:\d+)foo
  2158. </pre>
  2159. This kind of parenthesized group "locks up" the part of the pattern it contains
  2160. once it has matched, and a failure further into the pattern is prevented from
  2161. backtracking into it. Backtracking past it to previous items, however, works as
  2162. normal.
  2163. </P>
  2164. <P>
  2165. An alternative description is that a group of this type matches exactly the
  2166. string of characters that an identical standalone pattern would match, if
  2167. anchored at the current point in the subject string.
  2168. </P>
  2169. <P>
  2170. Atomic groups are not capture groups. Simple cases such as the above example
  2171. can be thought of as a maximizing repeat that must swallow everything it can.
  2172. So, while both \d+ and \d+? are prepared to adjust the number of digits they
  2173. match in order to make the rest of the pattern match, (?&#62;\d+) can only match
  2174. an entire sequence of digits.
  2175. </P>
  2176. <P>
  2177. Atomic groups in general can of course contain arbitrarily complicated
  2178. expressions, and can be nested. However, when the contents of an atomic
  2179. group is just a single repeated item, as in the example above, a simpler
  2180. notation, called a "possessive quantifier" can be used. This consists of an
  2181. additional + character following a quantifier. Using this notation, the
  2182. previous example can be rewritten as
  2183. <pre>
  2184. \d++foo
  2185. </pre>
  2186. Note that a possessive quantifier can be used with an entire group, for
  2187. example:
  2188. <pre>
  2189. (abc|xyz){2,3}+
  2190. </pre>
  2191. Possessive quantifiers are always greedy; the setting of the PCRE2_UNGREEDY
  2192. option is ignored. They are a convenient notation for the simpler forms of
  2193. atomic group. However, there is no difference in the meaning of a possessive
  2194. quantifier and the equivalent atomic group, though there may be a performance
  2195. difference; possessive quantifiers should be slightly faster.
  2196. </P>
  2197. <P>
  2198. The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
  2199. Jeffrey Friedl originated the idea (and the name) in the first edition of his
  2200. book. Mike McCloskey liked it, so implemented it when he built Sun's Java
  2201. package, and PCRE1 copied it from there. It found its way into Perl at release
  2202. 5.10.
  2203. </P>
  2204. <P>
  2205. PCRE2 has an optimization that automatically "possessifies" certain simple
  2206. pattern constructs. For example, the sequence A+B is treated as A++B because
  2207. there is no point in backtracking into a sequence of A's when B must follow.
  2208. This feature can be disabled by the PCRE2_NO_AUTOPOSSESS option, or starting
  2209. the pattern with (*NO_AUTO_POSSESS).
  2210. </P>
  2211. <P>
  2212. When a pattern contains an unlimited repeat inside a group that can itself be
  2213. repeated an unlimited number of times, the use of an atomic group is the only
  2214. way to avoid some failing matches taking a very long time indeed. The pattern
  2215. <pre>
  2216. (\D+|&#60;\d+&#62;)*[!?]
  2217. </pre>
  2218. matches an unlimited number of substrings that either consist of non-digits, or
  2219. digits enclosed in &#60;&#62;, followed by either ! or ?. When it matches, it runs
  2220. quickly. However, if it is applied to
  2221. <pre>
  2222. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  2223. </pre>
  2224. it takes a long time before reporting failure. This is because the string can
  2225. be divided between the internal \D+ repeat and the external * repeat in a
  2226. large number of ways, and all have to be tried. (The example uses [!?] rather
  2227. than a single character at the end, because both PCRE2 and Perl have an
  2228. optimization that allows for fast failure when a single character is used. They
  2229. remember the last single character that is required for a match, and fail early
  2230. if it is not present in the string.) If the pattern is changed so that it uses
  2231. an atomic group, like this:
  2232. <pre>
  2233. ((?&#62;\D+)|&#60;\d+&#62;)*[!?]
  2234. </pre>
  2235. sequences of non-digits cannot be broken, and failure happens quickly.
  2236. <a name="backreferences"></a></P>
  2237. <br><a name="SEC19" href="#TOC1">BACKREFERENCES</a><br>
  2238. <P>
  2239. Outside a character class, a backslash followed by a digit greater than 0 (and
  2240. possibly further digits) is a backreference to a capture group earlier (that
  2241. is, to its left) in the pattern, provided there have been that many previous
  2242. capture groups.
  2243. </P>
  2244. <P>
  2245. However, if the decimal number following the backslash is less than 8, it is
  2246. always taken as a backreference, and causes an error only if there are not that
  2247. many capture groups in the entire pattern. In other words, the group that is
  2248. referenced need not be to the left of the reference for numbers less than 8. A
  2249. "forward backreference" of this type can make sense when a repetition is
  2250. involved and the group to the right has participated in an earlier iteration.
  2251. </P>
  2252. <P>
  2253. It is not possible to have a numerical "forward backreference" to a group whose
  2254. number is 8 or more using this syntax because a sequence such as \50 is
  2255. interpreted as a character defined in octal. See the subsection entitled
  2256. "Non-printing characters"
  2257. <a href="#digitsafterbackslash">above</a>
  2258. for further details of the handling of digits following a backslash. Other
  2259. forms of backreferencing do not suffer from this restriction. In particular,
  2260. there is no problem when named capture groups are used (see below).
  2261. </P>
  2262. <P>
  2263. Another way of avoiding the ambiguity inherent in the use of digits following a
  2264. backslash is to use the \g escape sequence. This escape must be followed by a
  2265. signed or unsigned number, optionally enclosed in braces. These examples are
  2266. all identical:
  2267. <pre>
  2268. (ring), \1
  2269. (ring), \g1
  2270. (ring), \g{1}
  2271. </pre>
  2272. An unsigned number specifies an absolute reference without the ambiguity that
  2273. is present in the older syntax. It is also useful when literal digits follow
  2274. the reference. A signed number is a relative reference. Consider this example:
  2275. <pre>
  2276. (abc(def)ghi)\g{-1}
  2277. </pre>
  2278. The sequence \g{-1} is a reference to the capture group whose number is one
  2279. less than the number of the next group to be started, so in this example (where
  2280. the next group would be numbered 3) is it equivalent to \2, and \g{-2} would
  2281. be equivalent to \1. Note that if this construct is inside a capture group,
  2282. that group is included in the count, so in this example \g{-2} also refers to
  2283. group 1:
  2284. <pre>
  2285. (A)(\g{-2}B)
  2286. </pre>
  2287. The use of relative references can be helpful in long patterns, and also in
  2288. patterns that are created by joining together fragments that contain references
  2289. within themselves.
  2290. </P>
  2291. <P>
  2292. The sequence \g{+1} is a reference to the next capture group that is started
  2293. after this item, and \g{+2} refers to the one after that, and so on. This kind
  2294. of forward reference can be useful in patterns that repeat. Perl does not
  2295. support the use of + in this way.
  2296. </P>
  2297. <P>
  2298. A backreference matches whatever actually most recently matched the capture
  2299. group in the current subject string, rather than anything at all that matches
  2300. the group (see
  2301. <a href="#groupsassubroutines">"Groups as subroutines"</a>
  2302. below for a way of doing that). So the pattern
  2303. <pre>
  2304. (sens|respons)e and \1ibility
  2305. </pre>
  2306. matches "sense and sensibility" and "response and responsibility", but not
  2307. "sense and responsibility". If caseful matching is in force at the time of the
  2308. backreference, the case of letters is relevant. For example,
  2309. <pre>
  2310. ((?i)rah)\s+\1
  2311. </pre>
  2312. matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
  2313. capture group is matched caselessly.
  2314. </P>
  2315. <P>
  2316. There are several different ways of writing backreferences to named capture
  2317. groups. The .NET syntax is \k{name}, the Python syntax is (?=name), and the
  2318. original Perl syntax is \k&#60;name&#62; or \k'name'. All of these are now supported
  2319. by both Perl and PCRE2. Perl 5.10's unified backreference syntax, in which \g
  2320. can be used for both numeric and named references, is also supported by PCRE2.
  2321. We could rewrite the above example in any of the following ways:
  2322. <pre>
  2323. (?&#60;p1&#62;(?i)rah)\s+\k&#60;p1&#62;
  2324. (?'p1'(?i)rah)\s+\k{p1}
  2325. (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
  2326. (?&#60;p1&#62;(?i)rah)\s+\g{p1}
  2327. </pre>
  2328. A capture group that is referenced by name may appear in the pattern before or
  2329. after the reference.
  2330. </P>
  2331. <P>
  2332. There may be more than one backreference to the same group. If a group has not
  2333. actually been used in a particular match, backreferences to it always fail by
  2334. default. For example, the pattern
  2335. <pre>
  2336. (a|(bc))\2
  2337. </pre>
  2338. always fails if it starts to match "a" rather than "bc". However, if the
  2339. PCRE2_MATCH_UNSET_BACKREF option is set at compile time, a backreference to an
  2340. unset value matches an empty string.
  2341. </P>
  2342. <P>
  2343. Because there may be many capture groups in a pattern, all digits following a
  2344. backslash are taken as part of a potential backreference number. If the pattern
  2345. continues with a digit character, some delimiter must be used to terminate the
  2346. backreference. If the PCRE2_EXTENDED or PCRE2_EXTENDED_MORE option is set, this
  2347. can be white space. Otherwise, the \g{} syntax or an empty comment (see
  2348. <a href="#comments">"Comments"</a>
  2349. below) can be used.
  2350. </P>
  2351. <br><b>
  2352. Recursive backreferences
  2353. </b><br>
  2354. <P>
  2355. A backreference that occurs inside the group to which it refers fails when the
  2356. group is first used, so, for example, (a\1) never matches. However, such
  2357. references can be useful inside repeated groups. For example, the pattern
  2358. <pre>
  2359. (a|b\1)+
  2360. </pre>
  2361. matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
  2362. the group, the backreference matches the character string corresponding to the
  2363. previous iteration. In order for this to work, the pattern must be such that
  2364. the first iteration does not need to match the backreference. This can be done
  2365. using alternation, as in the example above, or by a quantifier with a minimum
  2366. of zero.
  2367. </P>
  2368. <P>
  2369. For versions of PCRE2 less than 10.25, backreferences of this type used to
  2370. cause the group that they reference to be treated as an
  2371. <a href="#atomicgroup">atomic group.</a>
  2372. This restriction no longer applies, and backtracking into such groups can occur
  2373. as normal.
  2374. <a name="bigassertions"></a></P>
  2375. <br><a name="SEC20" href="#TOC1">ASSERTIONS</a><br>
  2376. <P>
  2377. An assertion is a test on the characters following or preceding the current
  2378. matching point that does not consume any characters. The simple assertions
  2379. coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described
  2380. <a href="#smallassertions">above.</a>
  2381. </P>
  2382. <P>
  2383. More complicated assertions are coded as parenthesized groups. There are two
  2384. kinds: those that look ahead of the current position in the subject string, and
  2385. those that look behind it, and in each case an assertion may be positive (must
  2386. match for the assertion to be true) or negative (must not match for the
  2387. assertion to be true). An assertion group is matched in the normal way,
  2388. and if it is true, matching continues after it, but with the matching position
  2389. in the subject string reset to what it was before the assertion was processed.
  2390. </P>
  2391. <P>
  2392. The Perl-compatible lookaround assertions are atomic. If an assertion is true,
  2393. but there is a subsequent matching failure, there is no backtracking into the
  2394. assertion. However, there are some cases where non-atomic assertions can be
  2395. useful. PCRE2 has some support for these, described in the section entitled
  2396. <a href="#nonatomicassertions">"Non-atomic assertions"</a>
  2397. below, but they are not Perl-compatible.
  2398. </P>
  2399. <P>
  2400. A lookaround assertion may appear as the condition in a
  2401. <a href="#conditions">conditional group</a>
  2402. (see below). In this case, the result of matching the assertion determines
  2403. which branch of the condition is followed.
  2404. </P>
  2405. <P>
  2406. Assertion groups are not capture groups. If an assertion contains capture
  2407. groups within it, these are counted for the purposes of numbering the capture
  2408. groups in the whole pattern. Within each branch of an assertion, locally
  2409. captured substrings may be referenced in the usual way. For example, a sequence
  2410. such as (.)\g{-1} can be used to check that two adjacent characters are the
  2411. same.
  2412. </P>
  2413. <P>
  2414. When a branch within an assertion fails to match, any substrings that were
  2415. captured are discarded (as happens with any pattern branch that fails to
  2416. match). A negative assertion is true only when all its branches fail to match;
  2417. this means that no captured substrings are ever retained after a successful
  2418. negative assertion. When an assertion contains a matching branch, what happens
  2419. depends on the type of assertion.
  2420. </P>
  2421. <P>
  2422. For a positive assertion, internally captured substrings in the successful
  2423. branch are retained, and matching continues with the next pattern item after
  2424. the assertion. For a negative assertion, a matching branch means that the
  2425. assertion is not true. If such an assertion is being used as a condition in a
  2426. <a href="#conditions">conditional group</a>
  2427. (see below), captured substrings are retained, because matching continues with
  2428. the "no" branch of the condition. For other failing negative assertions,
  2429. control passes to the previous backtracking point, thus discarding any captured
  2430. strings within the assertion.
  2431. </P>
  2432. <P>
  2433. Most assertion groups may be repeated; though it makes no sense to assert the
  2434. same thing several times, the side effect of capturing in positive assertions
  2435. may occasionally be useful. However, an assertion that forms the condition for
  2436. a conditional group may not be quantified. PCRE2 used to restrict the
  2437. repetition of assertions, but from release 10.35 the only restriction is that
  2438. an unlimited maximum repetition is changed to be one more than the minimum. For
  2439. example, {3,} is treated as {3,4}.
  2440. </P>
  2441. <br><b>
  2442. Alphabetic assertion names
  2443. </b><br>
  2444. <P>
  2445. Traditionally, symbolic sequences such as (?= and (?&#60;= have been used to
  2446. specify lookaround assertions. Perl 5.28 introduced some experimental
  2447. alphabetic alternatives which might be easier to remember. They all start with
  2448. (* instead of (? and must be written using lower case letters. PCRE2 supports
  2449. the following synonyms:
  2450. <pre>
  2451. (*positive_lookahead: or (*pla: is the same as (?=
  2452. (*negative_lookahead: or (*nla: is the same as (?!
  2453. (*positive_lookbehind: or (*plb: is the same as (?&#60;=
  2454. (*negative_lookbehind: or (*nlb: is the same as (?&#60;!
  2455. </pre>
  2456. For example, (*pla:foo) is the same assertion as (?=foo). In the following
  2457. sections, the various assertions are described using the original symbolic
  2458. forms.
  2459. </P>
  2460. <br><b>
  2461. Lookahead assertions
  2462. </b><br>
  2463. <P>
  2464. Lookahead assertions start with (?= for positive assertions and (?! for
  2465. negative assertions. For example,
  2466. <pre>
  2467. \w+(?=;)
  2468. </pre>
  2469. matches a word followed by a semicolon, but does not include the semicolon in
  2470. the match, and
  2471. <pre>
  2472. foo(?!bar)
  2473. </pre>
  2474. matches any occurrence of "foo" that is not followed by "bar". Note that the
  2475. apparently similar pattern
  2476. <pre>
  2477. (?!foo)bar
  2478. </pre>
  2479. does not find an occurrence of "bar" that is preceded by something other than
  2480. "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
  2481. (?!foo) is always true when the next three characters are "bar". A
  2482. lookbehind assertion is needed to achieve the other effect.
  2483. </P>
  2484. <P>
  2485. If you want to force a matching failure at some point in a pattern, the most
  2486. convenient way to do it is with (?!) because an empty string always matches, so
  2487. an assertion that requires there not to be an empty string must always fail.
  2488. The backtracking control verb (*FAIL) or (*F) is a synonym for (?!).
  2489. <a name="lookbehind"></a></P>
  2490. <br><b>
  2491. Lookbehind assertions
  2492. </b><br>
  2493. <P>
  2494. Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for
  2495. negative assertions. For example,
  2496. <pre>
  2497. (?&#60;!foo)bar
  2498. </pre>
  2499. does find an occurrence of "bar" that is not preceded by "foo". The contents of
  2500. a lookbehind assertion are restricted such that there must be a known maximum
  2501. to the lengths of all the strings it matches. There are two cases:
  2502. </P>
  2503. <P>
  2504. If every top-level alternative matches a fixed length, for example
  2505. <pre>
  2506. (?&#60;=colour|color)
  2507. </pre>
  2508. there is a limit of 65535 characters to the lengths, which do not have to be
  2509. the same, as this example demonstrates. This is the only kind of lookbehind
  2510. supported by PCRE2 versions earlier than 10.43 and by the alternative matching
  2511. function <b>pcre2_dfa_match()</b>.
  2512. </P>
  2513. <P>
  2514. In PCRE2 10.43 and later, <b>pcre2_match()</b> supports lookbehind assertions in
  2515. which one or more top-level alternatives can match more than one string length,
  2516. for example
  2517. <pre>
  2518. (?&#60;=colou?r)
  2519. </pre>
  2520. The maximum matching length for any branch of the lookbehind is limited to a
  2521. value set by the calling program (default 255 characters). Unlimited repetition
  2522. (for example \d*) is not supported. In some cases, the escape sequence \K
  2523. <a href="#resetmatchstart">(see above)</a>
  2524. can be used instead of a lookbehind assertion at the start of a pattern to get
  2525. round the length limit restriction.
  2526. </P>
  2527. <P>
  2528. In UTF-8 and UTF-16 modes, PCRE2 does not allow the \C escape (which matches a
  2529. single code unit even in a UTF mode) to appear in lookbehind assertions,
  2530. because it makes it impossible to calculate the length of the lookbehind. The
  2531. \X and \R escapes, which can match different numbers of code units, are never
  2532. permitted in lookbehinds.
  2533. </P>
  2534. <P>
  2535. <a href="#groupsassubroutines">"Subroutine"</a>
  2536. calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
  2537. as the called capture group matches a limited-length string. However,
  2538. <a href="#recursion">recursion,</a>
  2539. that is, a "subroutine" call into a group that is already active,
  2540. is not supported.
  2541. </P>
  2542. <P>
  2543. PCRE2 supports backreferences in lookbehinds, but only if certain conditions
  2544. are met. The PCRE2_MATCH_UNSET_BACKREF option must not be set, there must be no
  2545. use of (?| in the pattern (it creates duplicate group numbers), and if the
  2546. backreference is by name, the name must be unique. Of course, the referenced
  2547. group must itself match a limited length substring. The following pattern
  2548. matches words containing at least two characters that begin and end with the
  2549. same character:
  2550. <pre>
  2551. \b(\w)\w++(?&#60;=\1)
  2552. </PRE>
  2553. </P>
  2554. <P>
  2555. Possessive quantifiers can be used in conjunction with lookbehind assertions to
  2556. specify efficient matching at the end of subject strings. Consider a simple
  2557. pattern such as
  2558. <pre>
  2559. abcd$
  2560. </pre>
  2561. when applied to a long string that does not match. Because matching proceeds
  2562. from left to right, PCRE2 will look for each "a" in the subject and then see if
  2563. what follows matches the rest of the pattern. If the pattern is specified as
  2564. <pre>
  2565. ^.*abcd$
  2566. </pre>
  2567. the initial .* matches the entire string at first, but when this fails (because
  2568. there is no following "a"), it backtracks to match all but the last character,
  2569. then all but the last two characters, and so on. Once again the search for "a"
  2570. covers the entire string, from right to left, so we are no better off. However,
  2571. if the pattern is written as
  2572. <pre>
  2573. ^.*+(?&#60;=abcd)
  2574. </pre>
  2575. there can be no backtracking for the .*+ item because of the possessive
  2576. quantifier; it can match only the entire string. The subsequent lookbehind
  2577. assertion does a single test on the last four characters. If it fails, the
  2578. match fails immediately. For long strings, this approach makes a significant
  2579. difference to the processing time.
  2580. </P>
  2581. <br><b>
  2582. Using multiple assertions
  2583. </b><br>
  2584. <P>
  2585. Several assertions (of any sort) may occur in succession. For example,
  2586. <pre>
  2587. (?&#60;=\d{3})(?&#60;!999)foo
  2588. </pre>
  2589. matches "foo" preceded by three digits that are not "999". Notice that each of
  2590. the assertions is applied independently at the same point in the subject
  2591. string. First there is a check that the previous three characters are all
  2592. digits, and then there is a check that the same three characters are not "999".
  2593. This pattern does <i>not</i> match "foo" preceded by six characters, the first
  2594. of which are digits and the last three of which are not "999". For example, it
  2595. doesn't match "123abcfoo". A pattern to do that is
  2596. <pre>
  2597. (?&#60;=\d{3}...)(?&#60;!999)foo
  2598. </pre>
  2599. This time the first assertion looks at the preceding six characters, checking
  2600. that the first three are digits, and then the second assertion checks that the
  2601. preceding three characters are not "999".
  2602. </P>
  2603. <P>
  2604. Assertions can be nested in any combination. For example,
  2605. <pre>
  2606. (?&#60;=(?&#60;!foo)bar)baz
  2607. </pre>
  2608. matches an occurrence of "baz" that is preceded by "bar" which in turn is not
  2609. preceded by "foo", while
  2610. <pre>
  2611. (?&#60;=\d{3}(?!999)...)foo
  2612. </pre>
  2613. is another pattern that matches "foo" preceded by three digits and any three
  2614. characters that are not "999".
  2615. <a name="nonatomicassertions"></a></P>
  2616. <br><a name="SEC21" href="#TOC1">NON-ATOMIC ASSERTIONS</a><br>
  2617. <P>
  2618. Traditional lookaround assertions are atomic. That is, if an assertion is true,
  2619. but there is a subsequent matching failure, there is no backtracking into the
  2620. assertion. However, there are some cases where non-atomic positive assertions
  2621. can be useful. PCRE2 provides these using the following syntax:
  2622. <pre>
  2623. (*non_atomic_positive_lookahead: or (*napla: or (?*
  2624. (*non_atomic_positive_lookbehind: or (*naplb: or (?&#60;*
  2625. </pre>
  2626. Consider the problem of finding the right-most word in a string that also
  2627. appears earlier in the string, that is, it must appear at least twice in total.
  2628. This pattern returns the required result as captured substring 1:
  2629. <pre>
  2630. ^(?x)(*napla: .* \b(\w++)) (?&#62; .*? \b\1\b ){2}
  2631. </pre>
  2632. For a subject such as "word1 word2 word3 word2 word3 word4" the result is
  2633. "word3". How does it work? At the start, ^(?x) anchors the pattern and sets the
  2634. "x" option, which causes white space (introduced for readability) to be
  2635. ignored. Inside the assertion, the greedy .* at first consumes the entire
  2636. string, but then has to backtrack until the rest of the assertion can match a
  2637. word, which is captured by group 1. In other words, when the assertion first
  2638. succeeds, it captures the right-most word in the string.
  2639. </P>
  2640. <P>
  2641. The current matching point is then reset to the start of the subject, and the
  2642. rest of the pattern match checks for two occurrences of the captured word,
  2643. using an ungreedy .*? to scan from the left. If this succeeds, we are done, but
  2644. if the last word in the string does not occur twice, this part of the pattern
  2645. fails. If a traditional atomic lookahead (?= or (*pla: had been used, the
  2646. assertion could not be re-entered, and the whole match would fail. The pattern
  2647. would succeed only if the very last word in the subject was found twice.
  2648. </P>
  2649. <P>
  2650. Using a non-atomic lookahead, however, means that when the last word does not
  2651. occur twice in the string, the lookahead can backtrack and find the second-last
  2652. word, and so on, until either the match succeeds, or all words have been
  2653. tested.
  2654. </P>
  2655. <P>
  2656. Two conditions must be met for a non-atomic assertion to be useful: the
  2657. contents of one or more capturing groups must change after a backtrack into the
  2658. assertion, and there must be a backreference to a changed group later in the
  2659. pattern. If this is not the case, the rest of the pattern match fails exactly
  2660. as before because nothing has changed, so using a non-atomic assertion just
  2661. wastes resources.
  2662. </P>
  2663. <P>
  2664. There is one exception to backtracking into a non-atomic assertion. If an
  2665. (*ACCEPT) control verb is triggered, the assertion succeeds atomically. That
  2666. is, a subsequent match failure cannot backtrack into the assertion.
  2667. </P>
  2668. <P>
  2669. Non-atomic assertions are not supported by the alternative matching function
  2670. <b>pcre2_dfa_match()</b>. They are supported by JIT, but only if they do not
  2671. contain any control verbs such as (*ACCEPT). (This may change in future). Note
  2672. that assertions that appear as conditions for
  2673. <a href="#conditions">conditional groups</a>
  2674. (see below) must be atomic.
  2675. </P>
  2676. <br><a name="SEC22" href="#TOC1">SCRIPT RUNS</a><br>
  2677. <P>
  2678. In concept, a script run is a sequence of characters that are all from the same
  2679. Unicode script such as Latin or Greek. However, because some scripts are
  2680. commonly used together, and because some diacritical and other marks are used
  2681. with multiple scripts, it is not that simple. There is a full description of
  2682. the rules that PCRE2 uses in the section entitled
  2683. <a href="pcre2unicode.html#scriptruns">"Script Runs"</a>
  2684. in the
  2685. <a href="pcre2unicode.html"><b>pcre2unicode</b></a>
  2686. documentation.
  2687. </P>
  2688. <P>
  2689. If part of a pattern is enclosed between (*script_run: or (*sr: and a closing
  2690. parenthesis, it fails if the sequence of characters that it matches are not a
  2691. script run. After a failure, normal backtracking occurs. Script runs can be
  2692. used to detect spoofing attacks using characters that look the same, but are
  2693. from different scripts. The string "paypal.com" is an infamous example, where
  2694. the letters could be a mixture of Latin and Cyrillic. This pattern ensures that
  2695. the matched characters in a sequence of non-spaces that follow white space are
  2696. a script run:
  2697. <pre>
  2698. \s+(*sr:\S+)
  2699. </pre>
  2700. To be sure that they are all from the Latin script (for example), a lookahead
  2701. can be used:
  2702. <pre>
  2703. \s+(?=\p{Latin})(*sr:\S+)
  2704. </pre>
  2705. This works as long as the first character is expected to be a character in that
  2706. script, and not (for example) punctuation, which is allowed with any script. If
  2707. this is not the case, a more creative lookahead is needed. For example, if
  2708. digits, underscore, and dots are permitted at the start:
  2709. <pre>
  2710. \s+(?=[0-9_.]*\p{Latin})(*sr:\S+)
  2711. </PRE>
  2712. </P>
  2713. <P>
  2714. In many cases, backtracking into a script run pattern fragment is not
  2715. desirable. The script run can employ an atomic group to prevent this. Because
  2716. this is a common requirement, a shorthand notation is provided by
  2717. (*atomic_script_run: or (*asr:
  2718. <pre>
  2719. (*asr:...) is the same as (*sr:(?&#62;...))
  2720. </pre>
  2721. Note that the atomic group is inside the script run. Putting it outside would
  2722. not prevent backtracking into the script run pattern.
  2723. </P>
  2724. <P>
  2725. Support for script runs is not available if PCRE2 is compiled without Unicode
  2726. support. A compile-time error is given if any of the above constructs is
  2727. encountered. Script runs are not supported by the alternate matching function,
  2728. <b>pcre2_dfa_match()</b> because they use the same mechanism as capturing
  2729. parentheses.
  2730. </P>
  2731. <P>
  2732. <b>Warning:</b> The (*ACCEPT) control verb
  2733. <a href="#acceptverb">(see below)</a>
  2734. should not be used within a script run group, because it causes an immediate
  2735. exit from the group, bypassing the script run checking.
  2736. <a name="conditions"></a></P>
  2737. <br><a name="SEC23" href="#TOC1">CONDITIONAL GROUPS</a><br>
  2738. <P>
  2739. It is possible to cause the matching process to obey a pattern fragment
  2740. conditionally or to choose between two alternative fragments, depending on
  2741. the result of an assertion, or whether a specific capture group has
  2742. already been matched. The two possible forms of conditional group are:
  2743. <pre>
  2744. (?(condition)yes-pattern)
  2745. (?(condition)yes-pattern|no-pattern)
  2746. </pre>
  2747. If the condition is satisfied, the yes-pattern is used; otherwise the
  2748. no-pattern (if present) is used. An absent no-pattern is equivalent to an empty
  2749. string (it always matches). If there are more than two alternatives in the
  2750. group, a compile-time error occurs. Each of the two alternatives may itself
  2751. contain nested groups of any form, including conditional groups; the
  2752. restriction to two alternatives applies only at the level of the condition
  2753. itself. This pattern fragment is an example where the alternatives are complex:
  2754. <pre>
  2755. (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
  2756. </PRE>
  2757. </P>
  2758. <P>
  2759. There are five kinds of condition: references to capture groups, references to
  2760. recursion, two pseudo-conditions called DEFINE and VERSION, and assertions.
  2761. </P>
  2762. <br><b>
  2763. Checking for a used capture group by number
  2764. </b><br>
  2765. <P>
  2766. If the text between the parentheses consists of a sequence of digits, the
  2767. condition is true if a capture group of that number has previously matched. If
  2768. there is more than one capture group with the same number (see the earlier
  2769. <a href="#recursion">section about duplicate group numbers),</a>
  2770. the condition is true if any of them have matched. An alternative notation,
  2771. which is a PCRE2 extension, not supported by Perl, is to precede the digits
  2772. with a plus or minus sign. In this case, the group number is relative rather
  2773. than absolute. The most recently opened capture group (which could be enclosing
  2774. this condition) can be referenced by (?(-1), the next most recent by (?(-2),
  2775. and so on. Inside loops it can also make sense to refer to subsequent groups.
  2776. The next capture group to be opened can be referenced as (?(+1), and so on. The
  2777. value zero in any of these forms is not used; it provokes a compile-time error.
  2778. </P>
  2779. <P>
  2780. Consider the following pattern, which contains non-significant white space to
  2781. make it more readable (assume the PCRE2_EXTENDED option) and to divide it into
  2782. three parts for ease of discussion:
  2783. <pre>
  2784. ( \( )? [^()]+ (?(1) \) )
  2785. </pre>
  2786. The first part matches an optional opening parenthesis, and if that
  2787. character is present, sets it as the first captured substring. The second part
  2788. matches one or more characters that are not parentheses. The third part is a
  2789. conditional group that tests whether or not the first capture group
  2790. matched. If it did, that is, if subject started with an opening parenthesis,
  2791. the condition is true, and so the yes-pattern is executed and a closing
  2792. parenthesis is required. Otherwise, since no-pattern is not present, the
  2793. conditional group matches nothing. In other words, this pattern matches a
  2794. sequence of non-parentheses, optionally enclosed in parentheses.
  2795. </P>
  2796. <P>
  2797. If you were embedding this pattern in a larger one, you could use a relative
  2798. reference:
  2799. <pre>
  2800. ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
  2801. </pre>
  2802. This makes the fragment independent of the parentheses in the larger pattern.
  2803. </P>
  2804. <br><b>
  2805. Checking for a used capture group by name
  2806. </b><br>
  2807. <P>
  2808. Perl uses the syntax (?(&#60;name&#62;)...) or (?('name')...) to test for a used
  2809. capture group by name. For compatibility with earlier versions of PCRE1, which
  2810. had this facility before Perl, the syntax (?(name)...) is also recognized.
  2811. Note, however, that undelimited names consisting of the letter R followed by
  2812. digits are ambiguous (see the following section). Rewriting the above example
  2813. to use a named group gives this:
  2814. <pre>
  2815. (?&#60;OPEN&#62; \( )? [^()]+ (?(&#60;OPEN&#62;) \) )
  2816. </pre>
  2817. If the name used in a condition of this kind is a duplicate, the test is
  2818. applied to all groups of the same name, and is true if any one of them has
  2819. matched.
  2820. </P>
  2821. <br><b>
  2822. Checking for pattern recursion
  2823. </b><br>
  2824. <P>
  2825. "Recursion" in this sense refers to any subroutine-like call from one part of
  2826. the pattern to another, whether or not it is actually recursive. See the
  2827. sections entitled
  2828. <a href="#recursion">"Recursive patterns"</a>
  2829. and
  2830. <a href="#groupsassubroutines">"Groups as subroutines"</a>
  2831. below for details of recursion and subroutine calls.
  2832. </P>
  2833. <P>
  2834. If a condition is the string (R), and there is no capture group with the name
  2835. R, the condition is true if matching is currently in a recursion or subroutine
  2836. call to the whole pattern or any capture group. If digits follow the letter R,
  2837. and there is no group with that name, the condition is true if the most recent
  2838. call is into a group with the given number, which must exist somewhere in the
  2839. overall pattern. This is a contrived example that is equivalent to a+b:
  2840. <pre>
  2841. ((?(R1)a+|(?1)b))
  2842. </pre>
  2843. However, in both cases, if there is a capture group with a matching name, the
  2844. condition tests for its being set, as described in the section above, instead
  2845. of testing for recursion. For example, creating a group with the name R1 by
  2846. adding (?&#60;R1&#62;) to the above pattern completely changes its meaning.
  2847. </P>
  2848. <P>
  2849. If a name preceded by ampersand follows the letter R, for example:
  2850. <pre>
  2851. (?(R&name)...)
  2852. </pre>
  2853. the condition is true if the most recent recursion is into a group of that name
  2854. (which must exist within the pattern).
  2855. </P>
  2856. <P>
  2857. This condition does not check the entire recursion stack. It tests only the
  2858. current level. If the name used in a condition of this kind is a duplicate, the
  2859. test is applied to all groups of the same name, and is true if any one of
  2860. them is the most recent recursion.
  2861. </P>
  2862. <P>
  2863. At "top level", all these recursion test conditions are false.
  2864. <a name="subdefine"></a></P>
  2865. <br><b>
  2866. Defining capture groups for use by reference only
  2867. </b><br>
  2868. <P>
  2869. If the condition is the string (DEFINE), the condition is always false, even if
  2870. there is a group with the name DEFINE. In this case, there may be only one
  2871. alternative in the rest of the conditional group. It is always skipped if
  2872. control reaches this point in the pattern; the idea of DEFINE is that it can be
  2873. used to define subroutines that can be referenced from elsewhere. (The use of
  2874. <a href="#groupsassubroutines">subroutines</a>
  2875. is described below.) For example, a pattern to match an IPv4 address such as
  2876. "192.168.23.245" could be written like this (ignore white space and line
  2877. breaks):
  2878. <pre>
  2879. (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
  2880. \b (?&byte) (\.(?&byte)){3} \b
  2881. </pre>
  2882. The first part of the pattern is a DEFINE group inside which another group
  2883. named "byte" is defined. This matches an individual component of an IPv4
  2884. address (a number less than 256). When matching takes place, this part of the
  2885. pattern is skipped because DEFINE acts like a false condition. The rest of the
  2886. pattern uses references to the named group to match the four dot-separated
  2887. components of an IPv4 address, insisting on a word boundary at each end.
  2888. </P>
  2889. <br><b>
  2890. Checking the PCRE2 version
  2891. </b><br>
  2892. <P>
  2893. Programs that link with a PCRE2 library can check the version by calling
  2894. <b>pcre2_config()</b> with appropriate arguments. Users of applications that do
  2895. not have access to the underlying code cannot do this. A special "condition"
  2896. called VERSION exists to allow such users to discover which version of PCRE2
  2897. they are dealing with by using this condition to match a string such as
  2898. "yesno". VERSION must be followed either by "=" or "&#62;=" and a version number.
  2899. For example:
  2900. <pre>
  2901. (?(VERSION&#62;=10.4)yes|no)
  2902. </pre>
  2903. This pattern matches "yes" if the PCRE2 version is greater or equal to 10.4, or
  2904. "no" otherwise. The fractional part of the version number may not contain more
  2905. than two digits.
  2906. </P>
  2907. <br><b>
  2908. Assertion conditions
  2909. </b><br>
  2910. <P>
  2911. If the condition is not in any of the above formats, it must be a parenthesized
  2912. assertion. This may be a positive or negative lookahead or lookbehind
  2913. assertion. However, it must be a traditional atomic assertion, not one of the
  2914. <a href="#nonatomicassertions">non-atomic assertions.</a>
  2915. </P>
  2916. <P>
  2917. Consider this pattern, again containing non-significant white space, and with
  2918. the two alternatives on the second line:
  2919. <pre>
  2920. (?(?=[^a-z]*[a-z])
  2921. \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
  2922. </pre>
  2923. The condition is a positive lookahead assertion that matches an optional
  2924. sequence of non-letters followed by a letter. In other words, it tests for the
  2925. presence of at least one letter in the subject. If a letter is found, the
  2926. subject is matched against the first alternative; otherwise it is matched
  2927. against the second. This pattern matches strings in one of the two forms
  2928. dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
  2929. </P>
  2930. <P>
  2931. When an assertion that is a condition contains capture groups, any
  2932. capturing that occurs in a matching branch is retained afterwards, for both
  2933. positive and negative assertions, because matching always continues after the
  2934. assertion, whether it succeeds or fails. (Compare non-conditional assertions,
  2935. for which captures are retained only for positive assertions that succeed.)
  2936. <a name="comments"></a></P>
  2937. <br><a name="SEC24" href="#TOC1">COMMENTS</a><br>
  2938. <P>
  2939. There are two ways of including comments in patterns that are processed by
  2940. PCRE2. In both cases, the start of the comment must not be in a character
  2941. class, nor in the middle of any other sequence of related characters such as
  2942. (?: or a group name or number. The characters that make up a comment play
  2943. no part in the pattern matching.
  2944. </P>
  2945. <P>
  2946. The sequence (?# marks the start of a comment that continues up to the next
  2947. closing parenthesis. Nested parentheses are not permitted. If the
  2948. PCRE2_EXTENDED or PCRE2_EXTENDED_MORE option is set, an unescaped # character
  2949. also introduces a comment, which in this case continues to immediately after
  2950. the next newline character or character sequence in the pattern. Which
  2951. characters are interpreted as newlines is controlled by an option passed to the
  2952. compiling function or by a special sequence at the start of the pattern, as
  2953. described in the section entitled
  2954. <a href="#newlines">"Newline conventions"</a>
  2955. above. Note that the end of this type of comment is a literal newline sequence
  2956. in the pattern; escape sequences that happen to represent a newline do not
  2957. count. For example, consider this pattern when PCRE2_EXTENDED is set, and the
  2958. default newline convention (a single linefeed character) is in force:
  2959. <pre>
  2960. abc #comment \n still comment
  2961. </pre>
  2962. On encountering the # character, <b>pcre2_compile()</b> skips along, looking for
  2963. a newline in the pattern. The sequence \n is still literal at this stage, so
  2964. it does not terminate the comment. Only an actual character with the code value
  2965. 0x0a (the default newline) does so.
  2966. <a name="recursion"></a></P>
  2967. <br><a name="SEC25" href="#TOC1">RECURSIVE PATTERNS</a><br>
  2968. <P>
  2969. Consider the problem of matching a string in parentheses, allowing for
  2970. unlimited nested parentheses. Without the use of recursion, the best that can
  2971. be done is to use a pattern that matches up to some fixed depth of nesting. It
  2972. is not possible to handle an arbitrary nesting depth.
  2973. </P>
  2974. <P>
  2975. For some time, Perl has provided a facility that allows regular expressions to
  2976. recurse (amongst other things). It does this by interpolating Perl code in the
  2977. expression at run time, and the code can refer to the expression itself. A Perl
  2978. pattern using code interpolation to solve the parentheses problem can be
  2979. created like this:
  2980. <pre>
  2981. $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
  2982. </pre>
  2983. The (?p{...}) item interpolates Perl code at run time, and in this case refers
  2984. recursively to the pattern in which it appears.
  2985. </P>
  2986. <P>
  2987. Obviously, PCRE2 cannot support the interpolation of Perl code. Instead, it
  2988. supports special syntax for recursion of the entire pattern, and also for
  2989. individual capture group recursion. After its introduction in PCRE1 and Python,
  2990. this kind of recursion was subsequently introduced into Perl at release 5.10.
  2991. </P>
  2992. <P>
  2993. A special item that consists of (? followed by a number greater than zero and a
  2994. closing parenthesis is a recursive subroutine call of the capture group of the
  2995. given number, provided that it occurs inside that group. (If not, it is a
  2996. <a href="#groupsassubroutines">non-recursive subroutine</a>
  2997. call, which is described in the next section.) The special item (?R) or (?0) is
  2998. a recursive call of the entire regular expression.
  2999. </P>
  3000. <P>
  3001. This PCRE2 pattern solves the nested parentheses problem (assume the
  3002. PCRE2_EXTENDED option is set so that white space is ignored):
  3003. <pre>
  3004. \( ( [^()]++ | (?R) )* \)
  3005. </pre>
  3006. First it matches an opening parenthesis. Then it matches any number of
  3007. substrings which can either be a sequence of non-parentheses, or a recursive
  3008. match of the pattern itself (that is, a correctly parenthesized substring).
  3009. Finally there is a closing parenthesis. Note the use of a possessive quantifier
  3010. to avoid backtracking into sequences of non-parentheses.
  3011. </P>
  3012. <P>
  3013. If this were part of a larger pattern, you would not want to recurse the entire
  3014. pattern, so instead you could use this:
  3015. <pre>
  3016. ( \( ( [^()]++ | (?1) )* \) )
  3017. </pre>
  3018. We have put the pattern into parentheses, and caused the recursion to refer to
  3019. them instead of the whole pattern.
  3020. </P>
  3021. <P>
  3022. In a larger pattern, keeping track of parenthesis numbers can be tricky. This
  3023. is made easier by the use of relative references. Instead of (?1) in the
  3024. pattern above you can write (?-2) to refer to the second most recently opened
  3025. parentheses preceding the recursion. In other words, a negative number counts
  3026. capturing parentheses leftwards from the point at which it is encountered.
  3027. </P>
  3028. <P>
  3029. Be aware however, that if
  3030. <a href="#dupgroupnumber">duplicate capture group numbers</a>
  3031. are in use, relative references refer to the earliest group with the
  3032. appropriate number. Consider, for example:
  3033. <pre>
  3034. (?|(a)|(b)) (c) (?-2)
  3035. </pre>
  3036. The first two capture groups (a) and (b) are both numbered 1, and group (c)
  3037. is number 2. When the reference (?-2) is encountered, the second most recently
  3038. opened parentheses has the number 1, but it is the first such group (the (a)
  3039. group) to which the recursion refers. This would be the same if an absolute
  3040. reference (?1) was used. In other words, relative references are just a
  3041. shorthand for computing a group number.
  3042. </P>
  3043. <P>
  3044. It is also possible to refer to subsequent capture groups, by writing
  3045. references such as (?+2). However, these cannot be recursive because the
  3046. reference is not inside the parentheses that are referenced. They are always
  3047. <a href="#groupsassubroutines">non-recursive subroutine</a>
  3048. calls, as described in the next section.
  3049. </P>
  3050. <P>
  3051. An alternative approach is to use named parentheses. The Perl syntax for this
  3052. is (?&name); PCRE1's earlier syntax (?P&#62;name) is also supported. We could
  3053. rewrite the above example as follows:
  3054. <pre>
  3055. (?&#60;pn&#62; \( ( [^()]++ | (?&pn) )* \) )
  3056. </pre>
  3057. If there is more than one group with the same name, the earliest one is
  3058. used.
  3059. </P>
  3060. <P>
  3061. The example pattern that we have been looking at contains nested unlimited
  3062. repeats, and so the use of a possessive quantifier for matching strings of
  3063. non-parentheses is important when applying the pattern to strings that do not
  3064. match. For example, when this pattern is applied to
  3065. <pre>
  3066. (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
  3067. </pre>
  3068. it yields "no match" quickly. However, if a possessive quantifier is not used,
  3069. the match runs for a very long time indeed because there are so many different
  3070. ways the + and * repeats can carve up the subject, and all have to be tested
  3071. before failure can be reported.
  3072. </P>
  3073. <P>
  3074. At the end of a match, the values of capturing parentheses are those from
  3075. the outermost level. If you want to obtain intermediate values, a callout
  3076. function can be used (see below and the
  3077. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3078. documentation). If the pattern above is matched against
  3079. <pre>
  3080. (ab(cd)ef)
  3081. </pre>
  3082. the value for the inner capturing parentheses (numbered 2) is "ef", which is
  3083. the last value taken on at the top level. If a capture group is not matched at
  3084. the top level, its final captured value is unset, even if it was (temporarily)
  3085. set at a deeper level during the matching process.
  3086. </P>
  3087. <P>
  3088. Do not confuse the (?R) item with the condition (R), which tests for recursion.
  3089. Consider this pattern, which matches text in angle brackets, allowing for
  3090. arbitrary nesting. Only digits are allowed in nested brackets (that is, when
  3091. recursing), whereas any characters are permitted at the outer level.
  3092. <pre>
  3093. &#60; (?: (?(R) \d++ | [^&#60;&#62;]*+) | (?R)) * &#62;
  3094. </pre>
  3095. In this pattern, (?(R) is the start of a conditional group, with two different
  3096. alternatives for the recursive and non-recursive cases. The (?R) item is the
  3097. actual recursive call.
  3098. <a name="recursiondifference"></a></P>
  3099. <br><b>
  3100. Differences in recursion processing between PCRE2 and Perl
  3101. </b><br>
  3102. <P>
  3103. Some former differences between PCRE2 and Perl no longer exist.
  3104. </P>
  3105. <P>
  3106. Before release 10.30, recursion processing in PCRE2 differed from Perl in that
  3107. a recursive subroutine call was always treated as an atomic group. That is,
  3108. once it had matched some of the subject string, it was never re-entered, even
  3109. if it contained untried alternatives and there was a subsequent matching
  3110. failure. (Historical note: PCRE implemented recursion before Perl did.)
  3111. </P>
  3112. <P>
  3113. Starting with release 10.30, recursive subroutine calls are no longer treated
  3114. as atomic. That is, they can be re-entered to try unused alternatives if there
  3115. is a matching failure later in the pattern. This is now compatible with the way
  3116. Perl works. If you want a subroutine call to be atomic, you must explicitly
  3117. enclose it in an atomic group.
  3118. </P>
  3119. <P>
  3120. Supporting backtracking into recursions simplifies certain types of recursive
  3121. pattern. For example, this pattern matches palindromic strings:
  3122. <pre>
  3123. ^((.)(?1)\2|.?)$
  3124. </pre>
  3125. The second branch in the group matches a single central character in the
  3126. palindrome when there are an odd number of characters, or nothing when there
  3127. are an even number of characters, but in order to work it has to be able to try
  3128. the second case when the rest of the pattern match fails. If you want to match
  3129. typical palindromic phrases, the pattern has to ignore all non-word characters,
  3130. which can be done like this:
  3131. <pre>
  3132. ^\W*+((.)\W*+(?1)\W*+\2|\W*+.?)\W*+$
  3133. </pre>
  3134. If run with the PCRE2_CASELESS option, this pattern matches phrases such as "A
  3135. man, a plan, a canal: Panama!". Note the use of the possessive quantifier *+ to
  3136. avoid backtracking into sequences of non-word characters. Without this, PCRE2
  3137. takes a great deal longer (ten times or more) to match typical phrases, and
  3138. Perl takes so long that you think it has gone into a loop.
  3139. </P>
  3140. <P>
  3141. Another way in which PCRE2 and Perl used to differ in their recursion
  3142. processing is in the handling of captured values. Formerly in Perl, when a
  3143. group was called recursively or as a subroutine (see the next section), it
  3144. had no access to any values that were captured outside the recursion, whereas
  3145. in PCRE2 these values can be referenced. Consider this pattern:
  3146. <pre>
  3147. ^(.)(\1|a(?2))
  3148. </pre>
  3149. This pattern matches "bab". The first capturing parentheses match "b", then in
  3150. the second group, when the backreference \1 fails to match "b", the second
  3151. alternative matches "a" and then recurses. In the recursion, \1 does now match
  3152. "b" and so the whole match succeeds. This match used to fail in Perl, but in
  3153. later versions (I tried 5.024) it now works.
  3154. <a name="groupsassubroutines"></a></P>
  3155. <br><a name="SEC26" href="#TOC1">GROUPS AS SUBROUTINES</a><br>
  3156. <P>
  3157. If the syntax for a recursive group call (either by number or by name) is used
  3158. outside the parentheses to which it refers, it operates a bit like a subroutine
  3159. in a programming language. More accurately, PCRE2 treats the referenced group
  3160. as an independent subpattern which it tries to match at the current matching
  3161. position. The called group may be defined before or after the reference. A
  3162. numbered reference can be absolute or relative, as in these examples:
  3163. <pre>
  3164. (...(absolute)...)...(?2)...
  3165. (...(relative)...)...(?-1)...
  3166. (...(?+1)...(relative)...
  3167. </pre>
  3168. An earlier example pointed out that the pattern
  3169. <pre>
  3170. (sens|respons)e and \1ibility
  3171. </pre>
  3172. matches "sense and sensibility" and "response and responsibility", but not
  3173. "sense and responsibility". If instead the pattern
  3174. <pre>
  3175. (sens|respons)e and (?1)ibility
  3176. </pre>
  3177. is used, it does match "sense and responsibility" as well as the other two
  3178. strings. Another example is given in the discussion of DEFINE above.
  3179. </P>
  3180. <P>
  3181. Like recursions, subroutine calls used to be treated as atomic, but this
  3182. changed at PCRE2 release 10.30, so backtracking into subroutine calls can now
  3183. occur. However, any capturing parentheses that are set during the subroutine
  3184. call revert to their previous values afterwards.
  3185. </P>
  3186. <P>
  3187. Processing options such as case-independence are fixed when a group is
  3188. defined, so if it is used as a subroutine, such options cannot be changed for
  3189. different calls. For example, consider this pattern:
  3190. <pre>
  3191. (abc)(?i:(?-1))
  3192. </pre>
  3193. It matches "abcabc". It does not match "abcABC" because the change of
  3194. processing option does not affect the called group.
  3195. </P>
  3196. <P>
  3197. The behaviour of
  3198. <a href="#backtrackcontrol">backtracking control verbs</a>
  3199. in groups when called as subroutines is described in the section entitled
  3200. <a href="#btsub">"Backtracking verbs in subroutines"</a>
  3201. below.
  3202. <a name="onigurumasubroutines"></a></P>
  3203. <br><a name="SEC27" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
  3204. <P>
  3205. For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
  3206. a number enclosed either in angle brackets or single quotes, is an alternative
  3207. syntax for calling a group as a subroutine, possibly recursively. Here are two
  3208. of the examples used above, rewritten using this syntax:
  3209. <pre>
  3210. (?&#60;pn&#62; \( ( (?&#62;[^()]+) | \g&#60;pn&#62; )* \) )
  3211. (sens|respons)e and \g'1'ibility
  3212. </pre>
  3213. PCRE2 supports an extension to Oniguruma: if a number is preceded by a
  3214. plus or a minus sign it is taken as a relative reference. For example:
  3215. <pre>
  3216. (abc)(?i:\g&#60;-1&#62;)
  3217. </pre>
  3218. Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
  3219. synonymous. The former is a backreference; the latter is a subroutine call.
  3220. </P>
  3221. <br><a name="SEC28" href="#TOC1">CALLOUTS</a><br>
  3222. <P>
  3223. Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
  3224. code to be obeyed in the middle of matching a regular expression. This makes it
  3225. possible, amongst other things, to extract different substrings that match the
  3226. same pair of parentheses when there is a repetition.
  3227. </P>
  3228. <P>
  3229. PCRE2 provides a similar feature, but of course it cannot obey arbitrary Perl
  3230. code. The feature is called "callout". The caller of PCRE2 provides an external
  3231. function by putting its entry point in a match context using the function
  3232. <b>pcre2_set_callout()</b>, and then passing that context to <b>pcre2_match()</b>
  3233. or <b>pcre2_dfa_match()</b>. If no match context is passed, or if the callout
  3234. entry point is set to NULL, callouts are disabled.
  3235. </P>
  3236. <P>
  3237. Within a regular expression, (?C&#60;arg&#62;) indicates a point at which the external
  3238. function is to be called. There are two kinds of callout: those with a
  3239. numerical argument and those with a string argument. (?C) on its own with no
  3240. argument is treated as (?C0). A numerical argument allows the application to
  3241. distinguish between different callouts. String arguments were added for release
  3242. 10.20 to make it possible for script languages that use PCRE2 to embed short
  3243. scripts within patterns in a similar way to Perl.
  3244. </P>
  3245. <P>
  3246. During matching, when PCRE2 reaches a callout point, the external function is
  3247. called. It is provided with the number or string argument of the callout, the
  3248. position in the pattern, and one item of data that is also set in the match
  3249. block. The callout function may cause matching to proceed, to backtrack, or to
  3250. fail.
  3251. </P>
  3252. <P>
  3253. By default, PCRE2 implements a number of optimizations at matching time, and
  3254. one side-effect is that sometimes callouts are skipped. If you need all
  3255. possible callouts to happen, you need to set options that disable the relevant
  3256. optimizations. More details, including a complete description of the
  3257. programming interface to the callout function, are given in the
  3258. <a href="pcre2callout.html"><b>pcre2callout</b></a>
  3259. documentation.
  3260. </P>
  3261. <br><b>
  3262. Callouts with numerical arguments
  3263. </b><br>
  3264. <P>
  3265. If you just want to have a means of identifying different callout points, put a
  3266. number less than 256 after the letter C. For example, this pattern has two
  3267. callout points:
  3268. <pre>
  3269. (?C1)abc(?C2)def
  3270. </pre>
  3271. If the PCRE2_AUTO_CALLOUT flag is passed to <b>pcre2_compile()</b>, numerical
  3272. callouts are automatically installed before each item in the pattern. They are
  3273. all numbered 255. If there is a conditional group in the pattern whose
  3274. condition is an assertion, an additional callout is inserted just before the
  3275. condition. An explicit callout may also be set at this position, as in this
  3276. example:
  3277. <pre>
  3278. (?(?C9)(?=a)abc|def)
  3279. </pre>
  3280. Note that this applies only to assertion conditions, not to other types of
  3281. condition.
  3282. </P>
  3283. <br><b>
  3284. Callouts with string arguments
  3285. </b><br>
  3286. <P>
  3287. A delimited string may be used instead of a number as a callout argument. The
  3288. starting delimiter must be one of ` ' " ^ % # $ { and the ending delimiter is
  3289. the same as the start, except for {, where the ending delimiter is }. If the
  3290. ending delimiter is needed within the string, it must be doubled. For
  3291. example:
  3292. <pre>
  3293. (?C'ab ''c'' d')xyz(?C{any text})pqr
  3294. </pre>
  3295. The doubling is removed before the string is passed to the callout function.
  3296. <a name="backtrackcontrol"></a></P>
  3297. <br><a name="SEC29" href="#TOC1">BACKTRACKING CONTROL</a><br>
  3298. <P>
  3299. There are a number of special "Backtracking Control Verbs" (to use Perl's
  3300. terminology) that modify the behaviour of backtracking during matching. They
  3301. are generally of the form (*VERB) or (*VERB:NAME). Some verbs take either form,
  3302. and may behave differently depending on whether or not a name argument is
  3303. present. The names are not required to be unique within the pattern.
  3304. </P>
  3305. <P>
  3306. By default, for compatibility with Perl, a name is any sequence of characters
  3307. that does not include a closing parenthesis. The name is not processed in
  3308. any way, and it is not possible to include a closing parenthesis in the name.
  3309. This can be changed by setting the PCRE2_ALT_VERBNAMES option, but the result
  3310. is no longer Perl-compatible.
  3311. </P>
  3312. <P>
  3313. When PCRE2_ALT_VERBNAMES is set, backslash processing is applied to verb names
  3314. and only an unescaped closing parenthesis terminates the name. However, the
  3315. only backslash items that are permitted are \Q, \E, and sequences such as
  3316. \x{100} that define character code points. Character type escapes such as \d
  3317. are faulted.
  3318. </P>
  3319. <P>
  3320. A closing parenthesis can be included in a name either as \) or between \Q
  3321. and \E. In addition to backslash processing, if the PCRE2_EXTENDED or
  3322. PCRE2_EXTENDED_MORE option is also set, unescaped whitespace in verb names is
  3323. skipped, and #-comments are recognized, exactly as in the rest of the pattern.
  3324. PCRE2_EXTENDED and PCRE2_EXTENDED_MORE do not affect verb names unless
  3325. PCRE2_ALT_VERBNAMES is also set.
  3326. </P>
  3327. <P>
  3328. The maximum length of a name is 255 in the 8-bit library and 65535 in the
  3329. 16-bit and 32-bit libraries. If the name is empty, that is, if the closing
  3330. parenthesis immediately follows the colon, the effect is as if the colon were
  3331. not there. Any number of these verbs may occur in a pattern. Except for
  3332. (*ACCEPT), they may not be quantified.
  3333. </P>
  3334. <P>
  3335. Since these verbs are specifically related to backtracking, most of them can be
  3336. used only when the pattern is to be matched using the traditional matching
  3337. function, because that uses a backtracking algorithm. With the exception of
  3338. (*FAIL), which behaves like a failing negative assertion, the backtracking
  3339. control verbs cause an error if encountered by the DFA matching function.
  3340. </P>
  3341. <P>
  3342. The behaviour of these verbs in
  3343. <a href="#btrepeat">repeated groups,</a>
  3344. <a href="#btassert">assertions,</a>
  3345. and in
  3346. <a href="#btsub">capture groups called as subroutines</a>
  3347. (whether or not recursively) is documented below.
  3348. <a name="nooptimize"></a></P>
  3349. <br><b>
  3350. Optimizations that affect backtracking verbs
  3351. </b><br>
  3352. <P>
  3353. PCRE2 contains some optimizations that are used to speed up matching by running
  3354. some checks at the start of each match attempt. For example, it may know the
  3355. minimum length of matching subject, or that a particular character must be
  3356. present. When one of these optimizations bypasses the running of a match, any
  3357. included backtracking verbs will not, of course, be processed. You can suppress
  3358. the start-of-match optimizations by setting the PCRE2_NO_START_OPTIMIZE option
  3359. when calling <b>pcre2_compile()</b>, or by starting the pattern with
  3360. (*NO_START_OPT). There is more discussion of this option in the section
  3361. entitled
  3362. <a href="pcre2api.html#compiling">"Compiling a pattern"</a>
  3363. in the
  3364. <a href="pcre2api.html"><b>pcre2api</b></a>
  3365. documentation.
  3366. </P>
  3367. <P>
  3368. Experiments with Perl suggest that it too has similar optimizations, and like
  3369. PCRE2, turning them off can change the result of a match.
  3370. <a name="acceptverb"></a></P>
  3371. <br><b>
  3372. Verbs that act immediately
  3373. </b><br>
  3374. <P>
  3375. The following verbs act as soon as they are encountered.
  3376. <pre>
  3377. (*ACCEPT) or (*ACCEPT:NAME)
  3378. </pre>
  3379. This verb causes the match to end successfully, skipping the remainder of the
  3380. pattern. However, when it is inside a capture group that is called as a
  3381. subroutine, only that group is ended successfully. Matching then continues
  3382. at the outer level. If (*ACCEPT) in triggered in a positive assertion, the
  3383. assertion succeeds; in a negative assertion, the assertion fails.
  3384. </P>
  3385. <P>
  3386. If (*ACCEPT) is inside capturing parentheses, the data so far is captured. For
  3387. example:
  3388. <pre>
  3389. A((?:A|B(*ACCEPT)|C)D)
  3390. </pre>
  3391. This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
  3392. the outer parentheses.
  3393. </P>
  3394. <P>
  3395. (*ACCEPT) is the only backtracking verb that is allowed to be quantified
  3396. because an ungreedy quantification with a minimum of zero acts only when a
  3397. backtrack happens. Consider, for example,
  3398. <pre>
  3399. (A(*ACCEPT)??B)C
  3400. </pre>
  3401. where A, B, and C may be complex expressions. After matching "A", the matcher
  3402. processes "BC"; if that fails, causing a backtrack, (*ACCEPT) is triggered and
  3403. the match succeeds. In both cases, all but C is captured. Whereas (*COMMIT)
  3404. (see below) means "fail on backtrack", a repeated (*ACCEPT) of this type means
  3405. "succeed on backtrack".
  3406. </P>
  3407. <P>
  3408. <b>Warning:</b> (*ACCEPT) should not be used within a script run group, because
  3409. it causes an immediate exit from the group, bypassing the script run checking.
  3410. <pre>
  3411. (*FAIL) or (*FAIL:NAME)
  3412. </pre>
  3413. This verb causes a matching failure, forcing backtracking to occur. It may be
  3414. abbreviated to (*F). It is equivalent to (?!) but easier to read. The Perl
  3415. documentation notes that it is probably useful only when combined with (?{}) or
  3416. (??{}). Those are, of course, Perl features that are not present in PCRE2. The
  3417. nearest equivalent is the callout feature, as for example in this pattern:
  3418. <pre>
  3419. a+(?C)(*FAIL)
  3420. </pre>
  3421. A match with the string "aaaa" always fails, but the callout is taken before
  3422. each backtrack happens (in this example, 10 times).
  3423. </P>
  3424. <P>
  3425. (*ACCEPT:NAME) and (*FAIL:NAME) behave the same as (*MARK:NAME)(*ACCEPT) and
  3426. (*MARK:NAME)(*FAIL), respectively, that is, a (*MARK) is recorded just before
  3427. the verb acts.
  3428. </P>
  3429. <br><b>
  3430. Recording which path was taken
  3431. </b><br>
  3432. <P>
  3433. There is one verb whose main purpose is to track how a match was arrived at,
  3434. though it also has a secondary use in conjunction with advancing the match
  3435. starting point (see (*SKIP) below).
  3436. <pre>
  3437. (*MARK:NAME) or (*:NAME)
  3438. </pre>
  3439. A name is always required with this verb. For all the other backtracking
  3440. control verbs, a NAME argument is optional.
  3441. </P>
  3442. <P>
  3443. When a match succeeds, the name of the last-encountered mark name on the
  3444. matching path is passed back to the caller as described in the section entitled
  3445. <a href="pcre2api.html#matchotherdata">"Other information about the match"</a>
  3446. in the
  3447. <a href="pcre2api.html"><b>pcre2api</b></a>
  3448. documentation. This applies to all instances of (*MARK) and other verbs,
  3449. including those inside assertions and atomic groups. However, there are
  3450. differences in those cases when (*MARK) is used in conjunction with (*SKIP) as
  3451. described below.
  3452. </P>
  3453. <P>
  3454. The mark name that was last encountered on the matching path is passed back. A
  3455. verb without a NAME argument is ignored for this purpose. Here is an example of
  3456. <b>pcre2test</b> output, where the "mark" modifier requests the retrieval and
  3457. outputting of (*MARK) data:
  3458. <pre>
  3459. re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/mark
  3460. data&#62; XY
  3461. 0: XY
  3462. MK: A
  3463. XZ
  3464. 0: XZ
  3465. MK: B
  3466. </pre>
  3467. The (*MARK) name is tagged with "MK:" in this output, and in this example it
  3468. indicates which of the two alternatives matched. This is a more efficient way
  3469. of obtaining this information than putting each alternative in its own
  3470. capturing parentheses.
  3471. </P>
  3472. <P>
  3473. If a verb with a name is encountered in a positive assertion that is true, the
  3474. name is recorded and passed back if it is the last-encountered. This does not
  3475. happen for negative assertions or failing positive assertions.
  3476. </P>
  3477. <P>
  3478. After a partial match or a failed match, the last encountered name in the
  3479. entire match process is returned. For example:
  3480. <pre>
  3481. re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/mark
  3482. data&#62; XP
  3483. No match, mark = B
  3484. </pre>
  3485. Note that in this unanchored example the mark is retained from the match
  3486. attempt that started at the letter "X" in the subject. Subsequent match
  3487. attempts starting at "P" and then with an empty string do not get as far as the
  3488. (*MARK) item, but nevertheless do not reset it.
  3489. </P>
  3490. <P>
  3491. If you are interested in (*MARK) values after failed matches, you should
  3492. probably set the PCRE2_NO_START_OPTIMIZE option
  3493. <a href="#nooptimize">(see above)</a>
  3494. to ensure that the match is always attempted.
  3495. </P>
  3496. <br><b>
  3497. Verbs that act after backtracking
  3498. </b><br>
  3499. <P>
  3500. The following verbs do nothing when they are encountered. Matching continues
  3501. with what follows, but if there is a subsequent match failure, causing a
  3502. backtrack to the verb, a failure is forced. That is, backtracking cannot pass
  3503. to the left of the verb. However, when one of these verbs appears inside an
  3504. atomic group or in a lookaround assertion that is true, its effect is confined
  3505. to that group, because once the group has been matched, there is never any
  3506. backtracking into it. Backtracking from beyond an assertion or an atomic group
  3507. ignores the entire group, and seeks a preceding backtracking point.
  3508. </P>
  3509. <P>
  3510. These verbs differ in exactly what kind of failure occurs when backtracking
  3511. reaches them. The behaviour described below is what happens when the verb is
  3512. not in a subroutine or an assertion. Subsequent sections cover these special
  3513. cases.
  3514. <pre>
  3515. (*COMMIT) or (*COMMIT:NAME)
  3516. </pre>
  3517. This verb causes the whole match to fail outright if there is a later matching
  3518. failure that causes backtracking to reach it. Even if the pattern is
  3519. unanchored, no further attempts to find a match by advancing the starting point
  3520. take place. If (*COMMIT) is the only backtracking verb that is encountered,
  3521. once it has been passed <b>pcre2_match()</b> is committed to finding a match at
  3522. the current starting point, or not at all. For example:
  3523. <pre>
  3524. a+(*COMMIT)b
  3525. </pre>
  3526. This matches "xxaab" but not "aacaab". It can be thought of as a kind of
  3527. dynamic anchor, or "I've started, so I must finish."
  3528. </P>
  3529. <P>
  3530. The behaviour of (*COMMIT:NAME) is not the same as (*MARK:NAME)(*COMMIT). It is
  3531. like (*MARK:NAME) in that the name is remembered for passing back to the
  3532. caller. However, (*SKIP:NAME) searches only for names that are set with
  3533. (*MARK), ignoring those set by any of the other backtracking verbs.
  3534. </P>
  3535. <P>
  3536. If there is more than one backtracking verb in a pattern, a different one that
  3537. follows (*COMMIT) may be triggered first, so merely passing (*COMMIT) during a
  3538. match does not always guarantee that a match must be at this starting point.
  3539. </P>
  3540. <P>
  3541. Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
  3542. unless PCRE2's start-of-match optimizations are turned off, as shown in this
  3543. output from <b>pcre2test</b>:
  3544. <pre>
  3545. re&#62; /(*COMMIT)abc/
  3546. data&#62; xyzabc
  3547. 0: abc
  3548. data&#62;
  3549. re&#62; /(*COMMIT)abc/no_start_optimize
  3550. data&#62; xyzabc
  3551. No match
  3552. </pre>
  3553. For the first pattern, PCRE2 knows that any match must start with "a", so the
  3554. optimization skips along the subject to "a" before applying the pattern to the
  3555. first set of data. The match attempt then succeeds. The second pattern disables
  3556. the optimization that skips along to the first character. The pattern is now
  3557. applied starting at "x", and so the (*COMMIT) causes the match to fail without
  3558. trying any other starting points.
  3559. <pre>
  3560. (*PRUNE) or (*PRUNE:NAME)
  3561. </pre>
  3562. This verb causes the match to fail at the current starting position in the
  3563. subject if there is a later matching failure that causes backtracking to reach
  3564. it. If the pattern is unanchored, the normal "bumpalong" advance to the next
  3565. starting character then happens. Backtracking can occur as usual to the left of
  3566. (*PRUNE), before it is reached, or when matching to the right of (*PRUNE), but
  3567. if there is no match to the right, backtracking cannot cross (*PRUNE). In
  3568. simple cases, the use of (*PRUNE) is just an alternative to an atomic group or
  3569. possessive quantifier, but there are some uses of (*PRUNE) that cannot be
  3570. expressed in any other way. In an anchored pattern (*PRUNE) has the same effect
  3571. as (*COMMIT).
  3572. </P>
  3573. <P>
  3574. The behaviour of (*PRUNE:NAME) is not the same as (*MARK:NAME)(*PRUNE). It is
  3575. like (*MARK:NAME) in that the name is remembered for passing back to the
  3576. caller. However, (*SKIP:NAME) searches only for names set with (*MARK),
  3577. ignoring those set by other backtracking verbs.
  3578. <pre>
  3579. (*SKIP)
  3580. </pre>
  3581. This verb, when given without a name, is like (*PRUNE), except that if the
  3582. pattern is unanchored, the "bumpalong" advance is not to the next character,
  3583. but to the position in the subject where (*SKIP) was encountered. (*SKIP)
  3584. signifies that whatever text was matched leading up to it cannot be part of a
  3585. successful match if there is a later mismatch. Consider:
  3586. <pre>
  3587. a+(*SKIP)b
  3588. </pre>
  3589. If the subject is "aaaac...", after the first match attempt fails (starting at
  3590. the first character in the string), the starting point skips on to start the
  3591. next attempt at "c". Note that a possessive quantifier does not have the same
  3592. effect as this example; although it would suppress backtracking during the
  3593. first match attempt, the second attempt would start at the second character
  3594. instead of skipping on to "c".
  3595. </P>
  3596. <P>
  3597. If (*SKIP) is used to specify a new starting position that is the same as the
  3598. starting position of the current match, or (by being inside a lookbehind)
  3599. earlier, the position specified by (*SKIP) is ignored, and instead the normal
  3600. "bumpalong" occurs.
  3601. <pre>
  3602. (*SKIP:NAME)
  3603. </pre>
  3604. When (*SKIP) has an associated name, its behaviour is modified. When such a
  3605. (*SKIP) is triggered, the previous path through the pattern is searched for the
  3606. most recent (*MARK) that has the same name. If one is found, the "bumpalong"
  3607. advance is to the subject position that corresponds to that (*MARK) instead of
  3608. to where (*SKIP) was encountered. If no (*MARK) with a matching name is found,
  3609. the (*SKIP) is ignored.
  3610. </P>
  3611. <P>
  3612. The search for a (*MARK) name uses the normal backtracking mechanism, which
  3613. means that it does not see (*MARK) settings that are inside atomic groups or
  3614. assertions, because they are never re-entered by backtracking. Compare the
  3615. following <b>pcre2test</b> examples:
  3616. <pre>
  3617. re&#62; /a(?&#62;(*MARK:X))(*SKIP:X)(*F)|(.)/
  3618. data: abc
  3619. 0: a
  3620. 1: a
  3621. data:
  3622. re&#62; /a(?:(*MARK:X))(*SKIP:X)(*F)|(.)/
  3623. data: abc
  3624. 0: b
  3625. 1: b
  3626. </pre>
  3627. In the first example, the (*MARK) setting is in an atomic group, so it is not
  3628. seen when (*SKIP:X) triggers, causing the (*SKIP) to be ignored. This allows
  3629. the second branch of the pattern to be tried at the first character position.
  3630. In the second example, the (*MARK) setting is not in an atomic group. This
  3631. allows (*SKIP:X) to find the (*MARK) when it backtracks, and this causes a new
  3632. matching attempt to start at the second character. This time, the (*MARK) is
  3633. never seen because "a" does not match "b", so the matcher immediately jumps to
  3634. the second branch of the pattern.
  3635. </P>
  3636. <P>
  3637. Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It ignores
  3638. names that are set by other backtracking verbs.
  3639. <pre>
  3640. (*THEN) or (*THEN:NAME)
  3641. </pre>
  3642. This verb causes a skip to the next innermost alternative when backtracking
  3643. reaches it. That is, it cancels any further backtracking within the current
  3644. alternative. Its name comes from the observation that it can be used for a
  3645. pattern-based if-then-else block:
  3646. <pre>
  3647. ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
  3648. </pre>
  3649. If the COND1 pattern matches, FOO is tried (and possibly further items after
  3650. the end of the group if FOO succeeds); on failure, the matcher skips to the
  3651. second alternative and tries COND2, without backtracking into COND1. If that
  3652. succeeds and BAR fails, COND3 is tried. If subsequently BAZ fails, there are no
  3653. more alternatives, so there is a backtrack to whatever came before the entire
  3654. group. If (*THEN) is not inside an alternation, it acts like (*PRUNE).
  3655. </P>
  3656. <P>
  3657. The behaviour of (*THEN:NAME) is not the same as (*MARK:NAME)(*THEN). It is
  3658. like (*MARK:NAME) in that the name is remembered for passing back to the
  3659. caller. However, (*SKIP:NAME) searches only for names set with (*MARK),
  3660. ignoring those set by other backtracking verbs.
  3661. </P>
  3662. <P>
  3663. A group that does not contain a | character is just a part of the enclosing
  3664. alternative; it is not a nested alternation with only one alternative. The
  3665. effect of (*THEN) extends beyond such a group to the enclosing alternative.
  3666. Consider this pattern, where A, B, etc. are complex pattern fragments that do
  3667. not contain any | characters at this level:
  3668. <pre>
  3669. A (B(*THEN)C) | D
  3670. </pre>
  3671. If A and B are matched, but there is a failure in C, matching does not
  3672. backtrack into A; instead it moves to the next alternative, that is, D.
  3673. However, if the group containing (*THEN) is given an alternative, it
  3674. behaves differently:
  3675. <pre>
  3676. A (B(*THEN)C | (*FAIL)) | D
  3677. </pre>
  3678. The effect of (*THEN) is now confined to the inner group. After a failure in C,
  3679. matching moves to (*FAIL), which causes the whole group to fail because there
  3680. are no more alternatives to try. In this case, matching does backtrack into A.
  3681. </P>
  3682. <P>
  3683. Note that a conditional group is not considered as having two alternatives,
  3684. because only one is ever used. In other words, the | character in a conditional
  3685. group has a different meaning. Ignoring white space, consider:
  3686. <pre>
  3687. ^.*? (?(?=a) a | b(*THEN)c )
  3688. </pre>
  3689. If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
  3690. it initially matches zero characters. The condition (?=a) then fails, the
  3691. character "b" is matched, but "c" is not. At this point, matching does not
  3692. backtrack to .*? as might perhaps be expected from the presence of the |
  3693. character. The conditional group is part of the single alternative that
  3694. comprises the whole pattern, and so the match fails. (If there was a backtrack
  3695. into .*?, allowing it to match "b", the match would succeed.)
  3696. </P>
  3697. <P>
  3698. The verbs just described provide four different "strengths" of control when
  3699. subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
  3700. next alternative. (*PRUNE) comes next, failing the match at the current
  3701. starting position, but allowing an advance to the next character (for an
  3702. unanchored pattern). (*SKIP) is similar, except that the advance may be more
  3703. than one character. (*COMMIT) is the strongest, causing the entire match to
  3704. fail.
  3705. </P>
  3706. <br><b>
  3707. More than one backtracking verb
  3708. </b><br>
  3709. <P>
  3710. If more than one backtracking verb is present in a pattern, the one that is
  3711. backtracked onto first acts. For example, consider this pattern, where A, B,
  3712. etc. are complex pattern fragments:
  3713. <pre>
  3714. (A(*COMMIT)B(*THEN)C|ABD)
  3715. </pre>
  3716. If A matches but B fails, the backtrack to (*COMMIT) causes the entire match to
  3717. fail. However, if A and B match, but C fails, the backtrack to (*THEN) causes
  3718. the next alternative (ABD) to be tried. This behaviour is consistent, but is
  3719. not always the same as Perl's. It means that if two or more backtracking verbs
  3720. appear in succession, all but the last of them has no effect. Consider this
  3721. example:
  3722. <pre>
  3723. ...(*COMMIT)(*PRUNE)...
  3724. </pre>
  3725. If there is a matching failure to the right, backtracking onto (*PRUNE) causes
  3726. it to be triggered, and its action is taken. There can never be a backtrack
  3727. onto (*COMMIT).
  3728. <a name="btrepeat"></a></P>
  3729. <br><b>
  3730. Backtracking verbs in repeated groups
  3731. </b><br>
  3732. <P>
  3733. PCRE2 sometimes differs from Perl in its handling of backtracking verbs in
  3734. repeated groups. For example, consider:
  3735. <pre>
  3736. /(a(*COMMIT)b)+ac/
  3737. </pre>
  3738. If the subject is "abac", Perl matches unless its optimizations are disabled,
  3739. but PCRE2 always fails because the (*COMMIT) in the second repeat of the group
  3740. acts.
  3741. <a name="btassert"></a></P>
  3742. <br><b>
  3743. Backtracking verbs in assertions
  3744. </b><br>
  3745. <P>
  3746. (*FAIL) in any assertion has its normal effect: it forces an immediate
  3747. backtrack. The behaviour of the other backtracking verbs depends on whether or
  3748. not the assertion is standalone or acting as the condition in a conditional
  3749. group.
  3750. </P>
  3751. <P>
  3752. (*ACCEPT) in a standalone positive assertion causes the assertion to succeed
  3753. without any further processing; captured strings and a mark name (if set) are
  3754. retained. In a standalone negative assertion, (*ACCEPT) causes the assertion to
  3755. fail without any further processing; captured substrings and any mark name are
  3756. discarded.
  3757. </P>
  3758. <P>
  3759. If the assertion is a condition, (*ACCEPT) causes the condition to be true for
  3760. a positive assertion and false for a negative one; captured substrings are
  3761. retained in both cases.
  3762. </P>
  3763. <P>
  3764. The remaining verbs act only when a later failure causes a backtrack to
  3765. reach them. This means that, for the Perl-compatible assertions, their effect
  3766. is confined to the assertion, because Perl lookaround assertions are atomic. A
  3767. backtrack that occurs after such an assertion is complete does not jump back
  3768. into the assertion. Note in particular that a (*MARK) name that is set in an
  3769. assertion is not "seen" by an instance of (*SKIP:NAME) later in the pattern.
  3770. </P>
  3771. <P>
  3772. PCRE2 now supports non-atomic positive assertions, as described in the section
  3773. entitled
  3774. <a href="#nonatomicassertions">"Non-atomic assertions"</a>
  3775. above. These assertions must be standalone (not used as conditions). They are
  3776. not Perl-compatible. For these assertions, a later backtrack does jump back
  3777. into the assertion, and therefore verbs such as (*COMMIT) can be triggered by
  3778. backtracks from later in the pattern.
  3779. </P>
  3780. <P>
  3781. The effect of (*THEN) is not allowed to escape beyond an assertion. If there
  3782. are no more branches to try, (*THEN) causes a positive assertion to be false,
  3783. and a negative assertion to be true.
  3784. </P>
  3785. <P>
  3786. The other backtracking verbs are not treated specially if they appear in a
  3787. standalone positive assertion. In a conditional positive assertion,
  3788. backtracking (from within the assertion) into (*COMMIT), (*SKIP), or (*PRUNE)
  3789. causes the condition to be false. However, for both standalone and conditional
  3790. negative assertions, backtracking into (*COMMIT), (*SKIP), or (*PRUNE) causes
  3791. the assertion to be true, without considering any further alternative branches.
  3792. <a name="btsub"></a></P>
  3793. <br><b>
  3794. Backtracking verbs in subroutines
  3795. </b><br>
  3796. <P>
  3797. These behaviours occur whether or not the group is called recursively.
  3798. </P>
  3799. <P>
  3800. (*ACCEPT) in a group called as a subroutine causes the subroutine match to
  3801. succeed without any further processing. Matching then continues after the
  3802. subroutine call. Perl documents this behaviour. Perl's treatment of the other
  3803. verbs in subroutines is different in some cases.
  3804. </P>
  3805. <P>
  3806. (*FAIL) in a group called as a subroutine has its normal effect: it forces
  3807. an immediate backtrack.
  3808. </P>
  3809. <P>
  3810. (*COMMIT), (*SKIP), and (*PRUNE) cause the subroutine match to fail when
  3811. triggered by being backtracked to in a group called as a subroutine. There is
  3812. then a backtrack at the outer level.
  3813. </P>
  3814. <P>
  3815. (*THEN), when triggered, skips to the next alternative in the innermost
  3816. enclosing group that has alternatives (its normal behaviour). However, if there
  3817. is no such group within the subroutine's group, the subroutine match fails and
  3818. there is a backtrack at the outer level.
  3819. </P>
  3820. <br><a name="SEC30" href="#TOC1">SEE ALSO</a><br>
  3821. <P>
  3822. <b>pcre2api</b>(3), <b>pcre2callout</b>(3), <b>pcre2matching</b>(3),
  3823. <b>pcre2syntax</b>(3), <b>pcre2</b>(3).
  3824. </P>
  3825. <br><a name="SEC31" href="#TOC1">AUTHOR</a><br>
  3826. <P>
  3827. Philip Hazel
  3828. <br>
  3829. Retired from University Computing Service
  3830. <br>
  3831. Cambridge, England.
  3832. <br>
  3833. </P>
  3834. <br><a name="SEC32" href="#TOC1">REVISION</a><br>
  3835. <P>
  3836. Last updated: 04 June 2024
  3837. <br>
  3838. Copyright &copy; 1997-2024 University of Cambridge.
  3839. <br>
  3840. <p>
  3841. Return to the <a href="index.html">PCRE2 index page</a>.
  3842. </p>