Annotation of embedaddon/pcre/doc/html/pcrepattern.html, revision 1.1.1.4
1.1 misho 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>
1.1.1.4 ! misho 17: <li><a name="TOC2" href="#SEC2">SPECIAL START-OF-PATTERN ITEMS</a>
! 18: <li><a name="TOC3" href="#SEC3">EBCDIC CHARACTER CODES</a>
! 19: <li><a name="TOC4" href="#SEC4">CHARACTERS AND METACHARACTERS</a>
! 20: <li><a name="TOC5" href="#SEC5">BACKSLASH</a>
! 21: <li><a name="TOC6" href="#SEC6">CIRCUMFLEX AND DOLLAR</a>
! 22: <li><a name="TOC7" href="#SEC7">FULL STOP (PERIOD, DOT) AND \N</a>
! 23: <li><a name="TOC8" href="#SEC8">MATCHING A SINGLE DATA UNIT</a>
! 24: <li><a name="TOC9" href="#SEC9">SQUARE BRACKETS AND CHARACTER CLASSES</a>
! 25: <li><a name="TOC10" href="#SEC10">POSIX CHARACTER CLASSES</a>
! 26: <li><a name="TOC11" href="#SEC11">VERTICAL BAR</a>
! 27: <li><a name="TOC12" href="#SEC12">INTERNAL OPTION SETTING</a>
! 28: <li><a name="TOC13" href="#SEC13">SUBPATTERNS</a>
! 29: <li><a name="TOC14" href="#SEC14">DUPLICATE SUBPATTERN NUMBERS</a>
! 30: <li><a name="TOC15" href="#SEC15">NAMED SUBPATTERNS</a>
! 31: <li><a name="TOC16" href="#SEC16">REPETITION</a>
! 32: <li><a name="TOC17" href="#SEC17">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a>
! 33: <li><a name="TOC18" href="#SEC18">BACK REFERENCES</a>
! 34: <li><a name="TOC19" href="#SEC19">ASSERTIONS</a>
! 35: <li><a name="TOC20" href="#SEC20">CONDITIONAL SUBPATTERNS</a>
! 36: <li><a name="TOC21" href="#SEC21">COMMENTS</a>
! 37: <li><a name="TOC22" href="#SEC22">RECURSIVE PATTERNS</a>
! 38: <li><a name="TOC23" href="#SEC23">SUBPATTERNS AS SUBROUTINES</a>
! 39: <li><a name="TOC24" href="#SEC24">ONIGURUMA SUBROUTINE SYNTAX</a>
! 40: <li><a name="TOC25" href="#SEC25">CALLOUTS</a>
! 41: <li><a name="TOC26" href="#SEC26">BACKTRACKING CONTROL</a>
! 42: <li><a name="TOC27" href="#SEC27">SEE ALSO</a>
! 43: <li><a name="TOC28" href="#SEC28">AUTHOR</a>
! 44: <li><a name="TOC29" href="#SEC29">REVISION</a>
1.1 misho 45: </ul>
46: <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
47: <P>
48: The syntax and semantics of the regular expressions that are supported by PCRE
49: are described in detail below. There is a quick-reference syntax summary in the
50: <a href="pcresyntax.html"><b>pcresyntax</b></a>
51: page. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE
52: also supports some alternative regular expression syntax (which does not
53: conflict with the Perl syntax) in order to provide some compatibility with
54: regular expressions in Python, .NET, and Oniguruma.
55: </P>
56: <P>
57: Perl's regular expressions are described in its own documentation, and
58: regular expressions in general are covered in a number of books, some of which
59: have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
60: published by O'Reilly, covers regular expressions in great detail. This
61: description of PCRE's regular expressions is intended as reference material.
62: </P>
63: <P>
1.1.1.4 ! misho 64: This document discusses the patterns that are supported by PCRE when one its
! 65: main matching functions, <b>pcre_exec()</b> (8-bit) or <b>pcre[16|32]_exec()</b>
! 66: (16- or 32-bit), is used. PCRE also has alternative matching functions,
! 67: <b>pcre_dfa_exec()</b> and <b>pcre[16|32_dfa_exec()</b>, which match using a
! 68: different algorithm that is not Perl-compatible. Some of the features discussed
! 69: below are not available when DFA matching is used. The advantages and
! 70: disadvantages of the alternative functions, and how they differ from the normal
! 71: functions, are discussed in the
! 72: <a href="pcrematching.html"><b>pcrematching</b></a>
! 73: page.
! 74: </P>
! 75: <br><a name="SEC2" href="#TOC1">SPECIAL START-OF-PATTERN ITEMS</a><br>
! 76: <P>
! 77: A number of options that can be passed to <b>pcre_compile()</b> can also be set
! 78: by special items at the start of a pattern. These are not Perl-compatible, but
! 79: are provided to make these options accessible to pattern writers who are not
! 80: able to change the program that processes the pattern. Any number of these
! 81: items may appear, but they must all be together right at the start of the
! 82: pattern string, and the letters must be in upper case.
! 83: </P>
! 84: <br><b>
! 85: UTF support
! 86: </b><br>
! 87: <P>
1.1 misho 88: The original operation of PCRE was on strings of one-byte characters. However,
1.1.1.4 ! misho 89: there is now also support for UTF-8 strings in the original library, an
! 90: extra library that supports 16-bit and UTF-16 character strings, and a
! 91: third library that supports 32-bit and UTF-32 character strings. To use these
1.1.1.2 misho 92: features, PCRE must be built to include appropriate support. When using UTF
1.1.1.4 ! misho 93: strings you must either call the compiling function with the PCRE_UTF8,
! 94: PCRE_UTF16, or PCRE_UTF32 option, or the pattern must start with one of
! 95: these special sequences:
1.1 misho 96: <pre>
97: (*UTF8)
1.1.1.2 misho 98: (*UTF16)
1.1.1.4 ! misho 99: (*UTF32)
! 100: (*UTF)
1.1 misho 101: </pre>
1.1.1.4 ! misho 102: (*UTF) is a generic sequence that can be used with any of the libraries.
1.1.1.2 misho 103: Starting a pattern with such a sequence is equivalent to setting the relevant
1.1.1.4 ! misho 104: option. How setting a UTF mode affects pattern matching is mentioned in several
! 105: places below. There is also a summary of features in the
1.1 misho 106: <a href="pcreunicode.html"><b>pcreunicode</b></a>
107: page.
108: </P>
109: <P>
1.1.1.4 ! misho 110: Some applications that allow their users to supply patterns may wish to
! 111: restrict them to non-UTF data for security reasons. If the PCRE_NEVER_UTF
! 112: option is set at compile time, (*UTF) etc. are not allowed, and their
! 113: appearance causes an error.
! 114: </P>
! 115: <br><b>
! 116: Unicode property support
! 117: </b><br>
! 118: <P>
! 119: Another special sequence that may appear at the start of a pattern is
1.1 misho 120: <pre>
121: (*UCP)
122: </pre>
123: This has the same effect as setting the PCRE_UCP option: it causes sequences
124: such as \d and \w to use Unicode properties to determine character types,
125: instead of recognizing only characters with codes less than 128 via a lookup
126: table.
127: </P>
1.1.1.4 ! misho 128: <br><b>
! 129: Disabling start-up optimizations
! 130: </b><br>
1.1 misho 131: <P>
132: If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
1.1.1.4 ! misho 133: PCRE_NO_START_OPTIMIZE option either at compile or matching time.
1.1 misho 134: <a name="newlines"></a></P>
1.1.1.4 ! misho 135: <br><b>
! 136: Newline conventions
! 137: </b><br>
1.1 misho 138: <P>
139: PCRE supports five different conventions for indicating line breaks in
140: strings: a single CR (carriage return) character, a single LF (linefeed)
141: character, the two-character sequence CRLF, any of the three preceding, or any
142: Unicode newline sequence. The
143: <a href="pcreapi.html"><b>pcreapi</b></a>
144: page has
145: <a href="pcreapi.html#newlines">further discussion</a>
146: about newlines, and shows how to set the newline convention in the
147: <i>options</i> arguments for the compiling and matching functions.
148: </P>
149: <P>
150: It is also possible to specify a newline convention by starting a pattern
151: string with one of the following five sequences:
152: <pre>
153: (*CR) carriage return
154: (*LF) linefeed
155: (*CRLF) carriage return, followed by linefeed
156: (*ANYCRLF) any of the three above
157: (*ANY) all Unicode newline sequences
158: </pre>
1.1.1.2 misho 159: These override the default and the options given to the compiling function. For
160: example, on a Unix system where LF is the default newline sequence, the pattern
1.1 misho 161: <pre>
162: (*CR)a.b
163: </pre>
164: changes the convention to CR. That pattern matches "a\nb" because LF is no
1.1.1.4 ! misho 165: longer a newline. If more than one of these settings is present, the last one
1.1 misho 166: is used.
167: </P>
168: <P>
1.1.1.4 ! misho 169: The newline convention affects where the circumflex and dollar assertions are
! 170: true. It also affects the interpretation of the dot metacharacter when
! 171: PCRE_DOTALL is not set, and the behaviour of \N. However, it does not affect
! 172: what the \R escape sequence matches. By default, this is any Unicode newline
! 173: sequence, for Perl compatibility. However, this can be changed; see the
1.1 misho 174: description of \R in the section entitled
175: <a href="#newlineseq">"Newline sequences"</a>
176: below. A change of \R setting can be combined with a change of newline
177: convention.
178: </P>
1.1.1.4 ! misho 179: <br><b>
! 180: Setting match and recursion limits
! 181: </b><br>
! 182: <P>
! 183: The caller of <b>pcre_exec()</b> can set a limit on the number of times the
! 184: internal <b>match()</b> function is called and on the maximum depth of
! 185: recursive calls. These facilities are provided to catch runaway matches that
! 186: are provoked by patterns with huge matching trees (a typical example is a
! 187: pattern with nested unlimited repeats) and to avoid running out of system stack
! 188: by too much recursion. When one of these limits is reached, <b>pcre_exec()</b>
! 189: gives an error return. The limits can also be set by items at the start of the
! 190: pattern of the form
! 191: <pre>
! 192: (*LIMIT_MATCH=d)
! 193: (*LIMIT_RECURSION=d)
! 194: </pre>
! 195: where d is any number of decimal digits. However, the value of the setting must
! 196: be less than the value set by the caller of <b>pcre_exec()</b> for it to have
! 197: any effect. In other words, the pattern writer can lower the limit set by the
! 198: programmer, but not raise it. If there is more than one setting of one of these
! 199: limits, the lower value is used.
! 200: </P>
! 201: <br><a name="SEC3" href="#TOC1">EBCDIC CHARACTER CODES</a><br>
! 202: <P>
! 203: PCRE can be compiled to run in an environment that uses EBCDIC as its character
! 204: code rather than ASCII or Unicode (typically a mainframe system). In the
! 205: sections below, character code values are ASCII or Unicode; in an EBCDIC
! 206: environment these characters may have different code values, and there are no
! 207: code points greater than 255.
! 208: </P>
! 209: <br><a name="SEC4" href="#TOC1">CHARACTERS AND METACHARACTERS</a><br>
1.1 misho 210: <P>
211: A regular expression is a pattern that is matched against a subject string from
212: left to right. Most characters stand for themselves in a pattern, and match the
213: corresponding characters in the subject. As a trivial example, the pattern
214: <pre>
215: The quick brown fox
216: </pre>
217: matches a portion of a subject string that is identical to itself. When
218: caseless matching is specified (the PCRE_CASELESS option), letters are matched
1.1.1.2 misho 219: independently of case. In a UTF mode, PCRE always understands the concept of
1.1 misho 220: case for characters whose values are less than 128, so caseless matching is
221: always possible. For characters with higher values, the concept of case is
222: supported if PCRE is compiled with Unicode property support, but not otherwise.
223: If you want to use caseless matching for characters 128 and above, you must
224: ensure that PCRE is compiled with Unicode property support as well as with
1.1.1.2 misho 225: UTF support.
1.1 misho 226: </P>
227: <P>
228: The power of regular expressions comes from the ability to include alternatives
229: and repetitions in the pattern. These are encoded in the pattern by the use of
230: <i>metacharacters</i>, which do not stand for themselves but instead are
231: interpreted in some special way.
232: </P>
233: <P>
234: There are two different sets of metacharacters: those that are recognized
235: anywhere in the pattern except within square brackets, and those that are
236: recognized within square brackets. Outside square brackets, the metacharacters
237: are as follows:
238: <pre>
239: \ general escape character with several uses
240: ^ assert start of string (or line, in multiline mode)
241: $ assert end of string (or line, in multiline mode)
242: . match any character except newline (by default)
243: [ start character class definition
244: | start of alternative branch
245: ( start subpattern
246: ) end subpattern
247: ? extends the meaning of (
248: also 0 or 1 quantifier
249: also quantifier minimizer
250: * 0 or more quantifier
251: + 1 or more quantifier
252: also "possessive quantifier"
253: { start min/max quantifier
254: </pre>
255: Part of a pattern that is in square brackets is called a "character class". In
256: a character class the only metacharacters are:
257: <pre>
258: \ general escape character
259: ^ negate the class, but only if the first character
260: - indicates character range
261: [ POSIX character class (only if followed by POSIX syntax)
262: ] terminates the character class
263: </pre>
264: The following sections describe the use of each of the metacharacters.
265: </P>
1.1.1.4 ! misho 266: <br><a name="SEC5" href="#TOC1">BACKSLASH</a><br>
1.1 misho 267: <P>
268: The backslash character has several uses. Firstly, if it is followed by a
269: character that is not a number or a letter, it takes away any special meaning
270: that character may have. This use of backslash as an escape character applies
271: both inside and outside character classes.
272: </P>
273: <P>
274: For example, if you want to match a * character, you write \* in the pattern.
275: This escaping action applies whether or not the following character would
276: otherwise be interpreted as a metacharacter, so it is always safe to precede a
277: non-alphanumeric with backslash to specify that it stands for itself. In
278: particular, if you want to match a backslash, you write \\.
279: </P>
280: <P>
1.1.1.2 misho 281: In a UTF mode, only ASCII numbers and letters have any special meaning after a
1.1 misho 282: backslash. All other characters (in particular, those whose codepoints are
283: greater than 127) are treated as literals.
284: </P>
285: <P>
1.1.1.3 misho 286: If a pattern is compiled with the PCRE_EXTENDED option, white space in the
1.1 misho 287: pattern (other than in a character class) and characters between a # outside
288: a character class and the next newline are ignored. An escaping backslash can
1.1.1.3 misho 289: be used to include a white space or # character as part of the pattern.
1.1 misho 290: </P>
291: <P>
292: If you want to remove the special meaning from a sequence of characters, you
293: can do so by putting them between \Q and \E. This is different from Perl in
294: that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in
295: Perl, $ and @ cause variable interpolation. Note the following examples:
296: <pre>
297: Pattern PCRE matches Perl matches
298:
299: \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
300: \Qabc\$xyz\E abc\$xyz abc\$xyz
301: \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
302: </pre>
303: The \Q...\E sequence is recognized both inside and outside character classes.
304: An isolated \E that is not preceded by \Q is ignored. If \Q is not followed
305: by \E later in the pattern, the literal interpretation continues to the end of
306: the pattern (that is, \E is assumed at the end). If the isolated \Q is inside
307: a character class, this causes an error, because the character class is not
308: terminated.
309: <a name="digitsafterbackslash"></a></P>
310: <br><b>
311: Non-printing characters
312: </b><br>
313: <P>
314: A second use of backslash provides a way of encoding non-printing characters
315: in patterns in a visible manner. There is no restriction on the appearance of
316: non-printing characters, apart from the binary zero that terminates a pattern,
317: but when a pattern is being prepared by text editing, it is often easier to use
318: one of the following escape sequences than the binary character it represents:
319: <pre>
320: \a alarm, that is, the BEL character (hex 07)
321: \cx "control-x", where x is any ASCII character
322: \e escape (hex 1B)
1.1.1.3 misho 323: \f form feed (hex 0C)
1.1 misho 324: \n linefeed (hex 0A)
325: \r carriage return (hex 0D)
326: \t tab (hex 09)
327: \ddd character with octal code ddd, or back reference
328: \xhh character with hex code hh
329: \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
330: \uhhhh character with hex code hhhh (JavaScript mode only)
331: </pre>
1.1.1.4 ! misho 332: The precise effect of \cx on ASCII characters is as follows: if x is a lower
! 333: case letter, it is converted to upper case. Then bit 6 of the character (hex
! 334: 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A (A is 41, Z is 5A),
! 335: but \c{ becomes hex 3B ({ is 7B), and \c; becomes hex 7B (; is 3B). If the
! 336: data item (byte or 16-bit value) following \c has a value greater than 127, a
! 337: compile-time error occurs. This locks out non-ASCII characters in all modes.
! 338: </P>
! 339: <P>
! 340: The \c facility was designed for use with ASCII characters, but with the
! 341: extension to Unicode it is even less useful than it once was. It is, however,
! 342: recognized when PCRE is compiled in EBCDIC mode, where data items are always
! 343: bytes. In this mode, all values are valid after \c. If the next character is a
! 344: lower case letter, it is converted to upper case. Then the 0xc0 bits of the
! 345: byte are inverted. Thus \cA becomes hex 01, as in ASCII (A is C1), but because
! 346: the EBCDIC letters are disjoint, \cZ becomes hex 29 (Z is E9), and other
! 347: characters also generate different values.
1.1 misho 348: </P>
349: <P>
350: By default, after \x, from zero to two hexadecimal digits are read (letters
351: can be in upper or lower case). Any number of hexadecimal digits may appear
1.1.1.2 misho 352: between \x{ and }, but the character code is constrained as follows:
353: <pre>
354: 8-bit non-UTF mode less than 0x100
355: 8-bit UTF-8 mode less than 0x10ffff and a valid codepoint
356: 16-bit non-UTF mode less than 0x10000
357: 16-bit UTF-16 mode less than 0x10ffff and a valid codepoint
1.1.1.4 ! misho 358: 32-bit non-UTF mode less than 0x80000000
! 359: 32-bit UTF-32 mode less than 0x10ffff and a valid codepoint
1.1.1.2 misho 360: </pre>
361: Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so-called
1.1.1.4 ! misho 362: "surrogate" codepoints), and 0xffef.
1.1 misho 363: </P>
364: <P>
365: If characters other than hexadecimal digits appear between \x{ and }, or if
366: there is no terminating }, this form of escape is not recognized. Instead, the
367: initial \x will be interpreted as a basic hexadecimal escape, with no
368: following digits, giving a character whose value is zero.
369: </P>
370: <P>
371: If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x is
372: as just described only when it is followed by two hexadecimal digits.
373: Otherwise, it matches a literal "x" character. In JavaScript mode, support for
374: code points greater than 256 is provided by \u, which must be followed by
375: four hexadecimal digits; otherwise it matches a literal "u" character.
1.1.1.3 misho 376: Character codes specified by \u in JavaScript mode are constrained in the same
377: was as those specified by \x in non-JavaScript mode.
1.1 misho 378: </P>
379: <P>
380: Characters whose value is less than 256 can be defined by either of the two
381: syntaxes for \x (or by \u in JavaScript mode). There is no difference in the
382: way they are handled. For example, \xdc is exactly the same as \x{dc} (or
383: \u00dc in JavaScript mode).
384: </P>
385: <P>
386: After \0 up to two further octal digits are read. If there are fewer than two
387: digits, just those that are present are used. Thus the sequence \0\x\07
388: specifies two binary zeros followed by a BEL character (code value 7). Make
389: sure you supply two digits after the initial zero if the pattern character that
390: follows is itself an octal digit.
391: </P>
392: <P>
393: The handling of a backslash followed by a digit other than 0 is complicated.
394: Outside a character class, PCRE reads it and any following digits as a decimal
395: number. If the number is less than 10, or if there have been at least that many
396: previous capturing left parentheses in the expression, the entire sequence is
397: taken as a <i>back reference</i>. A description of how this works is given
398: <a href="#backreferences">later,</a>
399: following the discussion of
400: <a href="#subpattern">parenthesized subpatterns.</a>
401: </P>
402: <P>
403: Inside a character class, or if the decimal number is greater than 9 and there
404: have not been that many capturing subpatterns, PCRE re-reads up to three octal
405: digits following the backslash, and uses them to generate a data character. Any
1.1.1.2 misho 406: subsequent digits stand for themselves. The value of the character is
407: constrained in the same way as characters specified in hexadecimal.
408: For example:
1.1 misho 409: <pre>
1.1.1.4 ! misho 410: \040 is another way of writing an ASCII space
1.1 misho 411: \40 is the same, provided there are fewer than 40 previous capturing subpatterns
412: \7 is always a back reference
413: \11 might be a back reference, or another way of writing a tab
414: \011 is always a tab
415: \0113 is a tab followed by the character "3"
416: \113 might be a back reference, otherwise the character with octal code 113
1.1.1.2 misho 417: \377 might be a back reference, otherwise the value 255 (decimal)
1.1 misho 418: \81 is either a back reference, or a binary zero followed by the two characters "8" and "1"
419: </pre>
420: Note that octal values of 100 or greater must not be introduced by a leading
421: zero, because no more than three octal digits are ever read.
422: </P>
423: <P>
424: All the sequences that define a single character value can be used both inside
425: and outside character classes. In addition, inside a character class, \b is
426: interpreted as the backspace character (hex 08).
427: </P>
428: <P>
429: \N is not allowed in a character class. \B, \R, and \X are not special
430: inside a character class. Like other unrecognized escape sequences, they are
431: treated as the literal characters "B", "R", and "X" by default, but cause an
432: error if the PCRE_EXTRA option is set. Outside a character class, these
433: sequences have different meanings.
434: </P>
435: <br><b>
436: Unsupported escape sequences
437: </b><br>
438: <P>
439: In Perl, the sequences \l, \L, \u, and \U are recognized by its string
440: handler and used to modify the case of following characters. By default, PCRE
441: does not support these escape sequences. However, if the PCRE_JAVASCRIPT_COMPAT
442: option is set, \U matches a "U" character, and \u can be used to define a
443: character by code point, as described in the previous section.
444: </P>
445: <br><b>
446: Absolute and relative back references
447: </b><br>
448: <P>
449: The sequence \g followed by an unsigned or a negative number, optionally
450: enclosed in braces, is an absolute or relative back reference. A named back
451: reference can be coded as \g{name}. Back references are discussed
452: <a href="#backreferences">later,</a>
453: following the discussion of
454: <a href="#subpattern">parenthesized subpatterns.</a>
455: </P>
456: <br><b>
457: Absolute and relative subroutine calls
458: </b><br>
459: <P>
460: For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
461: a number enclosed either in angle brackets or single quotes, is an alternative
462: syntax for referencing a subpattern as a "subroutine". Details are discussed
463: <a href="#onigurumasubroutines">later.</a>
464: Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i>
465: synonymous. The former is a back reference; the latter is a
466: <a href="#subpatternsassubroutines">subroutine</a>
467: call.
468: <a name="genericchartypes"></a></P>
469: <br><b>
470: Generic character types
471: </b><br>
472: <P>
473: Another use of backslash is for specifying generic character types:
474: <pre>
475: \d any decimal digit
476: \D any character that is not a decimal digit
1.1.1.3 misho 477: \h any horizontal white space character
478: \H any character that is not a horizontal white space character
479: \s any white space character
480: \S any character that is not a white space character
481: \v any vertical white space character
482: \V any character that is not a vertical white space character
1.1 misho 483: \w any "word" character
484: \W any "non-word" character
485: </pre>
486: There is also the single sequence \N, which matches a non-newline character.
487: This is the same as
488: <a href="#fullstopdot">the "." metacharacter</a>
489: when PCRE_DOTALL is not set. Perl also uses \N to match characters by name;
490: PCRE does not support this.
491: </P>
492: <P>
493: Each pair of lower and upper case escape sequences partitions the complete set
494: of characters into two disjoint sets. Any given character matches one, and only
495: one, of each pair. The sequences can appear both inside and outside character
496: classes. They each match one character of the appropriate type. If the current
497: matching point is at the end of the subject string, all of them fail, because
498: there is no character to match.
499: </P>
500: <P>
501: For compatibility with Perl, \s does not match the VT character (code 11).
502: This makes it different from the the POSIX "space" class. The \s characters
503: are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is
504: included in a Perl script, \s may match the VT character. In PCRE, it never
505: does.
506: </P>
507: <P>
508: A "word" character is an underscore or any character that is a letter or digit.
509: By default, the definition of letters and digits is controlled by PCRE's
510: low-valued character tables, and may vary if locale-specific matching is taking
511: place (see
512: <a href="pcreapi.html#localesupport">"Locale support"</a>
513: in the
514: <a href="pcreapi.html"><b>pcreapi</b></a>
515: page). For example, in a French locale such as "fr_FR" in Unix-like systems,
516: or "french" in Windows, some character codes greater than 128 are used for
517: accented letters, and these are then matched by \w. The use of locales with
518: Unicode is discouraged.
519: </P>
520: <P>
1.1.1.2 misho 521: By default, in a UTF mode, characters with values greater than 128 never match
1.1 misho 522: \d, \s, or \w, and always match \D, \S, and \W. These sequences retain
1.1.1.2 misho 523: their original meanings from before UTF support was available, mainly for
1.1 misho 524: efficiency reasons. However, if PCRE is compiled with Unicode property support,
525: and the PCRE_UCP option is set, the behaviour is changed so that Unicode
526: properties are used to determine character types, as follows:
527: <pre>
528: \d any character that \p{Nd} matches (decimal digit)
529: \s any character that \p{Z} matches, plus HT, LF, FF, CR
530: \w any character that \p{L} or \p{N} matches, plus underscore
531: </pre>
532: The upper case escapes match the inverse sets of characters. Note that \d
533: matches only decimal digits, whereas \w matches any Unicode digit, as well as
534: any Unicode letter, and underscore. Note also that PCRE_UCP affects \b, and
535: \B because they are defined in terms of \w and \W. Matching these sequences
536: is noticeably slower when PCRE_UCP is set.
537: </P>
538: <P>
539: The sequences \h, \H, \v, and \V are features that were added to Perl at
540: release 5.10. In contrast to the other sequences, which match only ASCII
1.1.1.2 misho 541: characters by default, these always match certain high-valued codepoints,
542: whether or not PCRE_UCP is set. The horizontal space characters are:
1.1 misho 543: <pre>
1.1.1.4 ! misho 544: U+0009 Horizontal tab (HT)
1.1 misho 545: U+0020 Space
546: U+00A0 Non-break space
547: U+1680 Ogham space mark
548: U+180E Mongolian vowel separator
549: U+2000 En quad
550: U+2001 Em quad
551: U+2002 En space
552: U+2003 Em space
553: U+2004 Three-per-em space
554: U+2005 Four-per-em space
555: U+2006 Six-per-em space
556: U+2007 Figure space
557: U+2008 Punctuation space
558: U+2009 Thin space
559: U+200A Hair space
560: U+202F Narrow no-break space
561: U+205F Medium mathematical space
562: U+3000 Ideographic space
563: </pre>
564: The vertical space characters are:
565: <pre>
1.1.1.4 ! misho 566: U+000A Linefeed (LF)
! 567: U+000B Vertical tab (VT)
! 568: U+000C Form feed (FF)
! 569: U+000D Carriage return (CR)
! 570: U+0085 Next line (NEL)
1.1 misho 571: U+2028 Line separator
572: U+2029 Paragraph separator
1.1.1.2 misho 573: </pre>
574: In 8-bit, non-UTF-8 mode, only the characters with codepoints less than 256 are
575: relevant.
576: <a name="newlineseq"></a></P>
1.1 misho 577: <br><b>
578: Newline sequences
579: </b><br>
580: <P>
581: Outside a character class, by default, the escape sequence \R matches any
1.1.1.2 misho 582: Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent to the
583: following:
1.1 misho 584: <pre>
585: (?>\r\n|\n|\x0b|\f|\r|\x85)
586: </pre>
587: This is an example of an "atomic group", details of which are given
588: <a href="#atomicgroup">below.</a>
589: This particular group matches either the two-character sequence CR followed by
590: LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
1.1.1.3 misho 591: U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
1.1 misho 592: line, U+0085). The two-character sequence is treated as a single unit that
593: cannot be split.
594: </P>
595: <P>
1.1.1.2 misho 596: In other modes, two additional characters whose codepoints are greater than 255
1.1 misho 597: are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
598: Unicode character property support is not needed for these characters to be
599: recognized.
600: </P>
601: <P>
602: It is possible to restrict \R to match only CR, LF, or CRLF (instead of the
603: complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
604: either at compile time or when the pattern is matched. (BSR is an abbrevation
605: for "backslash R".) This can be made the default when PCRE is built; if this is
606: the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
607: It is also possible to specify these settings by starting a pattern string with
608: one of the following sequences:
609: <pre>
610: (*BSR_ANYCRLF) CR, LF, or CRLF only
611: (*BSR_UNICODE) any Unicode newline sequence
612: </pre>
1.1.1.2 misho 613: These override the default and the options given to the compiling function, but
614: they can themselves be overridden by options given to a matching function. Note
615: that these special settings, which are not Perl-compatible, are recognized only
616: at the very start of a pattern, and that they must be in upper case. If more
617: than one of them is present, the last one is used. They can be combined with a
618: change of newline convention; for example, a pattern can start with:
1.1 misho 619: <pre>
620: (*ANY)(*BSR_ANYCRLF)
621: </pre>
1.1.1.4 ! misho 622: They can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF) or
! 623: (*UCP) special sequences. Inside a character class, \R is treated as an
! 624: unrecognized escape sequence, and so matches the letter "R" by default, but
! 625: causes an error if PCRE_EXTRA is set.
1.1 misho 626: <a name="uniextseq"></a></P>
627: <br><b>
628: Unicode character properties
629: </b><br>
630: <P>
631: When PCRE is built with Unicode character property support, three additional
632: escape sequences that match characters with specific properties are available.
1.1.1.2 misho 633: When in 8-bit non-UTF-8 mode, these sequences are of course limited to testing
1.1 misho 634: characters whose codepoints are less than 256, but they do work in this mode.
635: The extra escape sequences are:
636: <pre>
637: \p{<i>xx</i>} a character with the <i>xx</i> property
638: \P{<i>xx</i>} a character without the <i>xx</i> property
1.1.1.4 ! misho 639: \X a Unicode extended grapheme cluster
1.1 misho 640: </pre>
641: The property names represented by <i>xx</i> above are limited to the Unicode
642: script names, the general category properties, "Any", which matches any
643: character (including newline), and some special PCRE properties (described
644: in the
645: <a href="#extraprops">next section).</a>
646: Other Perl properties such as "InMusicalSymbols" are not currently supported by
647: PCRE. Note that \P{Any} does not match any characters, so always causes a
648: match failure.
649: </P>
650: <P>
651: Sets of Unicode characters are defined as belonging to certain scripts. A
652: character from one of these sets can be matched using a script name. For
653: example:
654: <pre>
655: \p{Greek}
656: \P{Han}
657: </pre>
658: Those that are not part of an identified script are lumped together as
659: "Common". The current list of scripts is:
660: </P>
661: <P>
662: Arabic,
663: Armenian,
664: Avestan,
665: Balinese,
666: Bamum,
1.1.1.3 misho 667: Batak,
1.1 misho 668: Bengali,
669: Bopomofo,
1.1.1.3 misho 670: Brahmi,
1.1 misho 671: Braille,
672: Buginese,
673: Buhid,
674: Canadian_Aboriginal,
675: Carian,
1.1.1.3 misho 676: Chakma,
1.1 misho 677: Cham,
678: Cherokee,
679: Common,
680: Coptic,
681: Cuneiform,
682: Cypriot,
683: Cyrillic,
684: Deseret,
685: Devanagari,
686: Egyptian_Hieroglyphs,
687: Ethiopic,
688: Georgian,
689: Glagolitic,
690: Gothic,
691: Greek,
692: Gujarati,
693: Gurmukhi,
694: Han,
695: Hangul,
696: Hanunoo,
697: Hebrew,
698: Hiragana,
699: Imperial_Aramaic,
700: Inherited,
701: Inscriptional_Pahlavi,
702: Inscriptional_Parthian,
703: Javanese,
704: Kaithi,
705: Kannada,
706: Katakana,
707: Kayah_Li,
708: Kharoshthi,
709: Khmer,
710: Lao,
711: Latin,
712: Lepcha,
713: Limbu,
714: Linear_B,
715: Lisu,
716: Lycian,
717: Lydian,
718: Malayalam,
1.1.1.3 misho 719: Mandaic,
1.1 misho 720: Meetei_Mayek,
1.1.1.3 misho 721: Meroitic_Cursive,
722: Meroitic_Hieroglyphs,
723: Miao,
1.1 misho 724: Mongolian,
725: Myanmar,
726: New_Tai_Lue,
727: Nko,
728: Ogham,
729: Old_Italic,
730: Old_Persian,
731: Old_South_Arabian,
732: Old_Turkic,
733: Ol_Chiki,
734: Oriya,
735: Osmanya,
736: Phags_Pa,
737: Phoenician,
738: Rejang,
739: Runic,
740: Samaritan,
741: Saurashtra,
1.1.1.3 misho 742: Sharada,
1.1 misho 743: Shavian,
744: Sinhala,
1.1.1.3 misho 745: Sora_Sompeng,
1.1 misho 746: Sundanese,
747: Syloti_Nagri,
748: Syriac,
749: Tagalog,
750: Tagbanwa,
751: Tai_Le,
752: Tai_Tham,
753: Tai_Viet,
1.1.1.3 misho 754: Takri,
1.1 misho 755: Tamil,
756: Telugu,
757: Thaana,
758: Thai,
759: Tibetan,
760: Tifinagh,
761: Ugaritic,
762: Vai,
763: Yi.
764: </P>
765: <P>
766: Each character has exactly one Unicode general category property, specified by
767: a two-letter abbreviation. For compatibility with Perl, negation can be
768: specified by including a circumflex between the opening brace and the property
769: name. For example, \p{^Lu} is the same as \P{Lu}.
770: </P>
771: <P>
772: If only one letter is specified with \p or \P, it includes all the general
773: category properties that start with that letter. In this case, in the absence
774: of negation, the curly brackets in the escape sequence are optional; these two
775: examples have the same effect:
776: <pre>
777: \p{L}
778: \pL
779: </pre>
780: The following general category property codes are supported:
781: <pre>
782: C Other
783: Cc Control
784: Cf Format
785: Cn Unassigned
786: Co Private use
787: Cs Surrogate
788:
789: L Letter
790: Ll Lower case letter
791: Lm Modifier letter
792: Lo Other letter
793: Lt Title case letter
794: Lu Upper case letter
795:
796: M Mark
797: Mc Spacing mark
798: Me Enclosing mark
799: Mn Non-spacing mark
800:
801: N Number
802: Nd Decimal number
803: Nl Letter number
804: No Other number
805:
806: P Punctuation
807: Pc Connector punctuation
808: Pd Dash punctuation
809: Pe Close punctuation
810: Pf Final punctuation
811: Pi Initial punctuation
812: Po Other punctuation
813: Ps Open punctuation
814:
815: S Symbol
816: Sc Currency symbol
817: Sk Modifier symbol
818: Sm Mathematical symbol
819: So Other symbol
820:
821: Z Separator
822: Zl Line separator
823: Zp Paragraph separator
824: Zs Space separator
825: </pre>
826: The special property L& is also supported: it matches a character that has
827: the Lu, Ll, or Lt property, in other words, a letter that is not classified as
828: a modifier or "other".
829: </P>
830: <P>
831: The Cs (Surrogate) property applies only to characters in the range U+D800 to
1.1.1.2 misho 832: U+DFFF. Such characters are not valid in Unicode strings and so
833: cannot be tested by PCRE, unless UTF validity checking has been turned off
1.1.1.4 ! misho 834: (see the discussion of PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK and
! 835: PCRE_NO_UTF32_CHECK in the
1.1 misho 836: <a href="pcreapi.html"><b>pcreapi</b></a>
837: page). Perl does not support the Cs property.
838: </P>
839: <P>
840: The long synonyms for property names that Perl supports (such as \p{Letter})
841: are not supported by PCRE, nor is it permitted to prefix any of these
842: properties with "Is".
843: </P>
844: <P>
845: No character that is in the Unicode table has the Cn (unassigned) property.
846: Instead, this property is assumed for any code point that is not in the
847: Unicode table.
848: </P>
849: <P>
850: Specifying caseless matching does not affect these escape sequences. For
1.1.1.4 ! misho 851: example, \p{Lu} always matches only upper case letters. This is different from
! 852: the behaviour of current versions of Perl.
1.1 misho 853: </P>
854: <P>
1.1.1.4 ! misho 855: Matching characters by Unicode property is not fast, because PCRE has to do a
! 856: multistage table lookup in order to find a character's property. That is why
! 857: the traditional escape sequences such as \d and \w do not use Unicode
! 858: properties in PCRE by default, though you can make them do so by setting the
! 859: PCRE_UCP option or by starting the pattern with (*UCP).
! 860: </P>
! 861: <br><b>
! 862: Extended grapheme clusters
! 863: </b><br>
! 864: <P>
! 865: The \X escape matches any number of Unicode characters that form an "extended
! 866: grapheme cluster", and treats the sequence as an atomic group
! 867: <a href="#atomicgroup">(see below).</a>
! 868: Up to and including release 8.31, PCRE matched an earlier, simpler definition
! 869: that was equivalent to
1.1 misho 870: <pre>
871: (?>\PM\pM*)
872: </pre>
1.1.1.4 ! misho 873: That is, it matched a character without the "mark" property, followed by zero
! 874: or more characters with the "mark" property. Characters with the "mark"
! 875: property are typically non-spacing accents that affect the preceding character.
1.1 misho 876: </P>
877: <P>
1.1.1.4 ! misho 878: This simple definition was extended in Unicode to include more complicated
! 879: kinds of composite character by giving each character a grapheme breaking
! 880: property, and creating rules that use these properties to define the boundaries
! 881: of extended grapheme clusters. In releases of PCRE later than 8.31, \X matches
! 882: one of these clusters.
1.1 misho 883: </P>
884: <P>
1.1.1.4 ! misho 885: \X always matches at least one character. Then it decides whether to add
! 886: additional characters according to the following rules for ending a cluster:
! 887: </P>
! 888: <P>
! 889: 1. End at the end of the subject string.
! 890: </P>
! 891: <P>
! 892: 2. Do not end between CR and LF; otherwise end after any control character.
! 893: </P>
! 894: <P>
! 895: 3. Do not break Hangul (a Korean script) syllable sequences. Hangul characters
! 896: are of five types: L, V, T, LV, and LVT. An L character may be followed by an
! 897: L, V, LV, or LVT character; an LV or V character may be followed by a V or T
! 898: character; an LVT or T character may be follwed only by a T character.
! 899: </P>
! 900: <P>
! 901: 4. Do not end before extending characters or spacing marks. Characters with
! 902: the "mark" property always have the "extend" grapheme breaking property.
! 903: </P>
! 904: <P>
! 905: 5. Do not end after prepend characters.
! 906: </P>
! 907: <P>
! 908: 6. Otherwise, end the cluster.
1.1 misho 909: <a name="extraprops"></a></P>
910: <br><b>
911: PCRE's additional properties
912: </b><br>
913: <P>
1.1.1.4 ! misho 914: As well as the standard Unicode properties described above, PCRE supports four
! 915: more that make it possible to convert traditional escape sequences such as \w
! 916: and \s and POSIX character classes to use Unicode properties. PCRE uses these
! 917: non-standard, non-Perl properties internally when PCRE_UCP is set. However,
! 918: they may also be used explicitly. These properties are:
1.1 misho 919: <pre>
920: Xan Any alphanumeric character
921: Xps Any POSIX space character
922: Xsp Any Perl space character
923: Xwd Any Perl "word" character
924: </pre>
925: Xan matches characters that have either the L (letter) or the N (number)
1.1.1.3 misho 926: property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
1.1 misho 927: carriage return, and any other character that has the Z (separator) property.
928: Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the
929: same characters as Xan, plus underscore.
1.1.1.4 ! misho 930: </P>
! 931: <P>
! 932: There is another non-standard property, Xuc, which matches any character that
! 933: can be represented by a Universal Character Name in C++ and other programming
! 934: languages. These are the characters $, @, ` (grave accent), and all characters
! 935: with Unicode code points greater than or equal to U+00A0, except for the
! 936: surrogates U+D800 to U+DFFF. Note that most base (ASCII) characters are
! 937: excluded. (Universal Character Names are of the form \uHHHH or \UHHHHHHHH
! 938: where H is a hexadecimal digit. Note that the Xuc property does not match these
! 939: sequences but the characters that they represent.)
1.1 misho 940: <a name="resetmatchstart"></a></P>
941: <br><b>
942: Resetting the match start
943: </b><br>
944: <P>
945: The escape sequence \K causes any previously matched characters not to be
946: included in the final matched sequence. For example, the pattern:
947: <pre>
948: foo\Kbar
949: </pre>
950: matches "foobar", but reports that it has matched "bar". This feature is
951: similar to a lookbehind assertion
952: <a href="#lookbehind">(described below).</a>
953: However, in this case, the part of the subject before the real match does not
954: have to be of fixed length, as lookbehind assertions do. The use of \K does
955: not interfere with the setting of
956: <a href="#subpattern">captured substrings.</a>
957: For example, when the pattern
958: <pre>
959: (foo)\Kbar
960: </pre>
961: matches "foobar", the first substring is still set to "foo".
962: </P>
963: <P>
964: Perl documents that the use of \K within assertions is "not well defined". In
965: PCRE, \K is acted upon when it occurs inside positive assertions, but is
966: ignored in negative assertions.
967: <a name="smallassertions"></a></P>
968: <br><b>
969: Simple assertions
970: </b><br>
971: <P>
972: The final use of backslash is for certain simple assertions. An assertion
973: specifies a condition that has to be met at a particular point in a match,
974: without consuming any characters from the subject string. The use of
975: subpatterns for more complicated assertions is described
976: <a href="#bigassertions">below.</a>
977: The backslashed assertions are:
978: <pre>
979: \b matches at a word boundary
980: \B matches when not at a word boundary
981: \A matches at the start of the subject
982: \Z matches at the end of the subject
983: also matches before a newline at the end of the subject
984: \z matches only at the end of the subject
985: \G matches at the first matching position in the subject
986: </pre>
987: Inside a character class, \b has a different meaning; it matches the backspace
988: character. If any other of these assertions appears in a character class, by
989: default it matches the corresponding literal character (for example, \B
990: matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
991: escape sequence" error is generated instead.
992: </P>
993: <P>
994: A word boundary is a position in the subject string where the current character
995: and the previous character do not both match \w or \W (i.e. one matches
996: \w and the other matches \W), or the start or end of the string if the
1.1.1.2 misho 997: first or last character matches \w, respectively. In a UTF mode, the meanings
1.1 misho 998: of \w and \W can be changed by setting the PCRE_UCP option. When this is
999: done, it also affects \b and \B. Neither PCRE nor Perl has a separate "start
1000: of word" or "end of word" metasequence. However, whatever follows \b normally
1001: determines which it is. For example, the fragment \ba matches "a" at the start
1002: of a word.
1003: </P>
1004: <P>
1005: The \A, \Z, and \z assertions differ from the traditional circumflex and
1006: dollar (described in the next section) in that they only ever match at the very
1007: start and end of the subject string, whatever options are set. Thus, they are
1008: independent of multiline mode. These three assertions are not affected by the
1009: PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
1010: circumflex and dollar metacharacters. However, if the <i>startoffset</i>
1011: argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start
1012: at a point other than the beginning of the subject, \A can never match. The
1013: difference between \Z and \z is that \Z matches before a newline at the end
1014: of the string as well as at the very end, whereas \z matches only at the end.
1015: </P>
1016: <P>
1017: The \G assertion is true only when the current matching position is at the
1018: start point of the match, as specified by the <i>startoffset</i> argument of
1019: <b>pcre_exec()</b>. It differs from \A when the value of <i>startoffset</i> is
1020: non-zero. By calling <b>pcre_exec()</b> multiple times with appropriate
1021: arguments, you can mimic Perl's /g option, and it is in this kind of
1022: implementation where \G can be useful.
1023: </P>
1024: <P>
1025: Note, however, that PCRE's interpretation of \G, as the start of the current
1026: match, is subtly different from Perl's, which defines it as the end of the
1027: previous match. In Perl, these can be different when the previously matched
1028: string was empty. Because PCRE does just one match at a time, it cannot
1029: reproduce this behaviour.
1030: </P>
1031: <P>
1032: If all the alternatives of a pattern begin with \G, the expression is anchored
1033: to the starting match position, and the "anchored" flag is set in the compiled
1034: regular expression.
1035: </P>
1.1.1.4 ! misho 1036: <br><a name="SEC6" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
! 1037: <P>
! 1038: The circumflex and dollar metacharacters are zero-width assertions. That is,
! 1039: they test for a particular condition being true without consuming any
! 1040: characters from the subject string.
! 1041: </P>
1.1 misho 1042: <P>
1043: Outside a character class, in the default matching mode, the circumflex
1.1.1.4 ! misho 1044: character is an assertion that is true only if the current matching point is at
! 1045: the start of the subject string. If the <i>startoffset</i> argument of
1.1 misho 1046: <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE
1047: option is unset. Inside a character class, circumflex has an entirely different
1048: meaning
1049: <a href="#characterclass">(see below).</a>
1050: </P>
1051: <P>
1052: Circumflex need not be the first character of the pattern if a number of
1053: alternatives are involved, but it should be the first thing in each alternative
1054: in which it appears if the pattern is ever to match that branch. If all
1055: possible alternatives start with a circumflex, that is, if the pattern is
1056: constrained to match only at the start of the subject, it is said to be an
1057: "anchored" pattern. (There are also other constructs that can cause a pattern
1058: to be anchored.)
1059: </P>
1060: <P>
1.1.1.4 ! misho 1061: The dollar character is an assertion that is true only if the current matching
! 1062: point is at the end of the subject string, or immediately before a newline at
! 1063: the end of the string (by default). Note, however, that it does not actually
! 1064: match the newline. Dollar need not be the last character of the pattern if a
! 1065: number of alternatives are involved, but it should be the last item in any
! 1066: branch in which it appears. Dollar has no special meaning in a character class.
1.1 misho 1067: </P>
1068: <P>
1069: The meaning of dollar can be changed so that it matches only at the very end of
1070: the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
1071: does not affect the \Z assertion.
1072: </P>
1073: <P>
1074: The meanings of the circumflex and dollar characters are changed if the
1075: PCRE_MULTILINE option is set. When this is the case, a circumflex matches
1076: immediately after internal newlines as well as at the start of the subject
1077: string. It does not match after a newline that ends the string. A dollar
1078: matches before any newlines in the string, as well as at the very end, when
1079: PCRE_MULTILINE is set. When newline is specified as the two-character
1080: sequence CRLF, isolated CR and LF characters do not indicate newlines.
1081: </P>
1082: <P>
1083: For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
1084: \n represents a newline) in multiline mode, but not otherwise. Consequently,
1085: patterns that are anchored in single line mode because all branches start with
1086: ^ are not anchored in multiline mode, and a match for circumflex is possible
1087: when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The
1088: PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
1089: </P>
1090: <P>
1091: Note that the sequences \A, \Z, and \z can be used to match the start and
1092: end of the subject in both modes, and if all branches of a pattern start with
1093: \A it is always anchored, whether or not PCRE_MULTILINE is set.
1094: <a name="fullstopdot"></a></P>
1.1.1.4 ! misho 1095: <br><a name="SEC7" href="#TOC1">FULL STOP (PERIOD, DOT) AND \N</a><br>
1.1 misho 1096: <P>
1097: Outside a character class, a dot in the pattern matches any one character in
1098: the subject string except (by default) a character that signifies the end of a
1.1.1.2 misho 1099: line.
1.1 misho 1100: </P>
1101: <P>
1102: When a line ending is defined as a single character, dot never matches that
1103: character; when the two-character sequence CRLF is used, dot does not match CR
1104: if it is immediately followed by LF, but otherwise it matches all characters
1105: (including isolated CRs and LFs). When any Unicode line endings are being
1106: recognized, dot does not match CR or LF or any of the other line ending
1107: characters.
1108: </P>
1109: <P>
1110: The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
1111: option is set, a dot matches any one character, without exception. If the
1112: two-character sequence CRLF is present in the subject string, it takes two dots
1113: to match it.
1114: </P>
1115: <P>
1116: The handling of dot is entirely independent of the handling of circumflex and
1117: dollar, the only relationship being that they both involve newlines. Dot has no
1118: special meaning in a character class.
1119: </P>
1120: <P>
1121: The escape sequence \N behaves like a dot, except that it is not affected by
1122: the PCRE_DOTALL option. In other words, it matches any character except one
1123: that signifies the end of a line. Perl also uses \N to match characters by
1124: name; PCRE does not support this.
1125: </P>
1.1.1.4 ! misho 1126: <br><a name="SEC8" href="#TOC1">MATCHING A SINGLE DATA UNIT</a><br>
1.1 misho 1127: <P>
1.1.1.2 misho 1128: Outside a character class, the escape sequence \C matches any one data unit,
1129: whether or not a UTF mode is set. In the 8-bit library, one data unit is one
1.1.1.4 ! misho 1130: byte; in the 16-bit library it is a 16-bit unit; in the 32-bit library it is
! 1131: a 32-bit unit. Unlike a dot, \C always
1.1.1.2 misho 1132: matches line-ending characters. The feature is provided in Perl in order to
1133: match individual bytes in UTF-8 mode, but it is unclear how it can usefully be
1134: used. Because \C breaks up characters into individual data units, matching one
1135: unit with \C in a UTF mode means that the rest of the string may start with a
1136: malformed UTF character. This has undefined results, because PCRE assumes that
1137: it is dealing with valid UTF strings (and by default it checks this at the
1.1.1.4 ! misho 1138: start of processing unless the PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK or
! 1139: PCRE_NO_UTF32_CHECK option is used).
1.1 misho 1140: </P>
1141: <P>
1142: PCRE does not allow \C to appear in lookbehind assertions
1143: <a href="#lookbehind">(described below)</a>
1.1.1.2 misho 1144: in a UTF mode, because this would make it impossible to calculate the length of
1.1 misho 1145: the lookbehind.
1146: </P>
1147: <P>
1.1.1.2 misho 1148: In general, the \C escape sequence is best avoided. However, one
1149: way of using it that avoids the problem of malformed UTF characters is to use a
1150: lookahead to check the length of the next character, as in this pattern, which
1151: could be used with a UTF-8 string (ignore white space and line breaks):
1.1 misho 1152: <pre>
1153: (?| (?=[\x00-\x7f])(\C) |
1154: (?=[\x80-\x{7ff}])(\C)(\C) |
1155: (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
1156: (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
1157: </pre>
1158: A group that starts with (?| resets the capturing parentheses numbers in each
1159: alternative (see
1160: <a href="#dupsubpatternnumber">"Duplicate Subpattern Numbers"</a>
1161: below). The assertions at the start of each branch check the next UTF-8
1162: character for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
1163: character's individual bytes are then captured by the appropriate number of
1164: groups.
1165: <a name="characterclass"></a></P>
1.1.1.4 ! misho 1166: <br><a name="SEC9" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
1.1 misho 1167: <P>
1168: An opening square bracket introduces a character class, terminated by a closing
1169: square bracket. A closing square bracket on its own is not special by default.
1170: However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square
1171: bracket causes a compile-time error. If a closing square bracket is required as
1172: a member of the class, it should be the first data character in the class
1173: (after an initial circumflex, if present) or escaped with a backslash.
1174: </P>
1175: <P>
1.1.1.2 misho 1176: A character class matches a single character in the subject. In a UTF mode, the
1177: character may be more than one data unit long. A matched character must be in
1178: the set of characters defined by the class, unless the first character in the
1179: class definition is a circumflex, in which case the subject character must not
1180: be in the set defined by the class. If a circumflex is actually required as a
1181: member of the class, ensure it is not the first character, or escape it with a
1.1 misho 1182: backslash.
1183: </P>
1184: <P>
1185: For example, the character class [aeiou] matches any lower case vowel, while
1186: [^aeiou] matches any character that is not a lower case vowel. Note that a
1187: circumflex is just a convenient notation for specifying the characters that
1188: are in the class by enumerating those that are not. A class that starts with a
1189: circumflex is not an assertion; it still consumes a character from the subject
1190: string, and therefore it fails if the current pointer is at the end of the
1191: string.
1192: </P>
1193: <P>
1.1.1.4 ! misho 1194: In UTF-8 (UTF-16, UTF-32) mode, characters with values greater than 255 (0xffff)
! 1195: can be included in a class as a literal string of data units, or by using the
! 1196: \x{ escaping mechanism.
1.1 misho 1197: </P>
1198: <P>
1199: When caseless matching is set, any letters in a class represent both their
1200: upper case and lower case versions, so for example, a caseless [aeiou] matches
1201: "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
1.1.1.2 misho 1202: caseful version would. In a UTF mode, PCRE always understands the concept of
1.1 misho 1203: case for characters whose values are less than 128, so caseless matching is
1204: always possible. For characters with higher values, the concept of case is
1205: supported if PCRE is compiled with Unicode property support, but not otherwise.
1.1.1.2 misho 1206: If you want to use caseless matching in a UTF mode for characters 128 and
1207: above, you must ensure that PCRE is compiled with Unicode property support as
1208: well as with UTF support.
1.1 misho 1209: </P>
1210: <P>
1211: Characters that might indicate line breaks are never treated in any special way
1212: when matching character classes, whatever line-ending sequence is in use, and
1213: whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
1214: such as [^a] always matches one of these characters.
1215: </P>
1216: <P>
1217: The minus (hyphen) character can be used to specify a range of characters in a
1218: character class. For example, [d-m] matches any letter between d and m,
1219: inclusive. If a minus character is required in a class, it must be escaped with
1220: a backslash or appear in a position where it cannot be interpreted as
1221: indicating a range, typically as the first or last character in the class.
1222: </P>
1223: <P>
1224: It is not possible to have the literal character "]" as the end character of a
1225: range. A pattern such as [W-]46] is interpreted as a class of two characters
1226: ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
1227: "-46]". However, if the "]" is escaped with a backslash it is interpreted as
1228: the end of range, so [W-\]46] is interpreted as a class containing a range
1229: followed by two other characters. The octal or hexadecimal representation of
1230: "]" can also be used to end a range.
1231: </P>
1232: <P>
1233: Ranges operate in the collating sequence of character values. They can also be
1.1.1.2 misho 1234: used for characters specified numerically, for example [\000-\037]. Ranges
1235: can include any characters that are valid for the current mode.
1.1 misho 1236: </P>
1237: <P>
1238: If a range that includes letters is used when caseless matching is set, it
1239: matches the letters in either case. For example, [W-c] is equivalent to
1.1.1.2 misho 1240: [][\\^_`wxyzabc], matched caselessly, and in a non-UTF mode, if character
1.1 misho 1241: tables for a French locale are in use, [\xc8-\xcb] matches accented E
1.1.1.2 misho 1242: characters in both cases. In UTF modes, PCRE supports the concept of case for
1.1 misho 1243: characters with values greater than 128 only when it is compiled with Unicode
1244: property support.
1245: </P>
1246: <P>
1247: The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v,
1248: \V, \w, and \W may appear in a character class, and add the characters that
1249: they match to the class. For example, [\dABCDEF] matches any hexadecimal
1.1.1.2 misho 1250: digit. In UTF modes, the PCRE_UCP option affects the meanings of \d, \s, \w
1.1 misho 1251: and their upper case partners, just as it does when they appear outside a
1252: character class, as described in the section entitled
1253: <a href="#genericchartypes">"Generic character types"</a>
1254: above. The escape sequence \b has a different meaning inside a character
1255: class; it matches the backspace character. The sequences \B, \N, \R, and \X
1256: are not special inside a character class. Like any other unrecognized escape
1257: sequences, they are treated as the literal characters "B", "N", "R", and "X" by
1258: default, but cause an error if the PCRE_EXTRA option is set.
1259: </P>
1260: <P>
1261: A circumflex can conveniently be used with the upper case character types to
1262: specify a more restricted set of characters than the matching lower case type.
1263: For example, the class [^\W_] matches any letter or digit, but not underscore,
1264: whereas [\w] includes underscore. A positive character class should be read as
1265: "something OR something OR ..." and a negative class as "NOT something AND NOT
1266: something AND NOT ...".
1267: </P>
1268: <P>
1269: The only metacharacters that are recognized in character classes are backslash,
1270: hyphen (only where it can be interpreted as specifying a range), circumflex
1271: (only at the start), opening square bracket (only when it can be interpreted as
1272: introducing a POSIX class name - see the next section), and the terminating
1273: closing square bracket. However, escaping other non-alphanumeric characters
1274: does no harm.
1275: </P>
1.1.1.4 ! misho 1276: <br><a name="SEC10" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
1.1 misho 1277: <P>
1278: Perl supports the POSIX notation for character classes. This uses names
1279: enclosed by [: and :] within the enclosing square brackets. PCRE also supports
1280: this notation. For example,
1281: <pre>
1282: [01[:alpha:]%]
1283: </pre>
1284: matches "0", "1", any alphabetic character, or "%". The supported class names
1285: are:
1286: <pre>
1287: alnum letters and digits
1288: alpha letters
1289: ascii character codes 0 - 127
1290: blank space or tab only
1291: cntrl control characters
1292: digit decimal digits (same as \d)
1293: graph printing characters, excluding space
1294: lower lower case letters
1295: print printing characters, including space
1296: punct printing characters, excluding letters and digits and space
1297: space white space (not quite the same as \s)
1298: upper upper case letters
1299: word "word" characters (same as \w)
1300: xdigit hexadecimal digits
1301: </pre>
1302: The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
1303: space (32). Notice that this list includes the VT character (code 11). This
1304: makes "space" different to \s, which does not include VT (for Perl
1305: compatibility).
1306: </P>
1307: <P>
1308: The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
1309: 5.8. Another Perl extension is negation, which is indicated by a ^ character
1310: after the colon. For example,
1311: <pre>
1312: [12[:^digit:]]
1313: </pre>
1314: matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
1315: syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
1316: supported, and an error is given if they are encountered.
1317: </P>
1318: <P>
1.1.1.2 misho 1319: By default, in UTF modes, characters with values greater than 128 do not match
1.1 misho 1320: any of the POSIX character classes. However, if the PCRE_UCP option is passed
1321: to <b>pcre_compile()</b>, some of the classes are changed so that Unicode
1322: character properties are used. This is achieved by replacing the POSIX classes
1323: by other sequences, as follows:
1324: <pre>
1325: [:alnum:] becomes \p{Xan}
1326: [:alpha:] becomes \p{L}
1327: [:blank:] becomes \h
1328: [:digit:] becomes \p{Nd}
1329: [:lower:] becomes \p{Ll}
1330: [:space:] becomes \p{Xps}
1331: [:upper:] becomes \p{Lu}
1332: [:word:] becomes \p{Xwd}
1333: </pre>
1334: Negated versions, such as [:^alpha:] use \P instead of \p. The other POSIX
1335: classes are unchanged, and match only characters with code points less than
1336: 128.
1337: </P>
1.1.1.4 ! misho 1338: <br><a name="SEC11" href="#TOC1">VERTICAL BAR</a><br>
1.1 misho 1339: <P>
1340: Vertical bar characters are used to separate alternative patterns. For example,
1341: the pattern
1342: <pre>
1343: gilbert|sullivan
1344: </pre>
1345: matches either "gilbert" or "sullivan". Any number of alternatives may appear,
1346: and an empty alternative is permitted (matching the empty string). The matching
1347: process tries each alternative in turn, from left to right, and the first one
1348: that succeeds is used. If the alternatives are within a subpattern
1349: <a href="#subpattern">(defined below),</a>
1350: "succeeds" means matching the rest of the main pattern as well as the
1351: alternative in the subpattern.
1352: </P>
1.1.1.4 ! misho 1353: <br><a name="SEC12" href="#TOC1">INTERNAL OPTION SETTING</a><br>
1.1 misho 1354: <P>
1355: The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1356: PCRE_EXTENDED options (which are Perl-compatible) can be changed from within
1357: the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1358: The option letters are
1359: <pre>
1360: i for PCRE_CASELESS
1361: m for PCRE_MULTILINE
1362: s for PCRE_DOTALL
1363: x for PCRE_EXTENDED
1364: </pre>
1365: For example, (?im) sets caseless, multiline matching. It is also possible to
1366: unset these options by preceding the letter with a hyphen, and a combined
1367: setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
1368: PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED, is also
1369: permitted. If a letter appears both before and after the hyphen, the option is
1370: unset.
1371: </P>
1372: <P>
1373: The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
1374: changed in the same way as the Perl-compatible options by using the characters
1375: J, U and X respectively.
1376: </P>
1377: <P>
1378: When one of these option changes occurs at top level (that is, not inside
1379: subpattern parentheses), the change applies to the remainder of the pattern
1380: that follows. If the change is placed right at the start of a pattern, PCRE
1381: extracts it into the global options (and it will therefore show up in data
1382: extracted by the <b>pcre_fullinfo()</b> function).
1383: </P>
1384: <P>
1385: An option change within a subpattern (see below for a description of
1386: subpatterns) affects only that part of the subpattern that follows it, so
1387: <pre>
1388: (a(?i)b)c
1389: </pre>
1390: matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
1391: By this means, options can be made to have different settings in different
1392: parts of the pattern. Any changes made in one alternative do carry on
1393: into subsequent branches within the same subpattern. For example,
1394: <pre>
1395: (a(?i)b|c)
1396: </pre>
1397: matches "ab", "aB", "c", and "C", even though when matching "C" the first
1398: branch is abandoned before the option setting. This is because the effects of
1399: option settings happen at compile time. There would be some very weird
1400: behaviour otherwise.
1401: </P>
1402: <P>
1403: <b>Note:</b> There are other PCRE-specific options that can be set by the
1.1.1.2 misho 1404: application when the compiling or matching functions are called. In some cases
1405: the pattern can contain special leading sequences such as (*CRLF) to override
1406: what the application has set or what has been defaulted. Details are given in
1407: the section entitled
1.1 misho 1408: <a href="#newlineseq">"Newline sequences"</a>
1.1.1.4 ! misho 1409: above. There are also the (*UTF8), (*UTF16),(*UTF32), and (*UCP) leading
! 1410: sequences that can be used to set UTF and Unicode property modes; they are
! 1411: equivalent to setting the PCRE_UTF8, PCRE_UTF16, PCRE_UTF32 and the PCRE_UCP
! 1412: options, respectively. The (*UTF) sequence is a generic version that can be
! 1413: used with any of the libraries. However, the application can set the
! 1414: PCRE_NEVER_UTF option, which locks out the use of the (*UTF) sequences.
1.1 misho 1415: <a name="subpattern"></a></P>
1.1.1.4 ! misho 1416: <br><a name="SEC13" href="#TOC1">SUBPATTERNS</a><br>
1.1 misho 1417: <P>
1418: Subpatterns are delimited by parentheses (round brackets), which can be nested.
1419: Turning part of a pattern into a subpattern does two things:
1420: <br>
1421: <br>
1422: 1. It localizes a set of alternatives. For example, the pattern
1423: <pre>
1424: cat(aract|erpillar|)
1425: </pre>
1426: matches "cataract", "caterpillar", or "cat". Without the parentheses, it would
1427: match "cataract", "erpillar" or an empty string.
1428: <br>
1429: <br>
1430: 2. It sets up the subpattern as a capturing subpattern. This means that, when
1431: the whole pattern matches, that portion of the subject string that matched the
1.1.1.2 misho 1432: subpattern is passed back to the caller via the <i>ovector</i> argument of the
1433: matching function. (This applies only to the traditional matching functions;
1434: the DFA matching functions do not support capturing.)
1435: </P>
1436: <P>
1437: Opening parentheses are counted from left to right (starting from 1) to obtain
1438: numbers for the capturing subpatterns. For example, if the string "the red
1439: king" is matched against the pattern
1.1 misho 1440: <pre>
1441: the ((red|white) (king|queen))
1442: </pre>
1443: the captured substrings are "red king", "red", and "king", and are numbered 1,
1444: 2, and 3, respectively.
1445: </P>
1446: <P>
1447: The fact that plain parentheses fulfil two functions is not always helpful.
1448: There are often times when a grouping subpattern is required without a
1449: capturing requirement. If an opening parenthesis is followed by a question mark
1450: and a colon, the subpattern does not do any capturing, and is not counted when
1451: computing the number of any subsequent capturing subpatterns. For example, if
1452: the string "the white queen" is matched against the pattern
1453: <pre>
1454: the ((?:red|white) (king|queen))
1455: </pre>
1456: the captured substrings are "white queen" and "queen", and are numbered 1 and
1457: 2. The maximum number of capturing subpatterns is 65535.
1458: </P>
1459: <P>
1460: As a convenient shorthand, if any option settings are required at the start of
1461: a non-capturing subpattern, the option letters may appear between the "?" and
1462: the ":". Thus the two patterns
1463: <pre>
1464: (?i:saturday|sunday)
1465: (?:(?i)saturday|sunday)
1466: </pre>
1467: match exactly the same set of strings. Because alternative branches are tried
1468: from left to right, and options are not reset until the end of the subpattern
1469: is reached, an option setting in one branch does affect subsequent branches, so
1470: the above patterns match "SUNDAY" as well as "Saturday".
1471: <a name="dupsubpatternnumber"></a></P>
1.1.1.4 ! misho 1472: <br><a name="SEC14" href="#TOC1">DUPLICATE SUBPATTERN NUMBERS</a><br>
1.1 misho 1473: <P>
1474: Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
1475: the same numbers for its capturing parentheses. Such a subpattern starts with
1476: (?| and is itself a non-capturing subpattern. For example, consider this
1477: pattern:
1478: <pre>
1479: (?|(Sat)ur|(Sun))day
1480: </pre>
1481: Because the two alternatives are inside a (?| group, both sets of capturing
1482: parentheses are numbered one. Thus, when the pattern matches, you can look
1483: at captured substring number one, whichever alternative matched. This construct
1484: is useful when you want to capture part, but not all, of one of a number of
1485: alternatives. Inside a (?| group, parentheses are numbered as usual, but the
1486: number is reset at the start of each branch. The numbers of any capturing
1487: parentheses that follow the subpattern start after the highest number used in
1488: any branch. The following example is taken from the Perl documentation. The
1489: numbers underneath show in which buffer the captured content will be stored.
1490: <pre>
1491: # before ---------------branch-reset----------- after
1492: / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1493: # 1 2 2 3 2 3 4
1494: </pre>
1495: A back reference to a numbered subpattern uses the most recent value that is
1496: set for that number by any subpattern. The following pattern matches "abcabc"
1497: or "defdef":
1498: <pre>
1499: /(?|(abc)|(def))\1/
1500: </pre>
1501: In contrast, a subroutine call to a numbered subpattern always refers to the
1502: first one in the pattern with the given number. The following pattern matches
1503: "abcabc" or "defabc":
1504: <pre>
1505: /(?|(abc)|(def))(?1)/
1506: </pre>
1507: If a
1508: <a href="#conditions">condition test</a>
1509: for a subpattern's having matched refers to a non-unique number, the test is
1510: true if any of the subpatterns of that number have matched.
1511: </P>
1512: <P>
1513: An alternative approach to using this "branch reset" feature is to use
1514: duplicate named subpatterns, as described in the next section.
1515: </P>
1.1.1.4 ! misho 1516: <br><a name="SEC15" href="#TOC1">NAMED SUBPATTERNS</a><br>
1.1 misho 1517: <P>
1518: Identifying capturing parentheses by number is simple, but it can be very hard
1519: to keep track of the numbers in complicated regular expressions. Furthermore,
1520: if an expression is modified, the numbers may change. To help with this
1521: difficulty, PCRE supports the naming of subpatterns. This feature was not
1522: added to Perl until release 5.10. Python had the feature earlier, and PCRE
1523: introduced it at release 4.0, using the Python syntax. PCRE now supports both
1524: the Perl and the Python syntax. Perl allows identically numbered subpatterns to
1525: have different names, but PCRE does not.
1526: </P>
1527: <P>
1528: In PCRE, a subpattern can be named in one of three ways: (?<name>...) or
1529: (?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing
1530: parentheses from other parts of the pattern, such as
1531: <a href="#backreferences">back references,</a>
1532: <a href="#recursion">recursion,</a>
1533: and
1534: <a href="#conditions">conditions,</a>
1535: can be made by name as well as by number.
1536: </P>
1537: <P>
1538: Names consist of up to 32 alphanumeric characters and underscores. Named
1539: capturing parentheses are still allocated numbers as well as names, exactly as
1540: if the names were not present. The PCRE API provides function calls for
1541: extracting the name-to-number translation table from a compiled pattern. There
1542: is also a convenience function for extracting a captured substring by name.
1543: </P>
1544: <P>
1545: By default, a name must be unique within a pattern, but it is possible to relax
1546: this constraint by setting the PCRE_DUPNAMES option at compile time. (Duplicate
1547: names are also always permitted for subpatterns with the same number, set up as
1548: described in the previous section.) Duplicate names can be useful for patterns
1549: where only one instance of the named parentheses can match. Suppose you want to
1550: match the name of a weekday, either as a 3-letter abbreviation or as the full
1551: name, and in both cases you want to extract the abbreviation. This pattern
1552: (ignoring the line breaks) does the job:
1553: <pre>
1554: (?<DN>Mon|Fri|Sun)(?:day)?|
1555: (?<DN>Tue)(?:sday)?|
1556: (?<DN>Wed)(?:nesday)?|
1557: (?<DN>Thu)(?:rsday)?|
1558: (?<DN>Sat)(?:urday)?
1559: </pre>
1560: There are five capturing substrings, but only one is ever set after a match.
1561: (An alternative way of solving this problem is to use a "branch reset"
1562: subpattern, as described in the previous section.)
1563: </P>
1564: <P>
1565: The convenience function for extracting the data by name returns the substring
1566: for the first (and in this example, the only) subpattern of that name that
1567: matched. This saves searching to find which numbered subpattern it was.
1568: </P>
1569: <P>
1570: If you make a back reference to a non-unique named subpattern from elsewhere in
1571: the pattern, the one that corresponds to the first occurrence of the name is
1572: used. In the absence of duplicate numbers (see the previous section) this is
1573: the one with the lowest number. If you use a named reference in a condition
1574: test (see the
1575: <a href="#conditions">section about conditions</a>
1576: below), either to check whether a subpattern has matched, or to check for
1577: recursion, all subpatterns with the same name are tested. If the condition is
1578: true for any one of them, the overall condition is true. This is the same
1579: behaviour as testing by number. For further details of the interfaces for
1580: handling named subpatterns, see the
1581: <a href="pcreapi.html"><b>pcreapi</b></a>
1582: documentation.
1583: </P>
1584: <P>
1585: <b>Warning:</b> You cannot use different names to distinguish between two
1586: subpatterns with the same number because PCRE uses only the numbers when
1587: matching. For this reason, an error is given at compile time if different names
1588: are given to subpatterns with the same number. However, you can give the same
1589: name to subpatterns with the same number, even when PCRE_DUPNAMES is not set.
1590: </P>
1.1.1.4 ! misho 1591: <br><a name="SEC16" href="#TOC1">REPETITION</a><br>
1.1 misho 1592: <P>
1593: Repetition is specified by quantifiers, which can follow any of the following
1594: items:
1595: <pre>
1596: a literal data character
1597: the dot metacharacter
1598: the \C escape sequence
1.1.1.2 misho 1599: the \X escape sequence
1.1 misho 1600: the \R escape sequence
1601: an escape such as \d or \pL that matches a single character
1602: a character class
1603: a back reference (see next section)
1604: a parenthesized subpattern (including assertions)
1605: a subroutine call to a subpattern (recursive or otherwise)
1606: </pre>
1607: The general repetition quantifier specifies a minimum and maximum number of
1608: permitted matches, by giving the two numbers in curly brackets (braces),
1609: separated by a comma. The numbers must be less than 65536, and the first must
1610: be less than or equal to the second. For example:
1611: <pre>
1612: z{2,4}
1613: </pre>
1614: matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
1615: character. If the second number is omitted, but the comma is present, there is
1616: no upper limit; if the second number and the comma are both omitted, the
1617: quantifier specifies an exact number of required matches. Thus
1618: <pre>
1619: [aeiou]{3,}
1620: </pre>
1621: matches at least 3 successive vowels, but may match many more, while
1622: <pre>
1623: \d{8}
1624: </pre>
1625: matches exactly 8 digits. An opening curly bracket that appears in a position
1626: where a quantifier is not allowed, or one that does not match the syntax of a
1627: quantifier, is taken as a literal character. For example, {,6} is not a
1628: quantifier, but a literal string of four characters.
1629: </P>
1630: <P>
1.1.1.2 misho 1631: In UTF modes, quantifiers apply to characters rather than to individual data
1632: units. Thus, for example, \x{100}{2} matches two characters, each of
1633: which is represented by a two-byte sequence in a UTF-8 string. Similarly,
1.1.1.4 ! misho 1634: \X{3} matches three Unicode extended grapheme clusters, each of which may be
! 1635: several data units long (and they may be of different lengths).
1.1 misho 1636: </P>
1637: <P>
1638: The quantifier {0} is permitted, causing the expression to behave as if the
1639: previous item and the quantifier were not present. This may be useful for
1640: subpatterns that are referenced as
1641: <a href="#subpatternsassubroutines">subroutines</a>
1642: from elsewhere in the pattern (but see also the section entitled
1643: <a href="#subdefine">"Defining subpatterns for use by reference only"</a>
1644: below). Items other than subpatterns that have a {0} quantifier are omitted
1645: from the compiled pattern.
1646: </P>
1647: <P>
1648: For convenience, the three most common quantifiers have single-character
1649: abbreviations:
1650: <pre>
1651: * is equivalent to {0,}
1652: + is equivalent to {1,}
1653: ? is equivalent to {0,1}
1654: </pre>
1655: It is possible to construct infinite loops by following a subpattern that can
1656: match no characters with a quantifier that has no upper limit, for example:
1657: <pre>
1658: (a?)*
1659: </pre>
1660: Earlier versions of Perl and PCRE used to give an error at compile time for
1661: such patterns. However, because there are cases where this can be useful, such
1662: patterns are now accepted, but if any repetition of the subpattern does in fact
1663: match no characters, the loop is forcibly broken.
1664: </P>
1665: <P>
1666: By default, the quantifiers are "greedy", that is, they match as much as
1667: possible (up to the maximum number of permitted times), without causing the
1668: rest of the pattern to fail. The classic example of where this gives problems
1669: is in trying to match comments in C programs. These appear between /* and */
1670: and within the comment, individual * and / characters may appear. An attempt to
1671: match C comments by applying the pattern
1672: <pre>
1673: /\*.*\*/
1674: </pre>
1675: to the string
1676: <pre>
1677: /* first comment */ not comment /* second comment */
1678: </pre>
1679: fails, because it matches the entire string owing to the greediness of the .*
1680: item.
1681: </P>
1682: <P>
1683: However, if a quantifier is followed by a question mark, it ceases to be
1684: greedy, and instead matches the minimum number of times possible, so the
1685: pattern
1686: <pre>
1687: /\*.*?\*/
1688: </pre>
1689: does the right thing with the C comments. The meaning of the various
1690: quantifiers is not otherwise changed, just the preferred number of matches.
1691: Do not confuse this use of question mark with its use as a quantifier in its
1692: own right. Because it has two uses, it can sometimes appear doubled, as in
1693: <pre>
1694: \d??\d
1695: </pre>
1696: which matches one digit by preference, but can match two if that is the only
1697: way the rest of the pattern matches.
1698: </P>
1699: <P>
1700: If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
1701: the quantifiers are not greedy by default, but individual ones can be made
1702: greedy by following them with a question mark. In other words, it inverts the
1703: default behaviour.
1704: </P>
1705: <P>
1706: When a parenthesized subpattern is quantified with a minimum repeat count that
1707: is greater than 1 or with a limited maximum, more memory is required for the
1708: compiled pattern, in proportion to the size of the minimum or maximum.
1709: </P>
1710: <P>
1711: If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1712: to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
1713: implicitly anchored, because whatever follows will be tried against every
1714: character position in the subject string, so there is no point in retrying the
1715: overall match at any position after the first. PCRE normally treats such a
1716: pattern as though it were preceded by \A.
1717: </P>
1718: <P>
1719: In cases where it is known that the subject string contains no newlines, it is
1720: worth setting PCRE_DOTALL in order to obtain this optimization, or
1721: alternatively using ^ to indicate anchoring explicitly.
1722: </P>
1723: <P>
1.1.1.4 ! misho 1724: However, there are some cases where the optimization cannot be used. When .*
1.1 misho 1725: is inside capturing parentheses that are the subject of a back reference
1726: elsewhere in the pattern, a match at the start may fail where a later one
1727: succeeds. Consider, for example:
1728: <pre>
1729: (.*)abc\1
1730: </pre>
1731: If the subject is "xyz123abc123" the match point is the fourth character. For
1732: this reason, such a pattern is not implicitly anchored.
1733: </P>
1734: <P>
1.1.1.4 ! misho 1735: Another case where implicit anchoring is not applied is when the leading .* is
! 1736: inside an atomic group. Once again, a match at the start may fail where a later
! 1737: one succeeds. Consider this pattern:
! 1738: <pre>
! 1739: (?>.*?a)b
! 1740: </pre>
! 1741: It matches "ab" in the subject "aab". The use of the backtracking control verbs
! 1742: (*PRUNE) and (*SKIP) also disable this optimization.
! 1743: </P>
! 1744: <P>
1.1 misho 1745: When a capturing subpattern is repeated, the value captured is the substring
1746: that matched the final iteration. For example, after
1747: <pre>
1748: (tweedle[dume]{3}\s*)+
1749: </pre>
1750: has matched "tweedledum tweedledee" the value of the captured substring is
1751: "tweedledee". However, if there are nested capturing subpatterns, the
1752: corresponding captured values may have been set in previous iterations. For
1753: example, after
1754: <pre>
1755: /(a|(b))+/
1756: </pre>
1757: matches "aba" the value of the second captured substring is "b".
1758: <a name="atomicgroup"></a></P>
1.1.1.4 ! misho 1759: <br><a name="SEC17" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
1.1 misho 1760: <P>
1761: With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1762: repetition, failure of what follows normally causes the repeated item to be
1763: re-evaluated to see if a different number of repeats allows the rest of the
1764: pattern to match. Sometimes it is useful to prevent this, either to change the
1765: nature of the match, or to cause it fail earlier than it otherwise might, when
1766: the author of the pattern knows there is no point in carrying on.
1767: </P>
1768: <P>
1769: Consider, for example, the pattern \d+foo when applied to the subject line
1770: <pre>
1771: 123456bar
1772: </pre>
1773: After matching all 6 digits and then failing to match "foo", the normal
1774: action of the matcher is to try again with only 5 digits matching the \d+
1775: item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
1776: (a term taken from Jeffrey Friedl's book) provides the means for specifying
1777: that once a subpattern has matched, it is not to be re-evaluated in this way.
1778: </P>
1779: <P>
1780: If we use atomic grouping for the previous example, the matcher gives up
1781: immediately on failing to match "foo" the first time. The notation is a kind of
1782: special parenthesis, starting with (?> as in this example:
1783: <pre>
1784: (?>\d+)foo
1785: </pre>
1786: This kind of parenthesis "locks up" the part of the pattern it contains once
1787: it has matched, and a failure further into the pattern is prevented from
1788: backtracking into it. Backtracking past it to previous items, however, works as
1789: normal.
1790: </P>
1791: <P>
1792: An alternative description is that a subpattern of this type matches the string
1793: of characters that an identical standalone pattern would match, if anchored at
1794: the current point in the subject string.
1795: </P>
1796: <P>
1797: Atomic grouping subpatterns are not capturing subpatterns. Simple cases such as
1798: the above example can be thought of as a maximizing repeat that must swallow
1799: everything it can. So, while both \d+ and \d+? are prepared to adjust the
1800: number of digits they match in order to make the rest of the pattern match,
1801: (?>\d+) can only match an entire sequence of digits.
1802: </P>
1803: <P>
1804: Atomic groups in general can of course contain arbitrarily complicated
1805: subpatterns, and can be nested. However, when the subpattern for an atomic
1806: group is just a single repeated item, as in the example above, a simpler
1807: notation, called a "possessive quantifier" can be used. This consists of an
1808: additional + character following a quantifier. Using this notation, the
1809: previous example can be rewritten as
1810: <pre>
1811: \d++foo
1812: </pre>
1813: Note that a possessive quantifier can be used with an entire group, for
1814: example:
1815: <pre>
1816: (abc|xyz){2,3}+
1817: </pre>
1818: Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1819: option is ignored. They are a convenient notation for the simpler forms of
1820: atomic group. However, there is no difference in the meaning of a possessive
1821: quantifier and the equivalent atomic group, though there may be a performance
1822: difference; possessive quantifiers should be slightly faster.
1823: </P>
1824: <P>
1825: The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1826: Jeffrey Friedl originated the idea (and the name) in the first edition of his
1827: book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1828: package, and PCRE copied it from there. It ultimately found its way into Perl
1829: at release 5.10.
1830: </P>
1831: <P>
1832: PCRE has an optimization that automatically "possessifies" certain simple
1833: pattern constructs. For example, the sequence A+B is treated as A++B because
1834: there is no point in backtracking into a sequence of A's when B must follow.
1835: </P>
1836: <P>
1837: When a pattern contains an unlimited repeat inside a subpattern that can itself
1838: be repeated an unlimited number of times, the use of an atomic group is the
1839: only way to avoid some failing matches taking a very long time indeed. The
1840: pattern
1841: <pre>
1842: (\D+|<\d+>)*[!?]
1843: </pre>
1844: matches an unlimited number of substrings that either consist of non-digits, or
1845: digits enclosed in <>, followed by either ! or ?. When it matches, it runs
1846: quickly. However, if it is applied to
1847: <pre>
1848: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1849: </pre>
1850: it takes a long time before reporting failure. This is because the string can
1851: be divided between the internal \D+ repeat and the external * repeat in a
1852: large number of ways, and all have to be tried. (The example uses [!?] rather
1853: than a single character at the end, because both PCRE and Perl have an
1854: optimization that allows for fast failure when a single character is used. They
1855: remember the last single character that is required for a match, and fail early
1856: if it is not present in the string.) If the pattern is changed so that it uses
1857: an atomic group, like this:
1858: <pre>
1859: ((?>\D+)|<\d+>)*[!?]
1860: </pre>
1861: sequences of non-digits cannot be broken, and failure happens quickly.
1862: <a name="backreferences"></a></P>
1.1.1.4 ! misho 1863: <br><a name="SEC18" href="#TOC1">BACK REFERENCES</a><br>
1.1 misho 1864: <P>
1865: Outside a character class, a backslash followed by a digit greater than 0 (and
1866: possibly further digits) is a back reference to a capturing subpattern earlier
1867: (that is, to its left) in the pattern, provided there have been that many
1868: previous capturing left parentheses.
1869: </P>
1870: <P>
1871: However, if the decimal number following the backslash is less than 10, it is
1872: always taken as a back reference, and causes an error only if there are not
1873: that many capturing left parentheses in the entire pattern. In other words, the
1874: parentheses that are referenced need not be to the left of the reference for
1875: numbers less than 10. A "forward back reference" of this type can make sense
1876: when a repetition is involved and the subpattern to the right has participated
1877: in an earlier iteration.
1878: </P>
1879: <P>
1880: It is not possible to have a numerical "forward back reference" to a subpattern
1881: whose number is 10 or more using this syntax because a sequence such as \50 is
1882: interpreted as a character defined in octal. See the subsection entitled
1883: "Non-printing characters"
1884: <a href="#digitsafterbackslash">above</a>
1885: for further details of the handling of digits following a backslash. There is
1886: no such problem when named parentheses are used. A back reference to any
1887: subpattern is possible using named parentheses (see below).
1888: </P>
1889: <P>
1890: Another way of avoiding the ambiguity inherent in the use of digits following a
1891: backslash is to use the \g escape sequence. This escape must be followed by an
1892: unsigned number or a negative number, optionally enclosed in braces. These
1893: examples are all identical:
1894: <pre>
1895: (ring), \1
1896: (ring), \g1
1897: (ring), \g{1}
1898: </pre>
1899: An unsigned number specifies an absolute reference without the ambiguity that
1900: is present in the older syntax. It is also useful when literal digits follow
1901: the reference. A negative number is a relative reference. Consider this
1902: example:
1903: <pre>
1904: (abc(def)ghi)\g{-1}
1905: </pre>
1906: The sequence \g{-1} is a reference to the most recently started capturing
1907: subpattern before \g, that is, is it equivalent to \2 in this example.
1908: Similarly, \g{-2} would be equivalent to \1. The use of relative references
1909: can be helpful in long patterns, and also in patterns that are created by
1910: joining together fragments that contain references within themselves.
1911: </P>
1912: <P>
1913: A back reference matches whatever actually matched the capturing subpattern in
1914: the current subject string, rather than anything matching the subpattern
1915: itself (see
1916: <a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a>
1917: below for a way of doing that). So the pattern
1918: <pre>
1919: (sens|respons)e and \1ibility
1920: </pre>
1921: matches "sense and sensibility" and "response and responsibility", but not
1922: "sense and responsibility". If caseful matching is in force at the time of the
1923: back reference, the case of letters is relevant. For example,
1924: <pre>
1925: ((?i)rah)\s+\1
1926: </pre>
1927: matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1928: capturing subpattern is matched caselessly.
1929: </P>
1930: <P>
1931: There are several different ways of writing back references to named
1932: subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or
1933: \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified
1934: back reference syntax, in which \g can be used for both numeric and named
1935: references, is also supported. We could rewrite the above example in any of
1936: the following ways:
1937: <pre>
1938: (?<p1>(?i)rah)\s+\k<p1>
1939: (?'p1'(?i)rah)\s+\k{p1}
1940: (?P<p1>(?i)rah)\s+(?P=p1)
1941: (?<p1>(?i)rah)\s+\g{p1}
1942: </pre>
1943: A subpattern that is referenced by name may appear in the pattern before or
1944: after the reference.
1945: </P>
1946: <P>
1947: There may be more than one back reference to the same subpattern. If a
1948: subpattern has not actually been used in a particular match, any back
1949: references to it always fail by default. For example, the pattern
1950: <pre>
1951: (a|(bc))\2
1952: </pre>
1953: always fails if it starts to match "a" rather than "bc". However, if the
1954: PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an
1955: unset value matches an empty string.
1956: </P>
1957: <P>
1958: Because there may be many capturing parentheses in a pattern, all digits
1959: following a backslash are taken as part of a potential back reference number.
1960: If the pattern continues with a digit character, some delimiter must be used to
1961: terminate the back reference. If the PCRE_EXTENDED option is set, this can be
1.1.1.3 misho 1962: white space. Otherwise, the \g{ syntax or an empty comment (see
1.1 misho 1963: <a href="#comments">"Comments"</a>
1964: below) can be used.
1965: </P>
1966: <br><b>
1967: Recursive back references
1968: </b><br>
1969: <P>
1970: A back reference that occurs inside the parentheses to which it refers fails
1971: when the subpattern is first used, so, for example, (a\1) never matches.
1972: However, such references can be useful inside repeated subpatterns. For
1973: example, the pattern
1974: <pre>
1975: (a|b\1)+
1976: </pre>
1977: matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
1978: the subpattern, the back reference matches the character string corresponding
1979: to the previous iteration. In order for this to work, the pattern must be such
1980: that the first iteration does not need to match the back reference. This can be
1981: done using alternation, as in the example above, or by a quantifier with a
1982: minimum of zero.
1983: </P>
1984: <P>
1985: Back references of this type cause the group that they reference to be treated
1986: as an
1987: <a href="#atomicgroup">atomic group.</a>
1988: Once the whole group has been matched, a subsequent matching failure cannot
1989: cause backtracking into the middle of the group.
1990: <a name="bigassertions"></a></P>
1.1.1.4 ! misho 1991: <br><a name="SEC19" href="#TOC1">ASSERTIONS</a><br>
1.1 misho 1992: <P>
1993: An assertion is a test on the characters following or preceding the current
1994: matching point that does not actually consume any characters. The simple
1995: assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described
1996: <a href="#smallassertions">above.</a>
1997: </P>
1998: <P>
1999: More complicated assertions are coded as subpatterns. There are two kinds:
2000: those that look ahead of the current position in the subject string, and those
2001: that look behind it. An assertion subpattern is matched in the normal way,
2002: except that it does not cause the current matching position to be changed.
2003: </P>
2004: <P>
2005: Assertion subpatterns are not capturing subpatterns. If such an assertion
2006: contains capturing subpatterns within it, these are counted for the purposes of
2007: numbering the capturing subpatterns in the whole pattern. However, substring
1.1.1.4 ! misho 2008: capturing is carried out only for positive assertions. (Perl sometimes, but not
! 2009: always, does do capturing in negative assertions.)
1.1 misho 2010: </P>
2011: <P>
2012: For compatibility with Perl, assertion subpatterns may be repeated; though
2013: it makes no sense to assert the same thing several times, the side effect of
2014: capturing parentheses may occasionally be useful. In practice, there only three
2015: cases:
2016: <br>
2017: <br>
2018: (1) If the quantifier is {0}, the assertion is never obeyed during matching.
2019: However, it may contain internal capturing parenthesized groups that are called
2020: from elsewhere via the
2021: <a href="#subpatternsassubroutines">subroutine mechanism.</a>
2022: <br>
2023: <br>
2024: (2) If quantifier is {0,n} where n is greater than zero, it is treated as if it
2025: were {0,1}. At run time, the rest of the pattern match is tried with and
2026: without the assertion, the order depending on the greediness of the quantifier.
2027: <br>
2028: <br>
2029: (3) If the minimum repetition is greater than zero, the quantifier is ignored.
2030: The assertion is obeyed just once when encountered during matching.
2031: </P>
2032: <br><b>
2033: Lookahead assertions
2034: </b><br>
2035: <P>
2036: Lookahead assertions start with (?= for positive assertions and (?! for
2037: negative assertions. For example,
2038: <pre>
2039: \w+(?=;)
2040: </pre>
2041: matches a word followed by a semicolon, but does not include the semicolon in
2042: the match, and
2043: <pre>
2044: foo(?!bar)
2045: </pre>
2046: matches any occurrence of "foo" that is not followed by "bar". Note that the
2047: apparently similar pattern
2048: <pre>
2049: (?!foo)bar
2050: </pre>
2051: does not find an occurrence of "bar" that is preceded by something other than
2052: "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
2053: (?!foo) is always true when the next three characters are "bar". A
2054: lookbehind assertion is needed to achieve the other effect.
2055: </P>
2056: <P>
2057: If you want to force a matching failure at some point in a pattern, the most
2058: convenient way to do it is with (?!) because an empty string always matches, so
2059: an assertion that requires there not to be an empty string must always fail.
2060: The backtracking control verb (*FAIL) or (*F) is a synonym for (?!).
2061: <a name="lookbehind"></a></P>
2062: <br><b>
2063: Lookbehind assertions
2064: </b><br>
2065: <P>
2066: Lookbehind assertions start with (?<= for positive assertions and (?<! for
2067: negative assertions. For example,
2068: <pre>
2069: (?<!foo)bar
2070: </pre>
2071: does find an occurrence of "bar" that is not preceded by "foo". The contents of
2072: a lookbehind assertion are restricted such that all the strings it matches must
2073: have a fixed length. However, if there are several top-level alternatives, they
2074: do not all have to have the same fixed length. Thus
2075: <pre>
2076: (?<=bullock|donkey)
2077: </pre>
2078: is permitted, but
2079: <pre>
2080: (?<!dogs?|cats?)
2081: </pre>
2082: causes an error at compile time. Branches that match different length strings
2083: are permitted only at the top level of a lookbehind assertion. This is an
2084: extension compared with Perl, which requires all branches to match the same
2085: length of string. An assertion such as
2086: <pre>
2087: (?<=ab(c|de))
2088: </pre>
2089: is not permitted, because its single top-level branch can match two different
2090: lengths, but it is acceptable to PCRE if rewritten to use two top-level
2091: branches:
2092: <pre>
2093: (?<=abc|abde)
2094: </pre>
2095: In some cases, the escape sequence \K
2096: <a href="#resetmatchstart">(see above)</a>
2097: can be used instead of a lookbehind assertion to get round the fixed-length
2098: restriction.
2099: </P>
2100: <P>
2101: The implementation of lookbehind assertions is, for each alternative, to
2102: temporarily move the current position back by the fixed length and then try to
2103: match. If there are insufficient characters before the current position, the
2104: assertion fails.
2105: </P>
2106: <P>
1.1.1.2 misho 2107: In a UTF mode, PCRE does not allow the \C escape (which matches a single data
2108: unit even in a UTF mode) to appear in lookbehind assertions, because it makes
2109: it impossible to calculate the length of the lookbehind. The \X and \R
2110: escapes, which can match different numbers of data units, are also not
2111: permitted.
1.1 misho 2112: </P>
2113: <P>
2114: <a href="#subpatternsassubroutines">"Subroutine"</a>
2115: calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
2116: as the subpattern matches a fixed-length string.
2117: <a href="#recursion">Recursion,</a>
2118: however, is not supported.
2119: </P>
2120: <P>
2121: Possessive quantifiers can be used in conjunction with lookbehind assertions to
2122: specify efficient matching of fixed-length strings at the end of subject
2123: strings. Consider a simple pattern such as
2124: <pre>
2125: abcd$
2126: </pre>
2127: when applied to a long string that does not match. Because matching proceeds
2128: from left to right, PCRE will look for each "a" in the subject and then see if
2129: what follows matches the rest of the pattern. If the pattern is specified as
2130: <pre>
2131: ^.*abcd$
2132: </pre>
2133: the initial .* matches the entire string at first, but when this fails (because
2134: there is no following "a"), it backtracks to match all but the last character,
2135: then all but the last two characters, and so on. Once again the search for "a"
2136: covers the entire string, from right to left, so we are no better off. However,
2137: if the pattern is written as
2138: <pre>
2139: ^.*+(?<=abcd)
2140: </pre>
2141: there can be no backtracking for the .*+ item; it can match only the entire
2142: string. The subsequent lookbehind assertion does a single test on the last four
2143: characters. If it fails, the match fails immediately. For long strings, this
2144: approach makes a significant difference to the processing time.
2145: </P>
2146: <br><b>
2147: Using multiple assertions
2148: </b><br>
2149: <P>
2150: Several assertions (of any sort) may occur in succession. For example,
2151: <pre>
2152: (?<=\d{3})(?<!999)foo
2153: </pre>
2154: matches "foo" preceded by three digits that are not "999". Notice that each of
2155: the assertions is applied independently at the same point in the subject
2156: string. First there is a check that the previous three characters are all
2157: digits, and then there is a check that the same three characters are not "999".
2158: This pattern does <i>not</i> match "foo" preceded by six characters, the first
2159: of which are digits and the last three of which are not "999". For example, it
2160: doesn't match "123abcfoo". A pattern to do that is
2161: <pre>
2162: (?<=\d{3}...)(?<!999)foo
2163: </pre>
2164: This time the first assertion looks at the preceding six characters, checking
2165: that the first three are digits, and then the second assertion checks that the
2166: preceding three characters are not "999".
2167: </P>
2168: <P>
2169: Assertions can be nested in any combination. For example,
2170: <pre>
2171: (?<=(?<!foo)bar)baz
2172: </pre>
2173: matches an occurrence of "baz" that is preceded by "bar" which in turn is not
2174: preceded by "foo", while
2175: <pre>
2176: (?<=\d{3}(?!999)...)foo
2177: </pre>
2178: is another pattern that matches "foo" preceded by three digits and any three
2179: characters that are not "999".
2180: <a name="conditions"></a></P>
1.1.1.4 ! misho 2181: <br><a name="SEC20" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1.1 misho 2182: <P>
2183: It is possible to cause the matching process to obey a subpattern
2184: conditionally or to choose between two alternative subpatterns, depending on
2185: the result of an assertion, or whether a specific capturing subpattern has
2186: already been matched. The two possible forms of conditional subpattern are:
2187: <pre>
2188: (?(condition)yes-pattern)
2189: (?(condition)yes-pattern|no-pattern)
2190: </pre>
2191: If the condition is satisfied, the yes-pattern is used; otherwise the
2192: no-pattern (if present) is used. If there are more than two alternatives in the
2193: subpattern, a compile-time error occurs. Each of the two alternatives may
2194: itself contain nested subpatterns of any form, including conditional
2195: subpatterns; the restriction to two alternatives applies only at the level of
2196: the condition. This pattern fragment is an example where the alternatives are
2197: complex:
2198: <pre>
2199: (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
2200:
2201: </PRE>
2202: </P>
2203: <P>
2204: There are four kinds of condition: references to subpatterns, references to
2205: recursion, a pseudo-condition called DEFINE, and assertions.
2206: </P>
2207: <br><b>
2208: Checking for a used subpattern by number
2209: </b><br>
2210: <P>
2211: If the text between the parentheses consists of a sequence of digits, the
2212: condition is true if a capturing subpattern of that number has previously
2213: matched. If there is more than one capturing subpattern with the same number
2214: (see the earlier
2215: <a href="#recursion">section about duplicate subpattern numbers),</a>
2216: the condition is true if any of them have matched. An alternative notation is
2217: to precede the digits with a plus or minus sign. In this case, the subpattern
2218: number is relative rather than absolute. The most recently opened parentheses
2219: can be referenced by (?(-1), the next most recent by (?(-2), and so on. Inside
2220: loops it can also make sense to refer to subsequent groups. The next
2221: parentheses to be opened can be referenced as (?(+1), and so on. (The value
2222: zero in any of these forms is not used; it provokes a compile-time error.)
2223: </P>
2224: <P>
2225: Consider the following pattern, which contains non-significant white space to
2226: make it more readable (assume the PCRE_EXTENDED option) and to divide it into
2227: three parts for ease of discussion:
2228: <pre>
2229: ( \( )? [^()]+ (?(1) \) )
2230: </pre>
2231: The first part matches an optional opening parenthesis, and if that
2232: character is present, sets it as the first captured substring. The second part
2233: matches one or more characters that are not parentheses. The third part is a
2234: conditional subpattern that tests whether or not the first set of parentheses
2235: matched. If they did, that is, if subject started with an opening parenthesis,
2236: the condition is true, and so the yes-pattern is executed and a closing
2237: parenthesis is required. Otherwise, since no-pattern is not present, the
2238: subpattern matches nothing. In other words, this pattern matches a sequence of
2239: non-parentheses, optionally enclosed in parentheses.
2240: </P>
2241: <P>
2242: If you were embedding this pattern in a larger one, you could use a relative
2243: reference:
2244: <pre>
2245: ...other stuff... ( \( )? [^()]+ (?(-1) \) ) ...
2246: </pre>
2247: This makes the fragment independent of the parentheses in the larger pattern.
2248: </P>
2249: <br><b>
2250: Checking for a used subpattern by name
2251: </b><br>
2252: <P>
2253: Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used
2254: subpattern by name. For compatibility with earlier versions of PCRE, which had
2255: this facility before Perl, the syntax (?(name)...) is also recognized. However,
2256: there is a possible ambiguity with this syntax, because subpattern names may
2257: consist entirely of digits. PCRE looks first for a named subpattern; if it
2258: cannot find one and the name consists entirely of digits, PCRE looks for a
2259: subpattern of that number, which must be greater than zero. Using subpattern
2260: names that consist entirely of digits is not recommended.
2261: </P>
2262: <P>
2263: Rewriting the above example to use a named subpattern gives this:
2264: <pre>
2265: (?<OPEN> \( )? [^()]+ (?(<OPEN>) \) )
2266: </pre>
2267: If the name used in a condition of this kind is a duplicate, the test is
2268: applied to all subpatterns of the same name, and is true if any one of them has
2269: matched.
2270: </P>
2271: <br><b>
2272: Checking for pattern recursion
2273: </b><br>
2274: <P>
2275: If the condition is the string (R), and there is no subpattern with the name R,
2276: the condition is true if a recursive call to the whole pattern or any
2277: subpattern has been made. If digits or a name preceded by ampersand follow the
2278: letter R, for example:
2279: <pre>
2280: (?(R3)...) or (?(R&name)...)
2281: </pre>
2282: the condition is true if the most recent recursion is into a subpattern whose
2283: number or name is given. This condition does not check the entire recursion
2284: stack. If the name used in a condition of this kind is a duplicate, the test is
2285: applied to all subpatterns of the same name, and is true if any one of them is
2286: the most recent recursion.
2287: </P>
2288: <P>
2289: At "top level", all these recursion test conditions are false.
2290: <a href="#recursion">The syntax for recursive patterns</a>
2291: is described below.
2292: <a name="subdefine"></a></P>
2293: <br><b>
2294: Defining subpatterns for use by reference only
2295: </b><br>
2296: <P>
2297: If the condition is the string (DEFINE), and there is no subpattern with the
2298: name DEFINE, the condition is always false. In this case, there may be only one
2299: alternative in the subpattern. It is always skipped if control reaches this
2300: point in the pattern; the idea of DEFINE is that it can be used to define
2301: subroutines that can be referenced from elsewhere. (The use of
2302: <a href="#subpatternsassubroutines">subroutines</a>
2303: is described below.) For example, a pattern to match an IPv4 address such as
1.1.1.3 misho 2304: "192.168.23.245" could be written like this (ignore white space and line
1.1 misho 2305: breaks):
2306: <pre>
2307: (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
2308: \b (?&byte) (\.(?&byte)){3} \b
2309: </pre>
2310: The first part of the pattern is a DEFINE group inside which a another group
2311: named "byte" is defined. This matches an individual component of an IPv4
2312: address (a number less than 256). When matching takes place, this part of the
2313: pattern is skipped because DEFINE acts like a false condition. The rest of the
2314: pattern uses references to the named group to match the four dot-separated
2315: components of an IPv4 address, insisting on a word boundary at each end.
2316: </P>
2317: <br><b>
2318: Assertion conditions
2319: </b><br>
2320: <P>
2321: If the condition is not in any of the above formats, it must be an assertion.
2322: This may be a positive or negative lookahead or lookbehind assertion. Consider
2323: this pattern, again containing non-significant white space, and with the two
2324: alternatives on the second line:
2325: <pre>
2326: (?(?=[^a-z]*[a-z])
2327: \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
2328: </pre>
2329: The condition is a positive lookahead assertion that matches an optional
2330: sequence of non-letters followed by a letter. In other words, it tests for the
2331: presence of at least one letter in the subject. If a letter is found, the
2332: subject is matched against the first alternative; otherwise it is matched
2333: against the second. This pattern matches strings in one of the two forms
2334: dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
2335: <a name="comments"></a></P>
1.1.1.4 ! misho 2336: <br><a name="SEC21" href="#TOC1">COMMENTS</a><br>
1.1 misho 2337: <P>
2338: There are two ways of including comments in patterns that are processed by
2339: PCRE. In both cases, the start of the comment must not be in a character class,
2340: nor in the middle of any other sequence of related characters such as (?: or a
2341: subpattern name or number. The characters that make up a comment play no part
2342: in the pattern matching.
2343: </P>
2344: <P>
2345: The sequence (?# marks the start of a comment that continues up to the next
2346: closing parenthesis. Nested parentheses are not permitted. If the PCRE_EXTENDED
2347: option is set, an unescaped # character also introduces a comment, which in
2348: this case continues to immediately after the next newline character or
2349: character sequence in the pattern. Which characters are interpreted as newlines
1.1.1.2 misho 2350: is controlled by the options passed to a compiling function or by a special
1.1 misho 2351: sequence at the start of the pattern, as described in the section entitled
2352: <a href="#newlines">"Newline conventions"</a>
2353: above. Note that the end of this type of comment is a literal newline sequence
2354: in the pattern; escape sequences that happen to represent a newline do not
2355: count. For example, consider this pattern when PCRE_EXTENDED is set, and the
2356: default newline convention is in force:
2357: <pre>
2358: abc #comment \n still comment
2359: </pre>
2360: On encountering the # character, <b>pcre_compile()</b> skips along, looking for
2361: a newline in the pattern. The sequence \n is still literal at this stage, so
2362: it does not terminate the comment. Only an actual character with the code value
2363: 0x0a (the default newline) does so.
2364: <a name="recursion"></a></P>
1.1.1.4 ! misho 2365: <br><a name="SEC22" href="#TOC1">RECURSIVE PATTERNS</a><br>
1.1 misho 2366: <P>
2367: Consider the problem of matching a string in parentheses, allowing for
2368: unlimited nested parentheses. Without the use of recursion, the best that can
2369: be done is to use a pattern that matches up to some fixed depth of nesting. It
2370: is not possible to handle an arbitrary nesting depth.
2371: </P>
2372: <P>
2373: For some time, Perl has provided a facility that allows regular expressions to
2374: recurse (amongst other things). It does this by interpolating Perl code in the
2375: expression at run time, and the code can refer to the expression itself. A Perl
2376: pattern using code interpolation to solve the parentheses problem can be
2377: created like this:
2378: <pre>
2379: $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
2380: </pre>
2381: The (?p{...}) item interpolates Perl code at run time, and in this case refers
2382: recursively to the pattern in which it appears.
2383: </P>
2384: <P>
2385: Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
2386: supports special syntax for recursion of the entire pattern, and also for
2387: individual subpattern recursion. After its introduction in PCRE and Python,
2388: this kind of recursion was subsequently introduced into Perl at release 5.10.
2389: </P>
2390: <P>
2391: A special item that consists of (? followed by a number greater than zero and a
2392: closing parenthesis is a recursive subroutine call of the subpattern of the
2393: given number, provided that it occurs inside that subpattern. (If not, it is a
2394: <a href="#subpatternsassubroutines">non-recursive subroutine</a>
2395: call, which is described in the next section.) The special item (?R) or (?0) is
2396: a recursive call of the entire regular expression.
2397: </P>
2398: <P>
2399: This PCRE pattern solves the nested parentheses problem (assume the
2400: PCRE_EXTENDED option is set so that white space is ignored):
2401: <pre>
2402: \( ( [^()]++ | (?R) )* \)
2403: </pre>
2404: First it matches an opening parenthesis. Then it matches any number of
2405: substrings which can either be a sequence of non-parentheses, or a recursive
2406: match of the pattern itself (that is, a correctly parenthesized substring).
2407: Finally there is a closing parenthesis. Note the use of a possessive quantifier
2408: to avoid backtracking into sequences of non-parentheses.
2409: </P>
2410: <P>
2411: If this were part of a larger pattern, you would not want to recurse the entire
2412: pattern, so instead you could use this:
2413: <pre>
2414: ( \( ( [^()]++ | (?1) )* \) )
2415: </pre>
2416: We have put the pattern into parentheses, and caused the recursion to refer to
2417: them instead of the whole pattern.
2418: </P>
2419: <P>
2420: In a larger pattern, keeping track of parenthesis numbers can be tricky. This
2421: is made easier by the use of relative references. Instead of (?1) in the
2422: pattern above you can write (?-2) to refer to the second most recently opened
2423: parentheses preceding the recursion. In other words, a negative number counts
2424: capturing parentheses leftwards from the point at which it is encountered.
2425: </P>
2426: <P>
2427: It is also possible to refer to subsequently opened parentheses, by writing
2428: references such as (?+2). However, these cannot be recursive because the
2429: reference is not inside the parentheses that are referenced. They are always
2430: <a href="#subpatternsassubroutines">non-recursive subroutine</a>
2431: calls, as described in the next section.
2432: </P>
2433: <P>
2434: An alternative approach is to use named parentheses instead. The Perl syntax
2435: for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We
2436: could rewrite the above example as follows:
2437: <pre>
2438: (?<pn> \( ( [^()]++ | (?&pn) )* \) )
2439: </pre>
2440: If there is more than one subpattern with the same name, the earliest one is
2441: used.
2442: </P>
2443: <P>
2444: This particular example pattern that we have been looking at contains nested
2445: unlimited repeats, and so the use of a possessive quantifier for matching
2446: strings of non-parentheses is important when applying the pattern to strings
2447: that do not match. For example, when this pattern is applied to
2448: <pre>
2449: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
2450: </pre>
2451: it yields "no match" quickly. However, if a possessive quantifier is not used,
2452: the match runs for a very long time indeed because there are so many different
2453: ways the + and * repeats can carve up the subject, and all have to be tested
2454: before failure can be reported.
2455: </P>
2456: <P>
2457: At the end of a match, the values of capturing parentheses are those from
2458: the outermost level. If you want to obtain intermediate values, a callout
2459: function can be used (see below and the
2460: <a href="pcrecallout.html"><b>pcrecallout</b></a>
2461: documentation). If the pattern above is matched against
2462: <pre>
2463: (ab(cd)ef)
2464: </pre>
2465: the value for the inner capturing parentheses (numbered 2) is "ef", which is
2466: the last value taken on at the top level. If a capturing subpattern is not
2467: matched at the top level, its final captured value is unset, even if it was
2468: (temporarily) set at a deeper level during the matching process.
2469: </P>
2470: <P>
2471: If there are more than 15 capturing parentheses in a pattern, PCRE has to
2472: obtain extra memory to store data during a recursion, which it does by using
2473: <b>pcre_malloc</b>, freeing it via <b>pcre_free</b> afterwards. If no memory can
2474: be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
2475: </P>
2476: <P>
2477: Do not confuse the (?R) item with the condition (R), which tests for recursion.
2478: Consider this pattern, which matches text in angle brackets, allowing for
2479: arbitrary nesting. Only digits are allowed in nested brackets (that is, when
2480: recursing), whereas any characters are permitted at the outer level.
2481: <pre>
2482: < (?: (?(R) \d++ | [^<>]*+) | (?R)) * >
2483: </pre>
2484: In this pattern, (?(R) is the start of a conditional subpattern, with two
2485: different alternatives for the recursive and non-recursive cases. The (?R) item
2486: is the actual recursive call.
2487: <a name="recursiondifference"></a></P>
2488: <br><b>
2489: Differences in recursion processing between PCRE and Perl
2490: </b><br>
2491: <P>
2492: Recursion processing in PCRE differs from Perl in two important ways. In PCRE
2493: (like Python, but unlike Perl), a recursive subpattern call is always treated
2494: as an atomic group. That is, once it has matched some of the subject string, it
2495: is never re-entered, even if it contains untried alternatives and there is a
2496: subsequent matching failure. This can be illustrated by the following pattern,
2497: which purports to match a palindromic string that contains an odd number of
2498: characters (for example, "a", "aba", "abcba", "abcdcba"):
2499: <pre>
2500: ^(.|(.)(?1)\2)$
2501: </pre>
2502: The idea is that it either matches a single character, or two identical
2503: characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
2504: it does not if the pattern is longer than three characters. Consider the
2505: subject string "abcba":
2506: </P>
2507: <P>
2508: At the top level, the first character is matched, but as it is not at the end
2509: of the string, the first alternative fails; the second alternative is taken
2510: and the recursion kicks in. The recursive call to subpattern 1 successfully
2511: matches the next character ("b"). (Note that the beginning and end of line
2512: tests are not part of the recursion).
2513: </P>
2514: <P>
2515: Back at the top level, the next character ("c") is compared with what
2516: subpattern 2 matched, which was "a". This fails. Because the recursion is
2517: treated as an atomic group, there are now no backtracking points, and so the
2518: entire match fails. (Perl is able, at this point, to re-enter the recursion and
2519: try the second alternative.) However, if the pattern is written with the
2520: alternatives in the other order, things are different:
2521: <pre>
2522: ^((.)(?1)\2|.)$
2523: </pre>
2524: This time, the recursing alternative is tried first, and continues to recurse
2525: until it runs out of characters, at which point the recursion fails. But this
2526: time we do have another alternative to try at the higher level. That is the big
2527: difference: in the previous case the remaining alternative is at a deeper
2528: recursion level, which PCRE cannot use.
2529: </P>
2530: <P>
2531: To change the pattern so that it matches all palindromic strings, not just
2532: those with an odd number of characters, it is tempting to change the pattern to
2533: this:
2534: <pre>
2535: ^((.)(?1)\2|.?)$
2536: </pre>
2537: Again, this works in Perl, but not in PCRE, and for the same reason. When a
2538: deeper recursion has matched a single character, it cannot be entered again in
2539: order to match an empty string. The solution is to separate the two cases, and
2540: write out the odd and even cases as alternatives at the higher level:
2541: <pre>
2542: ^(?:((.)(?1)\2|)|((.)(?3)\4|.))
2543: </pre>
2544: If you want to match typical palindromic phrases, the pattern has to ignore all
2545: non-word characters, which can be done like this:
2546: <pre>
2547: ^\W*+(?:((.)\W*+(?1)\W*+\2|)|((.)\W*+(?3)\W*+\4|\W*+.\W*+))\W*+$
2548: </pre>
2549: If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
2550: man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
2551: the use of the possessive quantifier *+ to avoid backtracking into sequences of
2552: non-word characters. Without this, PCRE takes a great deal longer (ten times or
2553: more) to match typical phrases, and Perl takes so long that you think it has
2554: gone into a loop.
2555: </P>
2556: <P>
2557: <b>WARNING</b>: The palindrome-matching patterns above work only if the subject
2558: string does not start with a palindrome that is shorter than the entire string.
2559: For example, although "abcba" is correctly matched, if the subject is "ababa",
2560: PCRE finds the palindrome "aba" at the start, then fails at top level because
2561: the end of the string does not follow. Once again, it cannot jump back into the
2562: recursion to try other alternatives, so the entire match fails.
2563: </P>
2564: <P>
2565: The second way in which PCRE and Perl differ in their recursion processing is
2566: in the handling of captured values. In Perl, when a subpattern is called
2567: recursively or as a subpattern (see the next section), it has no access to any
2568: values that were captured outside the recursion, whereas in PCRE these values
2569: can be referenced. Consider this pattern:
2570: <pre>
2571: ^(.)(\1|a(?2))
2572: </pre>
2573: In PCRE, this pattern matches "bab". The first capturing parentheses match "b",
2574: then in the second group, when the back reference \1 fails to match "b", the
2575: second alternative matches "a" and then recurses. In the recursion, \1 does
2576: now match "b" and so the whole match succeeds. In Perl, the pattern fails to
2577: match because inside the recursive call \1 cannot access the externally set
2578: value.
2579: <a name="subpatternsassubroutines"></a></P>
1.1.1.4 ! misho 2580: <br><a name="SEC23" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
1.1 misho 2581: <P>
2582: If the syntax for a recursive subpattern call (either by number or by
2583: name) is used outside the parentheses to which it refers, it operates like a
2584: subroutine in a programming language. The called subpattern may be defined
2585: before or after the reference. A numbered reference can be absolute or
2586: relative, as in these examples:
2587: <pre>
2588: (...(absolute)...)...(?2)...
2589: (...(relative)...)...(?-1)...
2590: (...(?+1)...(relative)...
2591: </pre>
2592: An earlier example pointed out that the pattern
2593: <pre>
2594: (sens|respons)e and \1ibility
2595: </pre>
2596: matches "sense and sensibility" and "response and responsibility", but not
2597: "sense and responsibility". If instead the pattern
2598: <pre>
2599: (sens|respons)e and (?1)ibility
2600: </pre>
2601: is used, it does match "sense and responsibility" as well as the other two
2602: strings. Another example is given in the discussion of DEFINE above.
2603: </P>
2604: <P>
2605: All subroutine calls, whether recursive or not, are always treated as atomic
2606: groups. That is, once a subroutine has matched some of the subject string, it
2607: is never re-entered, even if it contains untried alternatives and there is a
2608: subsequent matching failure. Any capturing parentheses that are set during the
2609: subroutine call revert to their previous values afterwards.
2610: </P>
2611: <P>
2612: Processing options such as case-independence are fixed when a subpattern is
2613: defined, so if it is used as a subroutine, such options cannot be changed for
2614: different calls. For example, consider this pattern:
2615: <pre>
2616: (abc)(?i:(?-1))
2617: </pre>
2618: It matches "abcabc". It does not match "abcABC" because the change of
2619: processing option does not affect the called subpattern.
2620: <a name="onigurumasubroutines"></a></P>
1.1.1.4 ! misho 2621: <br><a name="SEC24" href="#TOC1">ONIGURUMA SUBROUTINE SYNTAX</a><br>
1.1 misho 2622: <P>
2623: For compatibility with Oniguruma, the non-Perl syntax \g followed by a name or
2624: a number enclosed either in angle brackets or single quotes, is an alternative
2625: syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2626: are two of the examples used above, rewritten using this syntax:
2627: <pre>
2628: (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
2629: (sens|respons)e and \g'1'ibility
2630: </pre>
2631: PCRE supports an extension to Oniguruma: if a number is preceded by a
2632: plus or a minus sign it is taken as a relative reference. For example:
2633: <pre>
2634: (abc)(?i:\g<-1>)
2635: </pre>
2636: Note that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are <i>not</i>
2637: synonymous. The former is a back reference; the latter is a subroutine call.
2638: </P>
1.1.1.4 ! misho 2639: <br><a name="SEC25" href="#TOC1">CALLOUTS</a><br>
1.1 misho 2640: <P>
2641: Perl has a feature whereby using the sequence (?{...}) causes arbitrary Perl
2642: code to be obeyed in the middle of matching a regular expression. This makes it
2643: possible, amongst other things, to extract different substrings that match the
2644: same pair of parentheses when there is a repetition.
2645: </P>
2646: <P>
2647: PCRE provides a similar feature, but of course it cannot obey arbitrary Perl
2648: code. The feature is called "callout". The caller of PCRE provides an external
1.1.1.2 misho 2649: function by putting its entry point in the global variable <i>pcre_callout</i>
1.1.1.4 ! misho 2650: (8-bit library) or <i>pcre[16|32]_callout</i> (16-bit or 32-bit library).
! 2651: By default, this variable contains NULL, which disables all calling out.
1.1 misho 2652: </P>
2653: <P>
2654: Within a regular expression, (?C) indicates the points at which the external
2655: function is to be called. If you want to identify different callout points, you
2656: can put a number less than 256 after the letter C. The default value is zero.
2657: For example, this pattern has two callout points:
2658: <pre>
2659: (?C1)abc(?C2)def
2660: </pre>
1.1.1.2 misho 2661: If the PCRE_AUTO_CALLOUT flag is passed to a compiling function, callouts are
1.1 misho 2662: automatically installed before each item in the pattern. They are all numbered
1.1.1.4 ! misho 2663: 255. If there is a conditional group in the pattern whose condition is an
! 2664: assertion, an additional callout is inserted just before the condition. An
! 2665: explicit callout may also be set at this position, as in this example:
! 2666: <pre>
! 2667: (?(?C9)(?=a)abc|def)
! 2668: </pre>
! 2669: Note that this applies only to assertion conditions, not to other types of
! 2670: condition.
1.1 misho 2671: </P>
2672: <P>
1.1.1.2 misho 2673: During matching, when PCRE reaches a callout point, the external function is
2674: called. It is provided with the number of the callout, the position in the
2675: pattern, and, optionally, one item of data originally supplied by the caller of
2676: the matching function. The callout function may cause matching to proceed, to
2677: backtrack, or to fail altogether. A complete description of the interface to
2678: the callout function is given in the
1.1 misho 2679: <a href="pcrecallout.html"><b>pcrecallout</b></a>
2680: documentation.
2681: <a name="backtrackcontrol"></a></P>
1.1.1.4 ! misho 2682: <br><a name="SEC26" href="#TOC1">BACKTRACKING CONTROL</a><br>
1.1 misho 2683: <P>
2684: Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
1.1.1.4 ! misho 2685: are still described in the Perl documentation as "experimental and subject to
! 2686: change or removal in a future version of Perl". It goes on to say: "Their usage
! 2687: in production code should be noted to avoid problems during upgrades." The same
1.1 misho 2688: remarks apply to the PCRE features described in this section.
2689: </P>
2690: <P>
1.1.1.4 ! misho 2691: The new verbs make use of what was previously invalid syntax: an opening
! 2692: parenthesis followed by an asterisk. They are generally of the form
! 2693: (*VERB) or (*VERB:NAME). Some may take either form, possibly behaving
! 2694: differently depending on whether or not a name is present. A name is any
! 2695: sequence of characters that does not include a closing parenthesis. The maximum
! 2696: length of name is 255 in the 8-bit library and 65535 in the 16-bit and 32-bit
! 2697: libraries. If the name is empty, that is, if the closing parenthesis
! 2698: immediately follows the colon, the effect is as if the colon were not there.
! 2699: Any number of these verbs may occur in a pattern.
1.1 misho 2700: </P>
2701: <P>
1.1.1.4 ! misho 2702: Since these verbs are specifically related to backtracking, most of them can be
! 2703: used only when the pattern is to be matched using one of the traditional
! 2704: matching functions, because these use a backtracking algorithm. With the
! 2705: exception of (*FAIL), which behaves like a failing negative assertion, the
! 2706: backtracking control verbs cause an error if encountered by a DFA matching
! 2707: function.
1.1 misho 2708: </P>
2709: <P>
1.1.1.4 ! misho 2710: The behaviour of these verbs in
! 2711: <a href="#btrepeat">repeated groups,</a>
! 2712: <a href="#btassert">assertions,</a>
! 2713: and in
! 2714: <a href="#btsub">subpatterns called as subroutines</a>
! 2715: (whether or not recursively) is documented below.
1.1.1.3 misho 2716: <a name="nooptimize"></a></P>
2717: <br><b>
2718: Optimizations that affect backtracking verbs
2719: </b><br>
1.1 misho 2720: <P>
2721: PCRE contains some optimizations that are used to speed up matching by running
2722: some checks at the start of each match attempt. For example, it may know the
2723: minimum length of matching subject, or that a particular character must be
1.1.1.4 ! misho 2724: present. When one of these optimizations bypasses the running of a match, any
1.1 misho 2725: included backtracking verbs will not, of course, be processed. You can suppress
2726: the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2727: when calling <b>pcre_compile()</b> or <b>pcre_exec()</b>, or by starting the
1.1.1.3 misho 2728: pattern with (*NO_START_OPT). There is more discussion of this option in the
2729: section entitled
2730: <a href="pcreapi.html#execoptions">"Option bits for <b>pcre_exec()</b>"</a>
2731: in the
2732: <a href="pcreapi.html"><b>pcreapi</b></a>
2733: documentation.
1.1 misho 2734: </P>
2735: <P>
2736: Experiments with Perl suggest that it too has similar optimizations, sometimes
2737: leading to anomalous results.
2738: </P>
2739: <br><b>
2740: Verbs that act immediately
2741: </b><br>
2742: <P>
2743: The following verbs act as soon as they are encountered. They may not be
2744: followed by a name.
2745: <pre>
2746: (*ACCEPT)
2747: </pre>
2748: This verb causes the match to end successfully, skipping the remainder of the
2749: pattern. However, when it is inside a subpattern that is called as a
2750: subroutine, only that subpattern is ended successfully. Matching then continues
1.1.1.4 ! misho 2751: at the outer level. If (*ACCEPT) in triggered in a positive assertion, the
! 2752: assertion succeeds; in a negative assertion, the assertion fails.
! 2753: </P>
! 2754: <P>
! 2755: If (*ACCEPT) is inside capturing parentheses, the data so far is captured. For
! 2756: example:
1.1 misho 2757: <pre>
2758: A((?:A|B(*ACCEPT)|C)D)
2759: </pre>
2760: This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
2761: the outer parentheses.
2762: <pre>
2763: (*FAIL) or (*F)
2764: </pre>
2765: This verb causes a matching failure, forcing backtracking to occur. It is
2766: equivalent to (?!) but easier to read. The Perl documentation notes that it is
2767: probably useful only when combined with (?{}) or (??{}). Those are, of course,
2768: Perl features that are not present in PCRE. The nearest equivalent is the
2769: callout feature, as for example in this pattern:
2770: <pre>
2771: a+(?C)(*FAIL)
2772: </pre>
2773: A match with the string "aaaa" always fails, but the callout is taken before
2774: each backtrack happens (in this example, 10 times).
2775: </P>
2776: <br><b>
2777: Recording which path was taken
2778: </b><br>
2779: <P>
2780: There is one verb whose main purpose is to track how a match was arrived at,
2781: though it also has a secondary use in conjunction with advancing the match
2782: starting point (see (*SKIP) below).
2783: <pre>
2784: (*MARK:NAME) or (*:NAME)
2785: </pre>
2786: A name is always required with this verb. There may be as many instances of
2787: (*MARK) as you like in a pattern, and their names do not have to be unique.
2788: </P>
2789: <P>
1.1.1.4 ! misho 2790: When a match succeeds, the name of the last-encountered (*MARK:NAME),
! 2791: (*PRUNE:NAME), or (*THEN:NAME) on the matching path is passed back to the
! 2792: caller as described in the section entitled
1.1.1.2 misho 2793: <a href="pcreapi.html#extradata">"Extra data for <b>pcre_exec()</b>"</a>
1.1 misho 2794: in the
2795: <a href="pcreapi.html"><b>pcreapi</b></a>
2796: documentation. Here is an example of <b>pcretest</b> output, where the /K
2797: modifier requests the retrieval and outputting of (*MARK) data:
2798: <pre>
2799: re> /X(*MARK:A)Y|X(*MARK:B)Z/K
2800: data> XY
2801: 0: XY
2802: MK: A
2803: XZ
2804: 0: XZ
2805: MK: B
2806: </pre>
2807: The (*MARK) name is tagged with "MK:" in this output, and in this example it
2808: indicates which of the two alternatives matched. This is a more efficient way
2809: of obtaining this information than putting each alternative in its own
2810: capturing parentheses.
2811: </P>
2812: <P>
1.1.1.4 ! misho 2813: If a verb with a name is encountered in a positive assertion that is true, the
! 2814: name is recorded and passed back if it is the last-encountered. This does not
! 2815: happen for negative assertions or failing positive assertions.
1.1 misho 2816: </P>
2817: <P>
1.1.1.4 ! misho 2818: After a partial match or a failed match, the last encountered name in the
! 2819: entire match process is returned. For example:
1.1 misho 2820: <pre>
2821: re> /X(*MARK:A)Y|X(*MARK:B)Z/K
2822: data> XP
2823: No match, mark = B
2824: </pre>
2825: Note that in this unanchored example the mark is retained from the match
1.1.1.3 misho 2826: attempt that started at the letter "X" in the subject. Subsequent match
2827: attempts starting at "P" and then with an empty string do not get as far as the
2828: (*MARK) item, but nevertheless do not reset it.
2829: </P>
2830: <P>
2831: If you are interested in (*MARK) values after failed matches, you should
2832: probably set the PCRE_NO_START_OPTIMIZE option
2833: <a href="#nooptimize">(see above)</a>
2834: to ensure that the match is always attempted.
1.1 misho 2835: </P>
2836: <br><b>
2837: Verbs that act after backtracking
2838: </b><br>
2839: <P>
2840: The following verbs do nothing when they are encountered. Matching continues
2841: with what follows, but if there is no subsequent match, causing a backtrack to
2842: the verb, a failure is forced. That is, backtracking cannot pass to the left of
1.1.1.4 ! misho 2843: the verb. However, when one of these verbs appears inside an atomic group or an
! 2844: assertion that is true, its effect is confined to that group, because once the
! 2845: group has been matched, there is never any backtracking into it. In this
! 2846: situation, backtracking can "jump back" to the left of the entire atomic group
! 2847: or assertion. (Remember also, as stated above, that this localization also
! 2848: applies in subroutine calls.)
1.1 misho 2849: </P>
2850: <P>
2851: These verbs differ in exactly what kind of failure occurs when backtracking
1.1.1.4 ! misho 2852: reaches them. The behaviour described below is what happens when the verb is
! 2853: not in a subroutine or an assertion. Subsequent sections cover these special
! 2854: cases.
1.1 misho 2855: <pre>
2856: (*COMMIT)
2857: </pre>
2858: This verb, which may not be followed by a name, causes the whole match to fail
1.1.1.4 ! misho 2859: outright if there is a later matching failure that causes backtracking to reach
! 2860: it. Even if the pattern is unanchored, no further attempts to find a match by
! 2861: advancing the starting point take place. If (*COMMIT) is the only backtracking
! 2862: verb that is encountered, once it has been passed <b>pcre_exec()</b> is
! 2863: committed to finding a match at the current starting point, or not at all. For
! 2864: example:
1.1 misho 2865: <pre>
2866: a+(*COMMIT)b
2867: </pre>
2868: This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2869: dynamic anchor, or "I've started, so I must finish." The name of the most
2870: recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
2871: match failure.
2872: </P>
2873: <P>
1.1.1.4 ! misho 2874: If there is more than one backtracking verb in a pattern, a different one that
! 2875: follows (*COMMIT) may be triggered first, so merely passing (*COMMIT) during a
! 2876: match does not always guarantee that a match must be at this starting point.
! 2877: </P>
! 2878: <P>
1.1 misho 2879: Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
2880: unless PCRE's start-of-match optimizations are turned off, as shown in this
2881: <b>pcretest</b> example:
2882: <pre>
2883: re> /(*COMMIT)abc/
2884: data> xyzabc
2885: 0: abc
2886: xyzabc\Y
2887: No match
2888: </pre>
2889: PCRE knows that any match must start with "a", so the optimization skips along
2890: the subject to "a" before running the first match attempt, which succeeds. When
2891: the optimization is disabled by the \Y escape in the second subject, the match
2892: starts at "x" and so the (*COMMIT) causes it to fail without trying any other
2893: starting points.
2894: <pre>
2895: (*PRUNE) or (*PRUNE:NAME)
2896: </pre>
2897: This verb causes the match to fail at the current starting position in the
1.1.1.4 ! misho 2898: subject if there is a later matching failure that causes backtracking to reach
! 2899: it. If the pattern is unanchored, the normal "bumpalong" advance to the next
! 2900: starting character then happens. Backtracking can occur as usual to the left of
! 2901: (*PRUNE), before it is reached, or when matching to the right of (*PRUNE), but
! 2902: if there is no match to the right, backtracking cannot cross (*PRUNE). In
! 2903: simple cases, the use of (*PRUNE) is just an alternative to an atomic group or
! 2904: possessive quantifier, but there are some uses of (*PRUNE) that cannot be
! 2905: expressed in any other way. In an anchored pattern (*PRUNE) has the same effect
! 2906: as (*COMMIT).
! 2907: </P>
! 2908: <P>
! 2909: The behaviour of (*PRUNE:NAME) is the not the same as (*MARK:NAME)(*PRUNE).
! 2910: It is like (*MARK:NAME) in that the name is remembered for passing back to the
! 2911: caller. However, (*SKIP:NAME) searches only for names set with (*MARK).
1.1 misho 2912: <pre>
2913: (*SKIP)
2914: </pre>
2915: This verb, when given without a name, is like (*PRUNE), except that if the
2916: pattern is unanchored, the "bumpalong" advance is not to the next character,
2917: but to the position in the subject where (*SKIP) was encountered. (*SKIP)
2918: signifies that whatever text was matched leading up to it cannot be part of a
2919: successful match. Consider:
2920: <pre>
2921: a+(*SKIP)b
2922: </pre>
2923: If the subject is "aaaac...", after the first match attempt fails (starting at
2924: the first character in the string), the starting point skips on to start the
2925: next attempt at "c". Note that a possessive quantifer does not have the same
2926: effect as this example; although it would suppress backtracking during the
2927: first match attempt, the second attempt would start at the second character
2928: instead of skipping on to "c".
2929: <pre>
2930: (*SKIP:NAME)
2931: </pre>
1.1.1.4 ! misho 2932: When (*SKIP) has an associated name, its behaviour is modified. When it is
! 2933: triggered, the previous path through the pattern is searched for the most
! 2934: recent (*MARK) that has the same name. If one is found, the "bumpalong" advance
! 2935: is to the subject position that corresponds to that (*MARK) instead of to where
! 2936: (*SKIP) was encountered. If no (*MARK) with a matching name is found, the
! 2937: (*SKIP) is ignored.
! 2938: </P>
! 2939: <P>
! 2940: Note that (*SKIP:NAME) searches only for names set by (*MARK:NAME). It ignores
! 2941: names that are set by (*PRUNE:NAME) or (*THEN:NAME).
1.1 misho 2942: <pre>
2943: (*THEN) or (*THEN:NAME)
2944: </pre>
1.1.1.4 ! misho 2945: This verb causes a skip to the next innermost alternative when backtracking
! 2946: reaches it. That is, it cancels any further backtracking within the current
! 2947: alternative. Its name comes from the observation that it can be used for a
! 2948: pattern-based if-then-else block:
1.1 misho 2949: <pre>
2950: ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2951: </pre>
2952: If the COND1 pattern matches, FOO is tried (and possibly further items after
2953: the end of the group if FOO succeeds); on failure, the matcher skips to the
1.1.1.4 ! misho 2954: second alternative and tries COND2, without backtracking into COND1. If that
! 2955: succeeds and BAR fails, COND3 is tried. If subsequently BAZ fails, there are no
! 2956: more alternatives, so there is a backtrack to whatever came before the entire
! 2957: group. If (*THEN) is not inside an alternation, it acts like (*PRUNE).
! 2958: </P>
! 2959: <P>
! 2960: The behaviour of (*THEN:NAME) is the not the same as (*MARK:NAME)(*THEN).
! 2961: It is like (*MARK:NAME) in that the name is remembered for passing back to the
! 2962: caller. However, (*SKIP:NAME) searches only for names set with (*MARK).
1.1 misho 2963: </P>
2964: <P>
1.1.1.4 ! misho 2965: A subpattern that does not contain a | character is just a part of the
! 2966: enclosing alternative; it is not a nested alternation with only one
1.1 misho 2967: alternative. The effect of (*THEN) extends beyond such a subpattern to the
2968: enclosing alternative. Consider this pattern, where A, B, etc. are complex
2969: pattern fragments that do not contain any | characters at this level:
2970: <pre>
2971: A (B(*THEN)C) | D
2972: </pre>
2973: If A and B are matched, but there is a failure in C, matching does not
2974: backtrack into A; instead it moves to the next alternative, that is, D.
2975: However, if the subpattern containing (*THEN) is given an alternative, it
2976: behaves differently:
2977: <pre>
2978: A (B(*THEN)C | (*FAIL)) | D
2979: </pre>
2980: The effect of (*THEN) is now confined to the inner subpattern. After a failure
2981: in C, matching moves to (*FAIL), which causes the whole subpattern to fail
2982: because there are no more alternatives to try. In this case, matching does now
2983: backtrack into A.
2984: </P>
2985: <P>
1.1.1.4 ! misho 2986: Note that a conditional subpattern is not considered as having two
1.1 misho 2987: alternatives, because only one is ever used. In other words, the | character in
2988: a conditional subpattern has a different meaning. Ignoring white space,
2989: consider:
2990: <pre>
2991: ^.*? (?(?=a) a | b(*THEN)c )
2992: </pre>
2993: If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
2994: it initially matches zero characters. The condition (?=a) then fails, the
2995: character "b" is matched, but "c" is not. At this point, matching does not
2996: backtrack to .*? as might perhaps be expected from the presence of the |
2997: character. The conditional subpattern is part of the single alternative that
2998: comprises the whole pattern, and so the match fails. (If there was a backtrack
2999: into .*?, allowing it to match "b", the match would succeed.)
3000: </P>
3001: <P>
3002: The verbs just described provide four different "strengths" of control when
3003: subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
3004: next alternative. (*PRUNE) comes next, failing the match at the current
3005: starting position, but allowing an advance to the next character (for an
3006: unanchored pattern). (*SKIP) is similar, except that the advance may be more
3007: than one character. (*COMMIT) is the strongest, causing the entire match to
3008: fail.
3009: </P>
1.1.1.4 ! misho 3010: <br><b>
! 3011: More than one backtracking verb
! 3012: </b><br>
! 3013: <P>
! 3014: If more than one backtracking verb is present in a pattern, the one that is
! 3015: backtracked onto first acts. For example, consider this pattern, where A, B,
! 3016: etc. are complex pattern fragments:
! 3017: <pre>
! 3018: (A(*COMMIT)B(*THEN)C|ABD)
! 3019: </pre>
! 3020: If A matches but B fails, the backtrack to (*COMMIT) causes the entire match to
! 3021: fail. However, if A and B match, but C fails, the backtrack to (*THEN) causes
! 3022: the next alternative (ABD) to be tried. This behaviour is consistent, but is
! 3023: not always the same as Perl's. It means that if two or more backtracking verbs
! 3024: appear in succession, all the the last of them has no effect. Consider this
! 3025: example:
! 3026: <pre>
! 3027: ...(*COMMIT)(*PRUNE)...
! 3028: </pre>
! 3029: If there is a matching failure to the right, backtracking onto (*PRUNE) cases
! 3030: it to be triggered, and its action is taken. There can never be a backtrack
! 3031: onto (*COMMIT).
! 3032: <a name="btrepeat"></a></P>
! 3033: <br><b>
! 3034: Backtracking verbs in repeated groups
! 3035: </b><br>
! 3036: <P>
! 3037: PCRE differs from Perl in its handling of backtracking verbs in repeated
! 3038: groups. For example, consider:
! 3039: <pre>
! 3040: /(a(*COMMIT)b)+ac/
! 3041: </pre>
! 3042: If the subject is "abac", Perl matches, but PCRE fails because the (*COMMIT) in
! 3043: the second repeat of the group acts.
! 3044: <a name="btassert"></a></P>
! 3045: <br><b>
! 3046: Backtracking verbs in assertions
! 3047: </b><br>
! 3048: <P>
! 3049: (*FAIL) in an assertion has its normal effect: it forces an immediate backtrack.
! 3050: </P>
! 3051: <P>
! 3052: (*ACCEPT) in a positive assertion causes the assertion to succeed without any
! 3053: further processing. In a negative assertion, (*ACCEPT) causes the assertion to
! 3054: fail without any further processing.
! 3055: </P>
! 3056: <P>
! 3057: The other backtracking verbs are not treated specially if they appear in a
! 3058: positive assertion. In particular, (*THEN) skips to the next alternative in the
! 3059: innermost enclosing group that has alternations, whether or not this is within
! 3060: the assertion.
! 3061: </P>
! 3062: <P>
! 3063: Negative assertions are, however, different, in order to ensure that changing a
! 3064: positive assertion into a negative assertion changes its result. Backtracking
! 3065: into (*COMMIT), (*SKIP), or (*PRUNE) causes a negative assertion to be true,
! 3066: without considering any further alternative branches in the assertion.
! 3067: Backtracking into (*THEN) causes it to skip to the next enclosing alternative
! 3068: within the assertion (the normal behaviour), but if the assertion does not have
! 3069: such an alternative, (*THEN) behaves like (*PRUNE).
! 3070: <a name="btsub"></a></P>
! 3071: <br><b>
! 3072: Backtracking verbs in subroutines
! 3073: </b><br>
! 3074: <P>
! 3075: These behaviours occur whether or not the subpattern is called recursively.
! 3076: Perl's treatment of subroutines is different in some cases.
! 3077: </P>
! 3078: <P>
! 3079: (*FAIL) in a subpattern called as a subroutine has its normal effect: it forces
! 3080: an immediate backtrack.
! 3081: </P>
! 3082: <P>
! 3083: (*ACCEPT) in a subpattern called as a subroutine causes the subroutine match to
! 3084: succeed without any further processing. Matching then continues after the
! 3085: subroutine call.
! 3086: </P>
! 3087: <P>
! 3088: (*COMMIT), (*SKIP), and (*PRUNE) in a subpattern called as a subroutine cause
! 3089: the subroutine match to fail.
! 3090: </P>
1.1 misho 3091: <P>
1.1.1.4 ! misho 3092: (*THEN) skips to the next alternative in the innermost enclosing group within
! 3093: the subpattern that has alternatives. If there is no such group within the
! 3094: subpattern, (*THEN) causes the subroutine match to fail.
1.1 misho 3095: </P>
1.1.1.4 ! misho 3096: <br><a name="SEC27" href="#TOC1">SEE ALSO</a><br>
1.1 misho 3097: <P>
3098: <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3),
1.1.1.4 ! misho 3099: <b>pcresyntax</b>(3), <b>pcre</b>(3), <b>pcre16(3)</b>, <b>pcre32(3)</b>.
1.1 misho 3100: </P>
1.1.1.4 ! misho 3101: <br><a name="SEC28" href="#TOC1">AUTHOR</a><br>
1.1 misho 3102: <P>
3103: Philip Hazel
3104: <br>
3105: University Computing Service
3106: <br>
3107: Cambridge CB2 3QH, England.
3108: <br>
3109: </P>
1.1.1.4 ! misho 3110: <br><a name="SEC29" href="#TOC1">REVISION</a><br>
1.1 misho 3111: <P>
1.1.1.4 ! misho 3112: Last updated: 26 April 2013
1.1 misho 3113: <br>
1.1.1.4 ! misho 3114: Copyright © 1997-2013 University of Cambridge.
1.1 misho 3115: <br>
3116: <p>
3117: Return to the <a href="index.html">PCRE index page</a>.
3118: </p>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>