PageRenderTime 40ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/win32/pcre/doc/html/pcrepattern.html

https://github.com/MaDDoGo/xbmc
HTML | 1188 lines | 1181 code | 7 blank | 0 comment | 0 complexity | 7b69a0a3c4559a5f9f0121083faa9489 MD5 | raw file
  1. <html>
  2. <head>
  3. <title>pcrepattern specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcrepattern man page</h1>
  7. <p>
  8. Return to the <a href="index.html">PCRE index page</a>.
  9. </p>
  10. <p>
  11. This page is part of the PCRE HTML documentation. It was generated automatically
  12. from the original man page. If there is any nonsense in it, please consult the
  13. man page, in case the conversion went wrong.
  14. <br>
  15. <ul>
  16. <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>
  17. <li><a name="TOC2" href="#SEC2">NEWLINE CONVENTIONS</a>
  18. <li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a>
  19. <li><a name="TOC4" href="#SEC4">BACKSLASH</a>
  20. <li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a>
  21. <li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT)</a>
  22. <li><a name="TOC7" href="#SEC7">MATCHING A SINGLE BYTE</a>
  23. <li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a>
  24. <li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a>
  25. <li><a name="TOC10" href="#SEC10">VERTICAL BAR</a>
  26. <li><a name="TOC11" href="#SEC11">INTERNAL OPTION SETTING</a>
  27. <li><a name="TOC12" href="#SEC12">SUBPATTERNS</a>
  28. <li><a name="TOC13" href="#SEC13">DUPLICATE SUBPATTERN NUMBERS</a>
  29. <li><a name="TOC14" href="#SEC14">NAMED SUBPATTERNS</a>
  30. <li><a name="TOC15" href="#SEC15">REPETITION</a>
  31. <li><a name="TOC16" href="#SEC16">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
  32. <li><a name="TOC17" href="#SEC17">BACK REFERENCES</a>
  33. <li><a name="TOC18" href="#SEC18">ASSERTIONS</a>
  34. <li><a name="TOC19" href="#SEC19">CONDITIONAL SUBPATTERNS</a>
  35. <li><a name="TOC20" href="#SEC20">COMMENTS</a>
  36. <li><a name="TOC21" href="#SEC21">RECURSIVE PATTERNS</a>
  37. <li><a name="TOC22" href="#SEC22">SUBPATTERNS AS SUBROUTINES</a>
  38. <li><a name="TOC23" href="#SEC23">ONIGURUMA SUBROUTINE SYNTAX</a>
  39. <li><a name="TOC24" href="#SEC24">CALLOUTS</a>
  40. <li><a name="TOC25" href="#SEC25">BACKTRACKING CONTROL</a>
  41. <li><a name="TOC26" href="#SEC26">SEE ALSO</a>
  42. <li><a name="TOC27" href="#SEC27">AUTHOR</a>
  43. <li><a name="TOC28" href="#SEC28">REVISION</a>
  44. </ul>
  45. <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
  46. <P>
  47. The syntax and semantics of the regular expressions that are supported by PCRE
  48. are described in detail below. There is a quick-reference syntax summary in the
  49. <a href="pcresyntax.html"><b>pcresyntax</b></a>
  50. page. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE
  51. also supports some alternative regular expression syntax (which does not
  52. conflict with the Perl syntax) in order to provide some compatibility with
  53. regular expressions in Python, .NET, and Oniguruma.
  54. </P>
  55. <P>
  56. Perl's regular expressions are described in its own documentation, and
  57. regular expressions in general are covered in a number of books, some of which
  58. have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
  59. published by O'Reilly, covers regular expressions in great detail. This
  60. description of PCRE's regular expressions is intended as reference material.
  61. </P>
  62. <P>
  63. The original operation of PCRE was on strings of one-byte characters. However,
  64. there is now also support for UTF-8 character strings. To use this,
  65. PCRE must be built to include UTF-8 support, and you must call
  66. <b>pcre_compile()</b> or <b>pcre_compile2()</b> with the PCRE_UTF8 option. There
  67. is also a special sequence that can be given at the start of a pattern:
  68. <pre>
  69. (*UTF8)
  70. </pre>
  71. Starting a pattern with this sequence is equivalent to setting the PCRE_UTF8
  72. option. This feature is not Perl-compatible. How setting UTF-8 mode affects
  73. pattern matching is mentioned in several places below. There is also a summary
  74. of UTF-8 features in the
  75. <a href="pcre.html#utf8support">section on UTF-8 support</a>
  76. in the main
  77. <a href="pcre.html"><b>pcre</b></a>
  78. page.
  79. </P>
  80. <P>
  81. The remainder of this document discusses the patterns that are supported by
  82. PCRE when its main matching function, <b>pcre_exec()</b>, is used.
  83. From release 6.0, PCRE offers a second matching function,
  84. <b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not
  85. Perl-compatible. Some of the features discussed below are not available when
  86. <b>pcre_dfa_exec()</b> is used. The advantages and disadvantages of the
  87. alternative function, and how it differs from the normal function, are
  88. discussed in the
  89. <a href="pcrematching.html"><b>pcrematching</b></a>
  90. page.
  91. </P>
  92. <br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br>
  93. <P>
  94. PCRE supports five different conventions for indicating line breaks in
  95. strings: a single CR (carriage return) character, a single LF (linefeed)
  96. character, the two-character sequence CRLF, any of the three preceding, or any
  97. Unicode newline sequence. The
  98. <a href="pcreapi.html"><b>pcreapi</b></a>
  99. page has
  100. <a href="pcreapi.html#newlines">further discussion</a>
  101. about newlines, and shows how to set the newline convention in the
  102. <i>options</i> arguments for the compiling and matching functions.
  103. </P>
  104. <P>
  105. It is also possible to specify a newline convention by starting a pattern
  106. string with one of the following five sequences:
  107. <pre>
  108. (*CR) carriage return
  109. (*LF) linefeed
  110. (*CRLF) carriage return, followed by linefeed
  111. (*ANYCRLF) any of the three above
  112. (*ANY) all Unicode newline sequences
  113. </pre>
  114. These override the default and the options given to <b>pcre_compile()</b> or
  115. <b>pcre_compile2()</b>. For example, on a Unix system where LF is the default
  116. newline sequence, the pattern
  117. <pre>
  118. (*CR)a.b
  119. </pre>
  120. changes the convention to CR. That pattern matches "a\nb" because LF is no
  121. longer a newline. Note that these special settings, which are not
  122. Perl-compatible, are recognized only at the very start of a pattern, and that
  123. they must be in upper case. If more than one of them is present, the last one
  124. is used.
  125. </P>
  126. <P>
  127. The newline convention does not affect what the \R escape sequence matches. By
  128. default, this is any Unicode newline sequence, for Perl compatibility. However,
  129. this can be changed; see the description of \R in the section entitled
  130. <a href="#newlineseq">"Newline sequences"</a>
  131. below. A change of \R setting can be combined with a change of newline
  132. convention.
  133. </P>
  134. <br><a name="SEC3" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
  135. <P>
  136. A regular expression is a pattern that is matched against a subject string from
  137. left to right. Most characters stand for themselves in a pattern, and match the
  138. corresponding characters in the subject. As a trivial example, the pattern
  139. <pre>
  140. The quick brown fox
  141. </pre>
  142. matches a portion of a subject string that is identical to itself. When
  143. caseless matching is specified (the PCRE_CASELESS option), letters are matched
  144. independently of case. In UTF-8 mode, PCRE always understands the concept of
  145. case for characters whose values are less than 128, so caseless matching is
  146. always possible. For characters with higher values, the concept of case is
  147. supported if PCRE is compiled with Unicode property support, but not otherwise.
  148. If you want to use caseless matching for characters 128 and above, you must
  149. ensure that PCRE is compiled with Unicode property support as well as with
  150. UTF-8 support.
  151. </P>
  152. <P>
  153. The power of regular expressions comes from the ability to include alternatives
  154. and repetitions in the pattern. These are encoded in the pattern by the use of
  155. <i>metacharacters</i>, which do not stand for themselves but instead are
  156. interpreted in some special way.
  157. </P>
  158. <P>
  159. There are two different sets of metacharacters: those that are recognized
  160. anywhere in the pattern except within square brackets, and those that are
  161. recognized within square brackets. Outside square brackets, the metacharacters
  162. are as follows:
  163. <pre>
  164. \ general escape character with several uses
  165. ^ assert start of string (or line, in multiline mode)
  166. $ assert end of string (or line, in multiline mode)
  167. . match any character except newline (by default)
  168. [ start character class definition
  169. | start of alternative branch
  170. ( start subpattern
  171. ) end subpattern
  172. ? extends the meaning of (
  173. also 0 or 1 quantifier
  174. also quantifier minimizer
  175. * 0 or more quantifier
  176. + 1 or more quantifier
  177. also "possessive quantifier"
  178. { start min/max quantifier
  179. </pre>
  180. Part of a pattern that is in square brackets is called a "character class". In
  181. a character class the only metacharacters are:
  182. <pre>
  183. \ general escape character
  184. ^ negate the class, but only if the first character
  185. - indicates character range
  186. [ POSIX character class (only if followed by POSIX syntax)
  187. ] terminates the character class
  188. </pre>
  189. The following sections describe the use of each of the metacharacters.
  190. </P>
  191. <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>
  192. <P>
  193. The backslash character has several uses. Firstly, if it is followed by a
  194. non-alphanumeric character, it takes away any special meaning that character
  195. may have. This use of backslash as an escape character applies both inside and
  196. outside character classes.
  197. </P>
  198. <P>
  199. For example, if you want to match a * character, you write \* in the pattern.
  200. This escaping action applies whether or not the following character would
  201. otherwise be interpreted as a metacharacter, so it is always safe to precede a
  202. non-alphanumeric with backslash to specify that it stands for itself. In
  203. particular, if you want to match a backslash, you write \\.
  204. </P>
  205. <P>
  206. If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
  207. pattern (other than in a character class) and characters between a # outside
  208. a character class and the next newline are ignored. An escaping backslash can
  209. be used to include a whitespace or # character as part of the pattern.
  210. </P>
  211. <P>
  212. If you want to remove the special meaning from a sequence of characters, you
  213. can do so by putting them between \Q and \E. This is different from Perl in
  214. that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in
  215. Perl, $ and @ cause variable interpolation. Note the following examples:
  216. <pre>
  217. Pattern PCRE matches Perl matches
  218. \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
  219. \Qabc\$xyz\E abc\$xyz abc\$xyz
  220. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  221. </pre>
  222. The \Q...\E sequence is recognized both inside and outside character classes.
  223. <a name="digitsafterbackslash"></a></P>
  224. <br><b>
  225. Non-printing characters
  226. </b><br>
  227. <P>
  228. A second use of backslash provides a way of encoding non-printing characters
  229. in patterns in a visible manner. There is no restriction on the appearance of
  230. non-printing characters, apart from the binary zero that terminates a pattern,
  231. but when a pattern is being prepared by text editing, it is often easier to use
  232. one of the following escape sequences than the binary character it represents:
  233. <pre>
  234. \a alarm, that is, the BEL character (hex 07)
  235. \cx "control-x", where x is any character
  236. \e escape (hex 1B)
  237. \f formfeed (hex 0C)
  238. \n linefeed (hex 0A)
  239. \r carriage return (hex 0D)
  240. \t tab (hex 09)
  241. \ddd character with octal code ddd, or backreference
  242. \xhh character with hex code hh
  243. \x{hhh..} character with hex code hhh..
  244. </pre>
  245. The precise effect of \cx is as follows: if x is a lower case letter, it
  246. is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
  247. Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex
  248. 7B.
  249. </P>
  250. <P>
  251. After \x, from zero to two hexadecimal digits are read (letters can be in
  252. upper or lower case). Any number of hexadecimal digits may appear between \x{
  253. and }, but the value of the character code must be less than 256 in non-UTF-8
  254. mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in
  255. hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code
  256. point, which is 10FFFF.
  257. </P>
  258. <P>
  259. If characters other than hexadecimal digits appear between \x{ and }, or if
  260. there is no terminating }, this form of escape is not recognized. Instead, the
  261. initial \x will be interpreted as a basic hexadecimal escape, with no
  262. following digits, giving a character whose value is zero.
  263. </P>
  264. <P>
  265. Characters whose value is less than 256 can be defined by either of the two
  266. syntaxes for \x. There is no difference in the way they are handled. For
  267. example, \xdc is exactly the same as \x{dc}.
  268. </P>
  269. <P>
  270. After \0 up to two further octal digits are read. If there are fewer than two
  271. digits, just those that are present are used. Thus the sequence \0\x\07
  272. specifies two binary zeros followed by a BEL character (code value 7). Make
  273. sure you supply two digits after the initial zero if the pattern character that
  274. follows is itself an octal digit.
  275. </P>
  276. <P>
  277. The handling of a backslash followed by a digit other than 0 is complicated.
  278. Outside a character class, PCRE reads it and any following digits as a decimal
  279. number. If the number is less than 10, or if there have been at least that many
  280. previous capturing left parentheses in the expression, the entire sequence is
  281. taken as a <i>back reference</i>. A description of how this works is given
  282. <a href="#backreferences">later,</a>
  283. following the discussion of
  284. <a href="#subpattern">parenthesized subpatterns.</a>
  285. </P>
  286. <P>
  287. Inside a character class, or if the decimal number is greater than 9 and there
  288. have not been that many capturing subpatterns, PCRE re-reads up to three octal
  289. digits following the backslash, and uses them to generate a data character. Any
  290. subsequent digits stand for themselves. In non-UTF-8 mode, the value of a
  291. character specified in octal must be less than \400. In UTF-8 mode, values up
  292. to \777 are permitted. For example:
  293. <pre>
  294. \040 is another way of writing a space
  295. \40 is the same, provided there are fewer than 40 previous capturing subpatterns
  296. \7 is always a back reference
  297. \11 might be a back reference, or another way of writing a tab
  298. \011 is always a tab
  299. \0113 is a tab followed by the character "3"
  300. \113 might be a back reference, otherwise the character with octal code 113
  301. \377 might be a back reference, otherwise the byte consisting entirely of 1 bits
  302. \81 is either a back reference, or a binary zero followed by the two characters "8" and "1"
  303. </pre>
  304. Note that octal values of 100 or greater must not be introduced by a leading
  305. zero, because no more than three octal digits are ever read.
  306. </P>
  307. <P>
  308. All the sequences that define a single character value can be used both inside
  309. and outside character classes. In addition, inside a character class, the
  310. sequence \b is interpreted as the backspace character (hex 08), and the
  311. sequences \R and \X are interpreted as the characters "R" and "X",
  312. respectively. Outside a character class, these sequences have different
  313. meanings
  314. <a href="#uniextseq">(see below).</a>
  315. </P>
  316. <br><b>
  317. Absolute and relative back references
  318. </b><br>
  319. <P>
  320. The sequence \g followed by an unsigned or a negative number, optionally
  321. enclosed in braces, is an absolute or relative back reference. A named back
  322. reference can be coded as \g{name}. Back references are discussed
  323. <a href="#backreferences">later,</a>
  324. following the discussion of
  325. <a href="#subpattern">parenthesized subpatterns.</a>
  326. </P>
  327. <br><b>
  328. Absolute and relative subroutine calls
  329. </b><br>
  330. <P>
  331. For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
  332. a number enclosed either in angle brackets or single quotes, is an alternative
  333. syntax for referencing a subpattern as a "subroutine". Details are discussed
  334. <a href="#onigurumasubroutines">later.</a>
  335. Note that \g{...} (Perl syntax) and \g&#60;...&#62; (Oniguruma syntax) are <i>not</i>
  336. synonymous. The former is a back reference; the latter is a
  337. <a href="#subpatternsassubroutines">subroutine</a>
  338. call.
  339. </P>
  340. <br><b>
  341. Generic character types
  342. </b><br>
  343. <P>
  344. Another use of backslash is for specifying generic character types. The
  345. following are always recognized:
  346. <pre>
  347. \d any decimal digit
  348. \D any character that is not a decimal digit
  349. \h any horizontal whitespace character
  350. \H any character that is not a horizontal whitespace character
  351. \s any whitespace character
  352. \S any character that is not a whitespace character
  353. \v any vertical whitespace character
  354. \V any character that is not a vertical whitespace character
  355. \w any "word" character
  356. \W any "non-word" character
  357. </pre>
  358. Each pair of escape sequences partitions the complete set of characters into
  359. two disjoint sets. Any given character matches one, and only one, of each pair.
  360. </P>
  361. <P>
  362. These character type sequences can appear both inside and outside character
  363. classes. They each match one character of the appropriate type. If the current
  364. matching point is at the end of the subject string, all of them fail, since
  365. there is no character to match.
  366. </P>
  367. <P>
  368. For compatibility with Perl, \s does not match the VT character (code 11).
  369. This makes it different from the the POSIX "space" class. The \s characters
  370. are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is
  371. included in a Perl script, \s may match the VT character. In PCRE, it never
  372. does.
  373. </P>
  374. <P>
  375. In UTF-8 mode, characters with values greater than 128 never match \d, \s, or
  376. \w, and always match \D, \S, and \W. This is true even when Unicode
  377. character property support is available. These sequences retain their original
  378. meanings from before UTF-8 support was available, mainly for efficiency
  379. reasons. Note that this also affects \b, because it is defined in terms of \w
  380. and \W.
  381. </P>
  382. <P>
  383. The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the
  384. other sequences, these do match certain high-valued codepoints in UTF-8 mode.
  385. The horizontal space characters are:
  386. <pre>
  387. U+0009 Horizontal tab
  388. U+0020 Space
  389. U+00A0 Non-break space
  390. U+1680 Ogham space mark
  391. U+180E Mongolian vowel separator
  392. U+2000 En quad
  393. U+2001 Em quad
  394. U+2002 En space
  395. U+2003 Em space
  396. U+2004 Three-per-em space
  397. U+2005 Four-per-em space
  398. U+2006 Six-per-em space
  399. U+2007 Figure space
  400. U+2008 Punctuation space
  401. U+2009 Thin space
  402. U+200A Hair space
  403. U+202F Narrow no-break space
  404. U+205F Medium mathematical space
  405. U+3000 Ideographic space
  406. </pre>
  407. The vertical space characters are:
  408. <pre>
  409. U+000A Linefeed
  410. U+000B Vertical tab
  411. U+000C Formfeed
  412. U+000D Carriage return
  413. U+0085 Next line
  414. U+2028 Line separator
  415. U+2029 Paragraph separator
  416. </PRE>
  417. </P>
  418. <P>
  419. A "word" character is an underscore or any character less than 256 that is a
  420. letter or digit. The definition of letters and digits is controlled by PCRE's
  421. low-valued character tables, and may vary if locale-specific matching is taking
  422. place (see
  423. <a href="pcreapi.html#localesupport">"Locale support"</a>
  424. in the
  425. <a href="pcreapi.html"><b>pcreapi</b></a>
  426. page). For example, in a French locale such as "fr_FR" in Unix-like systems,
  427. or "french" in Windows, some character codes greater than 128 are used for
  428. accented letters, and these are matched by \w. The use of locales with Unicode
  429. is discouraged.
  430. <a name="newlineseq"></a></P>
  431. <br><b>
  432. Newline sequences
  433. </b><br>
  434. <P>
  435. Outside a character class, by default, the escape sequence \R matches any
  436. Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is
  437. equivalent to the following:
  438. <pre>
  439. (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
  440. </pre>
  441. This is an example of an "atomic group", details of which are given
  442. <a href="#atomicgroup">below.</a>
  443. This particular group matches either the two-character sequence CR followed by
  444. LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
  445. U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
  446. line, U+0085). The two-character sequence is treated as a single unit that
  447. cannot be split.
  448. </P>
  449. <P>
  450. In UTF-8 mode, two additional characters whose codepoints are greater than 255
  451. are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
  452. Unicode character property support is not needed for these characters to be
  453. recognized.
  454. </P>
  455. <P>
  456. It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
  457. complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
  458. either at compile time or when the pattern is matched. (BSR is an abbrevation
  459. for "backslash R".) This can be made the default when PCRE is built; if this is
  460. the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
  461. It is also possible to specify these settings by starting a pattern string with
  462. one of the following sequences:
  463. <pre>
  464. (*BSR_ANYCRLF) CR, LF, or CRLF only
  465. (*BSR_UNICODE) any Unicode newline sequence
  466. </pre>
  467. These override the default and the options given to <b>pcre_compile()</b> or
  468. <b>pcre_compile2()</b>, but they can be overridden by options given to
  469. <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings,
  470. which are not Perl-compatible, are recognized only at the very start of a
  471. pattern, and that they must be in upper case. If more than one of them is
  472. present, the last one is used. They can be combined with a change of newline
  473. convention, for example, a pattern can start with:
  474. <pre>
  475. (*ANY)(*BSR_ANYCRLF)
  476. </pre>
  477. Inside a character class, \R matches the letter "R".
  478. <a name="uniextseq"></a></P>
  479. <br><b>
  480. Unicode character properties
  481. </b><br>
  482. <P>
  483. When PCRE is built with Unicode character property support, three additional
  484. escape sequences that match characters with specific properties are available.
  485. When not in UTF-8 mode, these sequences are of course limited to testing
  486. characters whose codepoints are less than 256, but they do work in this mode.
  487. The extra escape sequences are:
  488. <pre>
  489. \p{<i>xx</i>} a character with the <i>xx</i> property
  490. \P{<i>xx</i>} a character without the <i>xx</i> property
  491. \X an extended Unicode sequence
  492. </pre>
  493. The property names represented by <i>xx</i> above are limited to the Unicode
  494. script names, the general category properties, and "Any", which matches any
  495. character (including newline). Other properties such as "InMusicalSymbols" are
  496. not currently supported by PCRE. Note that \P{Any} does not match any
  497. characters, so always causes a match failure.
  498. </P>
  499. <P>
  500. Sets of Unicode characters are defined as belonging to certain scripts. A
  501. character from one of these sets can be matched using a script name. For
  502. example:
  503. <pre>
  504. \p{Greek}
  505. \P{Han}
  506. </pre>
  507. Those that are not part of an identified script are lumped together as
  508. "Common". The current list of scripts is:
  509. </P>
  510. <P>
  511. Arabic,
  512. Armenian,
  513. Balinese,
  514. Bengali,
  515. Bopomofo,
  516. Braille,
  517. Buginese,
  518. Buhid,
  519. Canadian_Aboriginal,
  520. Cherokee,
  521. Common,
  522. Coptic,
  523. Cuneiform,
  524. Cypriot,
  525. Cyrillic,
  526. Deseret,
  527. Devanagari,
  528. Ethiopic,
  529. Georgian,
  530. Glagolitic,
  531. Gothic,
  532. Greek,
  533. Gujarati,
  534. Gurmukhi,
  535. Han,
  536. Hangul,
  537. Hanunoo,
  538. Hebrew,
  539. Hiragana,
  540. Inherited,
  541. Kannada,
  542. Katakana,
  543. Kharoshthi,
  544. Khmer,
  545. Lao,
  546. Latin,
  547. Limbu,
  548. Linear_B,
  549. Malayalam,
  550. Mongolian,
  551. Myanmar,
  552. New_Tai_Lue,
  553. Nko,
  554. Ogham,
  555. Old_Italic,
  556. Old_Persian,
  557. Oriya,
  558. Osmanya,
  559. Phags_Pa,
  560. Phoenician,
  561. Runic,
  562. Shavian,
  563. Sinhala,
  564. Syloti_Nagri,
  565. Syriac,
  566. Tagalog,
  567. Tagbanwa,
  568. Tai_Le,
  569. Tamil,
  570. Telugu,
  571. Thaana,
  572. Thai,
  573. Tibetan,
  574. Tifinagh,
  575. Ugaritic,
  576. Yi.
  577. </P>
  578. <P>
  579. Each character has exactly one general category property, specified by a
  580. two-letter abbreviation. For compatibility with Perl, negation can be specified
  581. by including a circumflex between the opening brace and the property name. For
  582. example, \p{^Lu} is the same as \P{Lu}.
  583. </P>
  584. <P>
  585. If only one letter is specified with \p or \P, it includes all the general
  586. category properties that start with that letter. In this case, in the absence
  587. of negation, the curly brackets in the escape sequence are optional; these two
  588. examples have the same effect:
  589. <pre>
  590. \p{L}
  591. \pL
  592. </pre>
  593. The following general category property codes are supported:
  594. <pre>
  595. C Other
  596. Cc Control
  597. Cf Format
  598. Cn Unassigned
  599. Co Private use
  600. Cs Surrogate
  601. L Letter
  602. Ll Lower case letter
  603. Lm Modifier letter
  604. Lo Other letter
  605. Lt Title case letter
  606. Lu Upper case letter
  607. M Mark
  608. Mc Spacing mark
  609. Me Enclosing mark
  610. Mn Non-spacing mark
  611. N Number
  612. Nd Decimal number
  613. Nl Letter number
  614. No Other number
  615. P Punctuation
  616. Pc Connector punctuation
  617. Pd Dash punctuation
  618. Pe Close punctuation
  619. Pf Final punctuation
  620. Pi Initial punctuation
  621. Po Other punctuation
  622. Ps Open punctuation
  623. S Symbol
  624. Sc Currency symbol
  625. Sk Modifier symbol
  626. Sm Mathematical symbol
  627. So Other symbol
  628. Z Separator
  629. Zl Line separator
  630. Zp Paragraph separator
  631. Zs Space separator
  632. </pre>
  633. The special property L& is also supported: it matches a character that has
  634. the Lu, Ll, or Lt property, in other words, a letter that is not classified as
  635. a modifier or "other".
  636. </P>
  637. <P>
  638. The Cs (Surrogate) property applies only to characters in the range U+D800 to
  639. U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so
  640. cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
  641. (see the discussion of PCRE_NO_UTF8_CHECK in the
  642. <a href="pcreapi.html"><b>pcreapi</b></a>
  643. page). Perl does not support the Cs property.
  644. </P>
  645. <P>
  646. The long synonyms for property names that Perl supports (such as \p{Letter})
  647. are not supported by PCRE, nor is it permitted to prefix any of these
  648. properties with "Is".
  649. </P>
  650. <P>
  651. No character that is in the Unicode table has the Cn (unassigned) property.
  652. Instead, this property is assumed for any code point that is not in the
  653. Unicode table.
  654. </P>
  655. <P>
  656. Specifying caseless matching does not affect these escape sequences. For
  657. example, \p{Lu} always matches only upper case letters.
  658. </P>
  659. <P>
  660. The \X escape matches any number of Unicode characters that form an extended
  661. Unicode sequence. \X is equivalent to
  662. <pre>
  663. (?&#62;\PM\pM*)
  664. </pre>
  665. That is, it matches a character without the "mark" property, followed by zero
  666. or more characters with the "mark" property, and treats the sequence as an
  667. atomic group
  668. <a href="#atomicgroup">(see below).</a>
  669. Characters with the "mark" property are typically accents that affect the
  670. preceding character. None of them have codepoints less than 256, so in
  671. non-UTF-8 mode \X matches any one character.
  672. </P>
  673. <P>
  674. Matching characters by Unicode property is not fast, because PCRE has to search
  675. a structure that contains data for over fifteen thousand characters. That is
  676. why the traditional escape sequences such as \d and \w do not use Unicode
  677. properties in PCRE.
  678. <a name="resetmatchstart"></a></P>
  679. <br><b>
  680. Resetting the match start
  681. </b><br>
  682. <P>
  683. The escape sequence \K, which is a Perl 5.10 feature, causes any previously
  684. matched characters not to be included in the final matched sequence. For
  685. example, the pattern:
  686. <pre>
  687. foo\Kbar
  688. </pre>
  689. matches "foobar", but reports that it has matched "bar". This feature is
  690. similar to a lookbehind assertion
  691. <a href="#lookbehind">(described below).</a>
  692. However, in this case, the part of the subject before the real match does not
  693. have to be of fixed length, as lookbehind assertions do. The use of \K does
  694. not interfere with the setting of
  695. <a href="#subpattern">captured substrings.</a>
  696. For example, when the pattern
  697. <pre>
  698. (foo)\Kbar
  699. </pre>
  700. matches "foobar", the first substring is still set to "foo".
  701. <a name="smallassertions"></a></P>
  702. <br><b>
  703. Simple assertions
  704. </b><br>
  705. <P>
  706. The final use of backslash is for certain simple assertions. An assertion
  707. specifies a condition that has to be met at a particular point in a match,
  708. without consuming any characters from the subject string. The use of
  709. subpatterns for more complicated assertions is described
  710. <a href="#bigassertions">below.</a>
  711. The backslashed assertions are:
  712. <pre>
  713. \b matches at a word boundary
  714. \B matches when not at a word boundary
  715. \A matches at the start of the subject
  716. \Z matches at the end of the subject
  717. also matches before a newline at the end of the subject
  718. \z matches only at the end of the subject
  719. \G matches at the first matching position in the subject
  720. </pre>
  721. These assertions may not appear in character classes (but note that \b has a
  722. different meaning, namely the backspace character, inside a character class).
  723. </P>
  724. <P>
  725. A word boundary is a position in the subject string where the current character
  726. and the previous character do not both match \w or \W (i.e. one matches
  727. \w and the other matches \W), or the start or end of the string if the
  728. first or last character matches \w, respectively. Neither PCRE nor Perl has a
  729. separte "start of word" or "end of word" metasequence. However, whatever
  730. follows \b normally determines which it is. For example, the fragment
  731. \ba matches "a" at the start of a word.
  732. </P>
  733. <P>
  734. The \A, \Z, and \z assertions differ from the traditional circumflex and
  735. dollar (described in the next section) in that they only ever match at the very
  736. start and end of the subject string, whatever options are set. Thus, they are
  737. independent of multiline mode. These three assertions are not affected by the
  738. PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
  739. circumflex and dollar metacharacters. However, if the <i>startoffset</i>
  740. argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start
  741. at a point other than the beginning of the subject, \A can never match. The
  742. difference between \Z and \z is that \Z matches before a newline at the end
  743. of the string as well as at the very end, whereas \z matches only at the end.
  744. </P>
  745. <P>
  746. The \G assertion is true only when the current matching position is at the
  747. start point of the match, as specified by the <i>startoffset</i> argument of
  748. <b>pcre_exec()</b>. It differs from \A when the value of <i>startoffset</i> is
  749. non-zero. By calling <b>pcre_exec()</b> multiple times with appropriate
  750. arguments, you can mimic Perl's /g option, and it is in this kind of
  751. implementation where \G can be useful.
  752. </P>
  753. <P>
  754. Note, however, that PCRE's interpretation of \G, as the start of the current
  755. match, is subtly different from Perl's, which defines it as the end of the
  756. previous match. In Perl, these can be different when the previously matched
  757. string was empty. Because PCRE does just one match at a time, it cannot
  758. reproduce this behaviour.
  759. </P>
  760. <P>
  761. If all the alternatives of a pattern begin with \G, the expression is anchored
  762. to the starting match position, and the "anchored" flag is set in the compiled
  763. regular expression.
  764. </P>
  765. <br><a name="SEC5" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
  766. <P>
  767. Outside a character class, in the default matching mode, the circumflex
  768. character is an assertion that is true only if the current matching point is
  769. at the start of the subject string. If the <i>startoffset</i> argument of
  770. <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE
  771. option is unset. Inside a character class, circumflex has an entirely different
  772. meaning
  773. <a href="#characterclass">(see below).</a>
  774. </P>
  775. <P>
  776. Circumflex need not be the first character of the pattern if a number of
  777. alternatives are involved, but it should be the first thing in each alternative
  778. in which it appears if the pattern is ever to match that branch. If all
  779. possible alternatives start with a circumflex, that is, if the pattern is
  780. constrained to match only at the start of the subject, it is said to be an
  781. "anchored" pattern. (There are also other constructs that can cause a pattern
  782. to be anchored.)
  783. </P>
  784. <P>
  785. A dollar character is an assertion that is true only if the current matching
  786. point is at the end of the subject string, or immediately before a newline
  787. at the end of the string (by default). Dollar need not be the last character of
  788. the pattern if a number of alternatives are involved, but it should be the last
  789. item in any branch in which it appears. Dollar has no special meaning in a
  790. character class.
  791. </P>
  792. <P>
  793. The meaning of dollar can be changed so that it matches only at the very end of
  794. the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
  795. does not affect the \Z assertion.
  796. </P>
  797. <P>
  798. The meanings of the circumflex and dollar characters are changed if the
  799. PCRE_MULTILINE option is set. When this is the case, a circumflex matches
  800. immediately after internal newlines as well as at the start of the subject
  801. string. It does not match after a newline that ends the string. A dollar
  802. matches before any newlines in the string, as well as at the very end, when
  803. PCRE_MULTILINE is set. When newline is specified as the two-character
  804. sequence CRLF, isolated CR and LF characters do not indicate newlines.
  805. </P>
  806. <P>
  807. For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
  808. \n represents a newline) in multiline mode, but not otherwise. Consequently,
  809. patterns that are anchored in single line mode because all branches start with
  810. ^ are not anchored in multiline mode, and a match for circumflex is possible
  811. when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The
  812. PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
  813. </P>
  814. <P>
  815. Note that the sequences \A, \Z, and \z can be used to match the start and
  816. end of the subject in both modes, and if all branches of a pattern start with
  817. \A it is always anchored, whether or not PCRE_MULTILINE is set.
  818. </P>
  819. <br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
  820. <P>
  821. Outside a character class, a dot in the pattern matches any one character in
  822. the subject string except (by default) a character that signifies the end of a
  823. line. In UTF-8 mode, the matched character may be more than one byte long.
  824. </P>
  825. <P>
  826. When a line ending is defined as a single character, dot never matches that
  827. character; when the two-character sequence CRLF is used, dot does not match CR
  828. if it is immediately followed by LF, but otherwise it matches all characters
  829. (including isolated CRs and LFs). When any Unicode line endings are being
  830. recognized, dot does not match CR or LF or any of the other line ending
  831. characters.
  832. </P>
  833. <P>
  834. The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
  835. option is set, a dot matches any one character, without exception. If the
  836. two-character sequence CRLF is present in the subject string, it takes two dots
  837. to match it.
  838. </P>
  839. <P>
  840. The handling of dot is entirely independent of the handling of circumflex and
  841. dollar, the only relationship being that they both involve newlines. Dot has no
  842. special meaning in a character class.
  843. </P>
  844. <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
  845. <P>
  846. Outside a character class, the escape sequence \C matches any one byte, both
  847. in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
  848. characters. The feature is provided in Perl in order to match individual bytes
  849. in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,
  850. what remains in the string may be a malformed UTF-8 string. For this reason,
  851. the \C escape sequence is best avoided.
  852. </P>
  853. <P>
  854. PCRE does not allow \C to appear in lookbehind assertions
  855. <a href="#lookbehind">(described below),</a>
  856. because in UTF-8 mode this would make it impossible to calculate the length of
  857. the lookbehind.
  858. <a name="characterclass"></a></P>
  859. <br><a name="SEC8" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
  860. <P>
  861. An opening square bracket introduces a character class, terminated by a closing
  862. square bracket. A closing square bracket on its own is not special by default.
  863. However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square
  864. bracket causes a compile-time error. If a closing square bracket is required as
  865. a member of the class, it should be the first data character in the class
  866. (after an initial circumflex, if present) or escaped with a backslash.
  867. </P>
  868. <P>
  869. A character class matches a single character in the subject. In UTF-8 mode, the
  870. character may be more than one byte long. A matched character must be in the
  871. set of characters defined by the class, unless the first character in the class
  872. definition is a circumflex, in which case the subject character must not be in
  873. the set defined by the class. If a circumflex is actually required as a member
  874. of the class, ensure it is not the first character, or escape it with a
  875. backslash.
  876. </P>
  877. <P>
  878. For example, the character class [aeiou] matches any lower case vowel, while
  879. [^aeiou] matches any character that is not a lower case vowel. Note that a
  880. circumflex is just a convenient notation for specifying the characters that
  881. are in the class by enumerating those that are not. A class that starts with a
  882. circumflex is not an assertion; it still consumes a character from the subject
  883. string, and therefore it fails if the current pointer is at the end of the
  884. string.
  885. </P>
  886. <P>
  887. In UTF-8 mode, characters with values greater than 255 can be included in a
  888. class as a literal string of bytes, or by using the \x{ escaping mechanism.
  889. </P>
  890. <P>
  891. When caseless matching is set, any letters in a class represent both their
  892. upper case and lower case versions, so for example, a caseless [aeiou] matches
  893. "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
  894. caseful version would. In UTF-8 mode, PCRE always understands the concept of
  895. case for characters whose values are less than 128, so caseless matching is
  896. always possible. For characters with higher values, the concept of case is
  897. supported if PCRE is compiled with Unicode property support, but not otherwise.
  898. If you want to use caseless matching in UTF8-mode for characters 128 and above,
  899. you must ensure that PCRE is compiled with Unicode property support as well as
  900. with UTF-8 support.
  901. </P>
  902. <P>
  903. Characters that might indicate line breaks are never treated in any special way
  904. when matching character classes, whatever line-ending sequence is in use, and
  905. whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
  906. such as [^a] always matches one of these characters.
  907. </P>
  908. <P>
  909. The minus (hyphen) character can be used to specify a range of characters in a
  910. character class. For example, [d-m] matches any letter between d and m,
  911. inclusive. If a minus character is required in a class, it must be escaped with
  912. a backslash or appear in a position where it cannot be interpreted as
  913. indicating a range, typically as the first or last character in the class.
  914. </P>
  915. <P>
  916. It is not possible to have the literal character "]" as the end character of a
  917. range. A pattern such as [W-]46] is interpreted as a class of two characters
  918. ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
  919. "-46]". However, if the "]" is escaped with a backslash it is interpreted as
  920. the end of range, so [W-\]46] is interpreted as a class containing a range
  921. followed by two other characters. The octal or hexadecimal representation of
  922. "]" can also be used to end a range.
  923. </P>
  924. <P>
  925. Ranges operate in the collating sequence of character values. They can also be
  926. used for characters specified numerically, for example [\000-\037]. In UTF-8
  927. mode, ranges can include characters whose values are greater than 255, for
  928. example [\x{100}-\x{2ff}].
  929. </P>
  930. <P>
  931. If a range that includes letters is used when caseless matching is set, it
  932. matches the letters in either case. For example, [W-c] is equivalent to
  933. [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
  934. tables for a French locale are in use, [\xc8-\xcb] matches accented E
  935. characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
  936. characters with values greater than 128 only when it is compiled with Unicode
  937. property support.
  938. </P>
  939. <P>
  940. The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear
  941. in a character class, and add the characters that they match to the class. For
  942. example, [\dABCDEF] matches any hexadecimal digit. A circumflex can
  943. conveniently be used with the upper case character types to specify a more
  944. restricted set of characters than the matching lower case type. For example,
  945. the class [^\W_] matches any letter or digit, but not underscore.
  946. </P>
  947. <P>
  948. The only metacharacters that are recognized in character classes are backslash,
  949. hyphen (only where it can be interpreted as specifying a range), circumflex
  950. (only at the start), opening square bracket (only when it can be interpreted as
  951. introducing a POSIX class name - see the next section), and the terminating
  952. closing square bracket. However, escaping other non-alphanumeric characters
  953. does no harm.
  954. </P>
  955. <br><a name="SEC9" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
  956. <P>
  957. Perl supports the POSIX notation for character classes. This uses names
  958. enclosed by [: and :] within the enclosing square brackets. PCRE also supports
  959. this notation. For example,
  960. <pre>
  961. [01[:alpha:]%]
  962. </pre>
  963. matches "0", "1", any alphabetic character, or "%". The supported class names
  964. are
  965. <pre>
  966. alnum letters and digits
  967. alpha letters
  968. ascii character codes 0 - 127
  969. blank space or tab only
  970. cntrl control characters
  971. digit decimal digits (same as \d)
  972. graph printing characters, excluding space
  973. lower lower case letters
  974. print printing characters, including space
  975. punct printing characters, excluding letters and digits
  976. space white space (not quite the same as \s)
  977. upper upper case letters
  978. word "word" characters (same as \w)
  979. xdigit hexadecimal digits
  980. </pre>
  981. The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
  982. space (32). Notice that this list includes the VT character (code 11). This
  983. makes "space" different to \s, which does not include VT (for Perl
  984. compatibility).
  985. </P>
  986. <P>
  987. The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
  988. 5.8. Another Perl extension is negation, which is indicated by a ^ character
  989. after the colon. For example,
  990. <pre>
  991. [12[:^digit:]]
  992. </pre>
  993. matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
  994. syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
  995. supported, and an error is given if they are encountered.
  996. </P>
  997. <P>
  998. In UTF-8 mode, characters with values greater than 128 do not match any of
  999. the POSIX character classes.
  1000. </P>
  1001. <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>
  1002. <P>
  1003. Vertical bar characters are used to separate alternative patterns. For example,
  1004. the pattern
  1005. <pre>
  1006. gilbert|sullivan
  1007. </pre>
  1008. matches either "gilbert" or "sullivan". Any number of alternatives may appear,
  1009. and an empty alternative is permitted (matching the empty string). The matching
  1010. process tries each alternative in turn, from left to right, and the first one
  1011. that succeeds is used. If the alternatives are within a subpattern
  1012. <a href="#subpattern">(defined below),</a>
  1013. "succeeds" means matching the rest of the main pattern as well as the
  1014. alternative in the subpattern.
  1015. </P>
  1016. <br><a name="SEC11" href="#TOC1">INTERNAL OPTION SETTING</a><br>
  1017. <P>
  1018. The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
  1019. PCRE_EXTENDED options (which are Perl-compatible) can be changed from within
  1020. the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
  1021. The option letters are
  1022. <pre>
  1023. i for PCRE_CASELESS
  1024. m for PCRE_MULTILINE
  1025. s for PCRE_DOTALL
  1026. x for PCRE_EXTENDED
  1027. </pre>
  1028. For example, (?im) sets caseless, multiline matching. It is also possible to
  1029. unset these options by preceding the letter with a hyphen, and a combined
  1030. setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
  1031. PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
  1032. permitted. If a letter appears both before and after the hyphen, the option is
  1033. unset.
  1034. </P>
  1035. <P>
  1036. The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
  1037. changed in the same way as the Perl-compatible options by using the characters
  1038. J, U and X respectively.
  1039. </P>
  1040. <P>
  1041. When one of these option changes occurs at top level (that is, not inside
  1042. subpattern parentheses), the change applies to the remainder of the pattern
  1043. that follows. If the change is placed right at the start of a pattern, PCRE
  1044. extracts it into the global options (and it will therefore show up in data
  1045. extracted by the <b>pcre_fullinfo()</b> function).
  1046. </P>
  1047. <P>
  1048. An option change within a subpattern (see below for a description of
  1049. subpatterns) affects only that part of the current pattern that follows it, so
  1050. <pre>
  1051. (a(?i)b)c
  1052. </pre>
  1053. matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
  1054. By this means, options can be made to have different settings in different
  1055. parts of the pattern. Any changes made in one alternative do carry on
  1056. into subsequent branches within the same subpattern. For example,
  1057. <pre>
  1058. (a(?i)b|c)
  1059. </pre>
  1060. matches "ab", "aB", "c", and "C", even though when matching "C" the first
  1061. branch is abandoned before the option setting. This is because the effects of
  1062. option settings happen at compile time. There would be some very weird
  1063. behaviour otherwise.
  1064. </P>
  1065. <P>
  1066. <b>Note:</b> There are other PCRE-specific options that can be set by the
  1067. application when the compile or match functions are called. In some cases the
  1068. pattern can contain special leading sequences such as (*CRLF) to override what
  1069. the application has set or what has been defaulted. Details are given in the
  1070. section entitled
  1071. <a href="#newlineseq">"Newline sequences"</a>
  1072. above. There is also the (*UTF8) leading sequence that can be used to set UTF-8
  1073. mode; this is equivalent to setting the PCRE_UTF8 option.
  1074. <a name="subpattern"></a></P>
  1075. <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>
  1076. <P>
  1077. Subpatterns are delimited by parentheses (round brackets), which can be nested.
  1078. Turning part of a pattern into a subpattern does two things:
  1079. <br>
  1080. <br>
  1081. 1. It localizes a set of alternatives. For example, the pattern
  1082. <pre>
  1083. cat(aract|erpillar|)
  1084. </pre>
  1085. matches one of the words "cat", "cataract", or "caterpillar". Without the
  1086. parentheses, it would match "cataract", "erpillar" or an empty string.
  1087. <br>
  1088. <br>
  1089. 2. It sets up the subpattern as a capturing subpattern. This means that, when
  1090. the whole pattern matches, that portion of the subject string that matched the
  1091. subpattern is passed back to the caller via the <i>ovector</i> argument of
  1092. <b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting
  1093. from 1) to obtain numbers for the capturing subpatterns.
  1094. </P>
  1095. <P>
  1096. For example, if the string "the red king" is matched against the pattern
  1097. <pre>
  1098. the ((red|white) (king|queen))
  1099. </pre>
  1100. the captured substrings are "red king", "red", and "king", and are numbered 1,
  1101. 2, and 3, respectively.
  1102. </P>
  1103. <P>
  1104. The fact that plain parentheses fulfil two functions is not always helpful.
  1105. There are often times when a grouping subpattern is required without a
  1106. capturing requirement. If an opening parenthesis is followed by a question mark
  1107. and a colon, the subpattern does not do any capturing, and is not counted when
  1108. computing the number of any subsequent capturing subpatterns. For example, if
  1109. the string "the white queen" is matched against the pattern
  1110. <pre>
  1111. the ((?:red|white) (king|queen))
  1112. </pre>
  1113. the captured substrings are "white queen" and "queen", and are numbered 1 and
  1114. 2. The maximum number of capturing subpatterns is 65535.
  1115. </P>
  1116. <P>
  1117. As a convenient shorthand, if any option settings are required at the start of
  1118. a non-capturing subpattern, the option letters may appear between the "?" and
  1119. the ":". Thus the two patterns
  1120. <pre>
  1121. (?i:saturday|sunday)
  1122. (?:(?i)saturday|sunday)
  1123. </pre>
  1124. match exactly the same set of strings. Because alternative branches are tried
  1125. from left to right, and options are not reset until the end of the subpattern
  1126. is reached, an option setting in one branch does affect subsequent branches, so
  1127. the above patterns match "SUNDAY" as well as "Saturday".
  1128. <a name="dupsubpatternnumber"></a></P>
  1129. <br><a name="SEC13" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
  1130. <P>
  1131. Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
  1132. the same numbers for its capturing parentheses. Such a subpattern starts with
  1133. (?| and is itself a non-capturing subpattern. For example, consider this
  1134. pattern:
  1135. <pre>
  1136. (?|(Sat)ur|(Sun))day
  1137. </pre>
  1138. Because the two alternatives are inside a (?| group, both sets of capturing
  1139. parentheses are numbered one. Thus, when the pattern matches, you can look
  1140. at captured substring number one, whichever alternative matched. This construct
  1141. is useful when you want to capture part, but not all, of one of a number of
  1142. alternatives. Inside a (?| group, parentheses are numbered as usual, but the
  1143. number is reset at the start of each branch. The numbers of any capturing
  1144. buffers that follow the subpattern start after the highest number used in any
  1145. branch. The following example is taken from the Perl documentation.
  1146. The numbers underneath show in which buffer the captured content will be
  1147. stored.
  1148. <pre>
  1149. # before ---------------branch-reset----------- after
  1150. / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
  1151. # 1 2 2 3 2 3 4
  1152. </pre>
  1153. A backreference to a numbered subpattern uses the most recent value that is set
  1154. for that number by any subpattern. The following pattern matches "abcabc" or
  1155. "defdef":
  1156. <pre>
  1157. /(?|(abc)|(def))\1/
  1158. </pre>
  1159. In contrast, a recursive or "subroutine" call to a numbered subpattern always
  1160. refers to the first one in the pattern with the given number. The following
  1161. pattern matches "abcabc" or "defabc":
  1162. <pre>
  1163. /(?|(abc)|(def))(?1)/
  1164. </pre>
  1165. If a
  1166. <a href="#conditions">condition test</a>
  1167. for a subpattern's having matched refers to a non-unique number, the test is
  1168. true if any of the subpatterns of that number have matched.
  1169. </P>
  1170. <P>
  1171. An alternative approach to using this "branch reset" feature is to use
  1172. duplicate named subpatterns, as described in the next section.
  1173. </P>
  1174. <br><a name="SEC14" href="#TOC1">NAMED SUBPATTERNS</a><br>
  1175. <P>
  1176. Identifying capturing parentheses by number is simple, but it can be very hard
  1177. to keep track of the numbers in complicated regular expressions. Furthermore,
  1178. if an expression is modified, the numbers may change. To help with this
  1179. difficulty, PCRE supports the naming of subpatterns. This feature was not
  1180. added to Perl until release 5.10. Python had the feature earlier, and PCRE
  1181. introduced it at release 4.0, using the Python syntax. PCRE now suppo