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.

3896 lines
164 KiB

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