/[pcre]/code/trunk/doc/html/pcrepattern.html
ViewVC logotype

Diff of /code/trunk/doc/html/pcrepattern.html

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 507 by ph10, Wed Mar 10 16:08:01 2010 UTC revision 975 by ph10, Sat Jun 2 11:03:06 2012 UTC
# Line 18  man page, in case the conversion went wr Line 18  man page, in case the conversion went wr
18  <li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a>  <li><a name="TOC3" href="#SEC3">CHARACTERS AND METACHARACTERS</a>
19  <li><a name="TOC4" href="#SEC4">BACKSLASH</a>  <li><a name="TOC4" href="#SEC4">BACKSLASH</a>
20  <li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a>  <li><a name="TOC5" href="#SEC5">CIRCUMFLEX AND DOLLAR</a>
21  <li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT)</a>  <li><a name="TOC6" href="#SEC6">FULL STOP (PERIOD, DOT) AND \N</a>
22  <li><a name="TOC7" href="#SEC7">MATCHING A SINGLE BYTE</a>  <li><a name="TOC7" href="#SEC7">MATCHING A SINGLE DATA UNIT</a>
23  <li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a>  <li><a name="TOC8" href="#SEC8">SQUARE BRACKETS AND CHARACTER CLASSES</a>
24  <li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a>  <li><a name="TOC9" href="#SEC9">POSIX CHARACTER CLASSES</a>
25  <li><a name="TOC10" href="#SEC10">VERTICAL BAR</a>  <li><a name="TOC10" href="#SEC10">VERTICAL BAR</a>
# Line 61  description of PCRE's regular expression Line 61  description of PCRE's regular expression
61  </P>  </P>
62  <P>  <P>
63  The original operation of PCRE was on strings of one-byte characters. However,  The original operation of PCRE was on strings of one-byte characters. However,
64  there is now also support for UTF-8 character strings. To use this,  there is now also support for UTF-8 strings in the original library, and a
65  PCRE must be built to include UTF-8 support, and you must call  second library that supports 16-bit and UTF-16 character strings. To use these
66  <b>pcre_compile()</b> or <b>pcre_compile2()</b> with the PCRE_UTF8 option. There  features, PCRE must be built to include appropriate support. When using UTF
67  is also a special sequence that can be given at the start of a pattern:  strings you must either call the compiling function with the PCRE_UTF8 or
68    PCRE_UTF16 option, or the pattern must start with one of these special
69    sequences:
70  <pre>  <pre>
71    (*UTF8)    (*UTF8)
72      (*UTF16)
73  </pre>  </pre>
74  Starting a pattern with this sequence is equivalent to setting the PCRE_UTF8  Starting a pattern with such a sequence is equivalent to setting the relevant
75  option. This feature is not Perl-compatible. How setting UTF-8 mode affects  option. This feature is not Perl-compatible. How setting a UTF mode affects
76  pattern matching is mentioned in several places below. There is also a summary  pattern matching is mentioned in several places below. There is also a summary
77  of UTF-8 features in the  of features in the
78  <a href="pcre.html#utf8support">section on UTF-8 support</a>  <a href="pcreunicode.html"><b>pcreunicode</b></a>
 in the main  
 <a href="pcre.html"><b>pcre</b></a>  
79  page.  page.
80  </P>  </P>
81  <P>  <P>
82    Another special sequence that may appear at the start of a pattern or in
83    combination with (*UTF8) or (*UTF16) is:
84    <pre>
85      (*UCP)
86    </pre>
87    This has the same effect as setting the PCRE_UCP option: it causes sequences
88    such as \d and \w to use Unicode properties to determine character types,
89    instead of recognizing only characters with codes less than 128 via a lookup
90    table.
91    </P>
92    <P>
93    If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
94    PCRE_NO_START_OPTIMIZE option either at compile or matching time. There are
95    also some more of these special sequences that are concerned with the handling
96    of newlines; they are described below.
97    </P>
98    <P>
99  The remainder of this document discusses the patterns that are supported by  The remainder of this document discusses the patterns that are supported by
100  PCRE when its main matching function, <b>pcre_exec()</b>, is used.  PCRE when one its main matching functions, <b>pcre_exec()</b> (8-bit) or
101  From release 6.0, PCRE offers a second matching function,  <b>pcre16_exec()</b> (16-bit), is used. PCRE also has alternative matching
102  <b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not  functions, <b>pcre_dfa_exec()</b> and <b>pcre16_dfa_exec()</b>, which match using
103  Perl-compatible. Some of the features discussed below are not available when  a different algorithm that is not Perl-compatible. Some of the features
104  <b>pcre_dfa_exec()</b> is used. The advantages and disadvantages of the  discussed below are not available when DFA matching is used. The advantages and
105  alternative function, and how it differs from the normal function, are  disadvantages of the alternative functions, and how they differ from the normal
106  discussed in the  functions, are discussed in the
107  <a href="pcrematching.html"><b>pcrematching</b></a>  <a href="pcrematching.html"><b>pcrematching</b></a>
108  page.  page.
109  </P>  <a name="newlines"></a></P>
110  <br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br>  <br><a name="SEC2" href="#TOC1">NEWLINE CONVENTIONS</a><br>
111  <P>  <P>
112  PCRE supports five different conventions for indicating line breaks in  PCRE supports five different conventions for indicating line breaks in
# Line 111  string with one of the following five se Line 129  string with one of the following five se
129    (*ANYCRLF)   any of the three above    (*ANYCRLF)   any of the three above
130    (*ANY)       all Unicode newline sequences    (*ANY)       all Unicode newline sequences
131  </pre>  </pre>
132  These override the default and the options given to <b>pcre_compile()</b> or  These override the default and the options given to the compiling function. For
133  <b>pcre_compile2()</b>. For example, on a Unix system where LF is the default  example, on a Unix system where LF is the default newline sequence, the pattern
 newline sequence, the pattern  
134  <pre>  <pre>
135    (*CR)a.b    (*CR)a.b
136  </pre>  </pre>
# Line 124  they must be in upper case. If more than Line 141  they must be in upper case. If more than
141  is used.  is used.
142  </P>  </P>
143  <P>  <P>
144  The newline convention does not affect what the \R escape sequence matches. By  The newline convention affects the interpretation of the dot metacharacter when
145  default, this is any Unicode newline sequence, for Perl compatibility. However,  PCRE_DOTALL is not set, and also the behaviour of \N. However, it does not
146  this can be changed; see the description of \R in the section entitled  affect what the \R escape sequence matches. By default, this is any Unicode
147    newline sequence, for Perl compatibility. However, this can be changed; see the
148    description of \R in the section entitled
149  <a href="#newlineseq">"Newline sequences"</a>  <a href="#newlineseq">"Newline sequences"</a>
150  below. A change of \R setting can be combined with a change of newline  below. A change of \R setting can be combined with a change of newline
151  convention.  convention.
# Line 141  corresponding characters in the subject. Line 160  corresponding characters in the subject.
160  </pre>  </pre>
161  matches a portion of a subject string that is identical to itself. When  matches a portion of a subject string that is identical to itself. When
162  caseless matching is specified (the PCRE_CASELESS option), letters are matched  caseless matching is specified (the PCRE_CASELESS option), letters are matched
163  independently of case. In UTF-8 mode, PCRE always understands the concept of  independently of case. In a UTF mode, PCRE always understands the concept of
164  case for characters whose values are less than 128, so caseless matching is  case for characters whose values are less than 128, so caseless matching is
165  always possible. For characters with higher values, the concept of case is  always possible. For characters with higher values, the concept of case is
166  supported if PCRE is compiled with Unicode property support, but not otherwise.  supported if PCRE is compiled with Unicode property support, but not otherwise.
167  If you want to use caseless matching for characters 128 and above, you must  If you want to use caseless matching for characters 128 and above, you must
168  ensure that PCRE is compiled with Unicode property support as well as with  ensure that PCRE is compiled with Unicode property support as well as with
169  UTF-8 support.  UTF support.
170  </P>  </P>
171  <P>  <P>
172  The power of regular expressions comes from the ability to include alternatives  The power of regular expressions comes from the ability to include alternatives
# Line 191  The following sections describe the use Line 210  The following sections describe the use
210  <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC4" href="#TOC1">BACKSLASH</a><br>
211  <P>  <P>
212  The backslash character has several uses. Firstly, if it is followed by a  The backslash character has several uses. Firstly, if it is followed by a
213  non-alphanumeric character, it takes away any special meaning that character  character that is not a number or a letter, it takes away any special meaning
214  may have. This use of backslash as an escape character applies both inside and  that character may have. This use of backslash as an escape character applies
215  outside character classes.  both inside and outside character classes.
216  </P>  </P>
217  <P>  <P>
218  For example, if you want to match a * character, you write \* in the pattern.  For example, if you want to match a * character, you write \* in the pattern.
# Line 203  non-alphanumeric with backslash to speci Line 222  non-alphanumeric with backslash to speci
222  particular, if you want to match a backslash, you write \\.  particular, if you want to match a backslash, you write \\.
223  </P>  </P>
224  <P>  <P>
225  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the  In a UTF mode, only ASCII numbers and letters have any special meaning after a
226    backslash. All other characters (in particular, those whose codepoints are
227    greater than 127) are treated as literals.
228    </P>
229    <P>
230    If a pattern is compiled with the PCRE_EXTENDED option, white space in the
231  pattern (other than in a character class) and characters between a # outside  pattern (other than in a character class) and characters between a # outside
232  a character class and the next newline are ignored. An escaping backslash can  a character class and the next newline are ignored. An escaping backslash can
233  be used to include a whitespace or # character as part of the pattern.  be used to include a white space or # character as part of the pattern.
234  </P>  </P>
235  <P>  <P>
236  If you want to remove the special meaning from a sequence of characters, you  If you want to remove the special meaning from a sequence of characters, you
# Line 221  Perl, $ and @ cause variable interpolati Line 245  Perl, $ and @ cause variable interpolati
245    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
246  </pre>  </pre>
247  The \Q...\E sequence is recognized both inside and outside character classes.  The \Q...\E sequence is recognized both inside and outside character classes.
248    An isolated \E that is not preceded by \Q is ignored. If \Q is not followed
249    by \E later in the pattern, the literal interpretation continues to the end of
250    the pattern (that is, \E is assumed at the end). If the isolated \Q is inside
251    a character class, this causes an error, because the character class is not
252    terminated.
253  <a name="digitsafterbackslash"></a></P>  <a name="digitsafterbackslash"></a></P>
254  <br><b>  <br><b>
255  Non-printing characters  Non-printing characters
# Line 233  but when a pattern is being prepared by Line 262  but when a pattern is being prepared by
262  one of the following escape sequences than the binary character it represents:  one of the following escape sequences than the binary character it represents:
263  <pre>  <pre>
264    \a        alarm, that is, the BEL character (hex 07)    \a        alarm, that is, the BEL character (hex 07)
265    \cx       "control-x", where x is any character    \cx       "control-x", where x is any ASCII character
266    \e        escape (hex 1B)    \e        escape (hex 1B)
267    \f        formfeed (hex 0C)    \f        form feed (hex 0C)
268    \n        linefeed (hex 0A)    \n        linefeed (hex 0A)
269    \r        carriage return (hex 0D)    \r        carriage return (hex 0D)
270    \t        tab (hex 09)    \t        tab (hex 09)
271    \ddd      character with octal code ddd, or back reference    \ddd      character with octal code ddd, or back reference
272    \xhh      character with hex code hh    \xhh      character with hex code hh
273    \x{hhh..} character with hex code hhh..    \x{hhh..} character with hex code hhh.. (non-JavaScript mode)
274      \uhhhh    character with hex code hhhh (JavaScript mode only)
275  </pre>  </pre>
276  The precise effect of \cx is as follows: if x is a lower case letter, it  The precise effect of \cx is as follows: if x is a lower case letter, it
277  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
278  Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex  Thus \cz becomes hex 1A (z is 7A), but \c{ becomes hex 3B ({ is 7B), while
279  7B.  \c; becomes hex 7B (; is 3B). If the byte following \c has a value greater
280  </P>  than 127, a compile-time error occurs. This locks out non-ASCII characters in
281  <P>  all modes. (When PCRE is compiled in EBCDIC mode, all byte values are valid. A
282  After \x, from zero to two hexadecimal digits are read (letters can be in  lower case letter is converted to upper case, and then the 0xc0 bits are
283  upper or lower case). Any number of hexadecimal digits may appear between \x{  flipped.)
284  and }, but the value of the character code must be less than 256 in non-UTF-8  </P>
285  mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in  <P>
286  hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code  By default, after \x, from zero to two hexadecimal digits are read (letters
287  point, which is 10FFFF.  can be in upper or lower case). Any number of hexadecimal digits may appear
288    between \x{ and }, but the character code is constrained as follows:
289    <pre>
290      8-bit non-UTF mode    less than 0x100
291      8-bit UTF-8 mode      less than 0x10ffff and a valid codepoint
292      16-bit non-UTF mode   less than 0x10000
293      16-bit UTF-16 mode    less than 0x10ffff and a valid codepoint
294    </pre>
295    Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so-called
296    "surrogate" codepoints).
297  </P>  </P>
298  <P>  <P>
299  If characters other than hexadecimal digits appear between \x{ and }, or if  If characters other than hexadecimal digits appear between \x{ and }, or if
# Line 263  initial \x will be interpreted as a basi Line 302  initial \x will be interpreted as a basi
302  following digits, giving a character whose value is zero.  following digits, giving a character whose value is zero.
303  </P>  </P>
304  <P>  <P>
305    If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x is
306    as just described only when it is followed by two hexadecimal digits.
307    Otherwise, it matches a literal "x" character. In JavaScript mode, support for
308    code points greater than 256 is provided by \u, which must be followed by
309    four hexadecimal digits; otherwise it matches a literal "u" character.
310    </P>
311    <P>
312  Characters whose value is less than 256 can be defined by either of the two  Characters whose value is less than 256 can be defined by either of the two
313  syntaxes for \x. There is no difference in the way they are handled. For  syntaxes for \x (or by \u in JavaScript mode). There is no difference in the
314  example, \xdc is exactly the same as \x{dc}.  way they are handled. For example, \xdc is exactly the same as \x{dc} (or
315    \u00dc in JavaScript mode).
316  </P>  </P>
317  <P>  <P>
318  After \0 up to two further octal digits are read. If there are fewer than two  After \0 up to two further octal digits are read. If there are fewer than two
# Line 288  following the discussion of Line 335  following the discussion of
335  Inside a character class, or if the decimal number is greater than 9 and there  Inside a character class, or if the decimal number is greater than 9 and there
336  have not been that many capturing subpatterns, PCRE re-reads up to three octal  have not been that many capturing subpatterns, PCRE re-reads up to three octal
337  digits following the backslash, and uses them to generate a data character. Any  digits following the backslash, and uses them to generate a data character. Any
338  subsequent digits stand for themselves. In non-UTF-8 mode, the value of a  subsequent digits stand for themselves. The value of the character is
339  character specified in octal must be less than \400. In UTF-8 mode, values up  constrained in the same way as characters specified in hexadecimal.
340  to \777 are permitted. For example:  For example:
341  <pre>  <pre>
342    \040   is another way of writing a space    \040   is another way of writing a space
343    \40    is the same, provided there are fewer than 40 previous capturing subpatterns    \40    is the same, provided there are fewer than 40 previous capturing subpatterns
# Line 299  to \777 are permitted. For example: Line 346  to \777 are permitted. For example:
346    \011   is always a tab    \011   is always a tab
347    \0113  is a tab followed by the character "3"    \0113  is a tab followed by the character "3"
348    \113   might be a back reference, otherwise the character with octal code 113    \113   might be a back reference, otherwise the character with octal code 113
349    \377   might be a back reference, otherwise the byte consisting entirely of 1 bits    \377   might be a back reference, otherwise the value 255 (decimal)
350    \81    is either a back reference, or a binary zero followed by the two characters "8" and "1"    \81    is either a back reference, or a binary zero followed by the two characters "8" and "1"
351  </pre>  </pre>
352  Note that octal values of 100 or greater must not be introduced by a leading  Note that octal values of 100 or greater must not be introduced by a leading
# Line 307  zero, because no more than three octal d Line 354  zero, because no more than three octal d
354  </P>  </P>
355  <P>  <P>
356  All the sequences that define a single character value can be used both inside  All the sequences that define a single character value can be used both inside
357  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, \b is
358  sequence \b is interpreted as the backspace character (hex 08), and the  interpreted as the backspace character (hex 08).
359  sequences \R and \X are interpreted as the characters "R" and "X",  </P>
360  respectively. Outside a character class, these sequences have different  <P>
361  meanings  \N is not allowed in a character class. \B, \R, and \X are not special
362  <a href="#uniextseq">(see below).</a>  inside a character class. Like other unrecognized escape sequences, they are
363    treated as the literal characters "B", "R", and "X" by default, but cause an
364    error if the PCRE_EXTRA option is set. Outside a character class, these
365    sequences have different meanings.
366    </P>
367    <br><b>
368    Unsupported escape sequences
369    </b><br>
370    <P>
371    In Perl, the sequences \l, \L, \u, and \U are recognized by its string
372    handler and used to modify the case of following characters. By default, PCRE
373    does not support these escape sequences. However, if the PCRE_JAVASCRIPT_COMPAT
374    option is set, \U matches a "U" character, and \u can be used to define a
375    character by code point, as described in the previous section.
376  </P>  </P>
377  <br><b>  <br><b>
378  Absolute and relative back references  Absolute and relative back references
# Line 337  Note that \g{...} (Perl syntax) and \g&# Line 397  Note that \g{...} (Perl syntax) and \g&#
397  synonymous. The former is a back reference; the latter is a  synonymous. The former is a back reference; the latter is a
398  <a href="#subpatternsassubroutines">subroutine</a>  <a href="#subpatternsassubroutines">subroutine</a>
399  call.  call.
400  </P>  <a name="genericchartypes"></a></P>
401  <br><b>  <br><b>
402  Generic character types  Generic character types
403  </b><br>  </b><br>
404  <P>  <P>
405  Another use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types:
 following are always recognized:  
406  <pre>  <pre>
407    \d     any decimal digit    \d     any decimal digit
408    \D     any character that is not a decimal digit    \D     any character that is not a decimal digit
409    \h     any horizontal whitespace character    \h     any horizontal white space character
410    \H     any character that is not a horizontal whitespace character    \H     any character that is not a horizontal white space character
411    \s     any whitespace character    \s     any white space character
412    \S     any character that is not a whitespace character    \S     any character that is not a white space character
413    \v     any vertical whitespace character    \v     any vertical white space character
414    \V     any character that is not a vertical whitespace character    \V     any character that is not a vertical white space character
415    \w     any "word" character    \w     any "word" character
416    \W     any "non-word" character    \W     any "non-word" character
417  </pre>  </pre>
418  Each pair of escape sequences partitions the complete set of characters into  There is also the single sequence \N, which matches a non-newline character.
419  two disjoint sets. Any given character matches one, and only one, of each pair.  This is the same as
420    <a href="#fullstopdot">the "." metacharacter</a>
421    when PCRE_DOTALL is not set. Perl also uses \N to match characters by name;
422    PCRE does not support this.
423  </P>  </P>
424  <P>  <P>
425  These character type sequences can appear both inside and outside character  Each pair of lower and upper case escape sequences partitions the complete set
426    of characters into two disjoint sets. Any given character matches one, and only
427    one, of each pair. The sequences can appear both inside and outside character
428  classes. They each match one character of the appropriate type. If the current  classes. They each match one character of the appropriate type. If the current
429  matching point is at the end of the subject string, all of them fail, since  matching point is at the end of the subject string, all of them fail, because
430  there is no character to match.  there is no character to match.
431  </P>  </P>
432  <P>  <P>
# Line 373  included in a Perl script, \s may match Line 437  included in a Perl script, \s may match
437  does.  does.
438  </P>  </P>
439  <P>  <P>
440  In UTF-8 mode, characters with values greater than 128 never match \d, \s, or  A "word" character is an underscore or any character that is a letter or digit.
441  \w, and always match \D, \S, and \W. This is true even when Unicode  By default, the definition of letters and digits is controlled by PCRE's
442  character property support is available. These sequences retain their original  low-valued character tables, and may vary if locale-specific matching is taking
443  meanings from before UTF-8 support was available, mainly for efficiency  place (see
444  reasons. Note that this also affects \b, because it is defined in terms of \w  <a href="pcreapi.html#localesupport">"Locale support"</a>
445  and \W.  in the
446    <a href="pcreapi.html"><b>pcreapi</b></a>
447    page). For example, in a French locale such as "fr_FR" in Unix-like systems,
448    or "french" in Windows, some character codes greater than 128 are used for
449    accented letters, and these are then matched by \w. The use of locales with
450    Unicode is discouraged.
451  </P>  </P>
452  <P>  <P>
453  The sequences \h, \H, \v, and \V are Perl 5.10 features. In contrast to the  By default, in a UTF mode, characters with values greater than 128 never match
454  other sequences, these do match certain high-valued codepoints in UTF-8 mode.  \d, \s, or \w, and always match \D, \S, and \W. These sequences retain
455  The horizontal space characters are:  their original meanings from before UTF support was available, mainly for
456    efficiency reasons. However, if PCRE is compiled with Unicode property support,
457    and the PCRE_UCP option is set, the behaviour is changed so that Unicode
458    properties are used to determine character types, as follows:
459    <pre>
460      \d  any character that \p{Nd} matches (decimal digit)
461      \s  any character that \p{Z} matches, plus HT, LF, FF, CR
462      \w  any character that \p{L} or \p{N} matches, plus underscore
463    </pre>
464    The upper case escapes match the inverse sets of characters. Note that \d
465    matches only decimal digits, whereas \w matches any Unicode digit, as well as
466    any Unicode letter, and underscore. Note also that PCRE_UCP affects \b, and
467    \B because they are defined in terms of \w and \W. Matching these sequences
468    is noticeably slower when PCRE_UCP is set.
469    </P>
470    <P>
471    The sequences \h, \H, \v, and \V are features that were added to Perl at
472    release 5.10. In contrast to the other sequences, which match only ASCII
473    characters by default, these always match certain high-valued codepoints,
474    whether or not PCRE_UCP is set. The horizontal space characters are:
475  <pre>  <pre>
476    U+0009     Horizontal tab    U+0009     Horizontal tab
477    U+0020     Space    U+0020     Space
# Line 409  The vertical space characters are: Line 497  The vertical space characters are:
497  <pre>  <pre>
498    U+000A     Linefeed    U+000A     Linefeed
499    U+000B     Vertical tab    U+000B     Vertical tab
500    U+000C     Formfeed    U+000C     Form feed
501    U+000D     Carriage return    U+000D     Carriage return
502    U+0085     Next line    U+0085     Next line
503    U+2028     Line separator    U+2028     Line separator
504    U+2029     Paragraph separator    U+2029     Paragraph separator
505  </PRE>  </pre>
506  </P>  In 8-bit, non-UTF-8 mode, only the characters with codepoints less than 256 are
507  <P>  relevant.
 A "word" character is an underscore or any character less than 256 that is a  
 letter or digit. The definition of letters and digits is controlled by PCRE's  
 low-valued character tables, and may vary if locale-specific matching is taking  
 place (see  
 <a href="pcreapi.html#localesupport">"Locale support"</a>  
 in the  
 <a href="pcreapi.html"><b>pcreapi</b></a>  
 page). For example, in a French locale such as "fr_FR" in Unix-like systems,  
 or "french" in Windows, some character codes greater than 128 are used for  
 accented letters, and these are matched by \w. The use of locales with Unicode  
 is discouraged.  
508  <a name="newlineseq"></a></P>  <a name="newlineseq"></a></P>
509  <br><b>  <br><b>
510  Newline sequences  Newline sequences
511  </b><br>  </b><br>
512  <P>  <P>
513  Outside a character class, by default, the escape sequence \R matches any  Outside a character class, by default, the escape sequence \R matches any
514  Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \R is  Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent to the
515  equivalent to the following:  following:
516  <pre>  <pre>
517    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)    (?&#62;\r\n|\n|\x0b|\f|\r|\x85)
518  </pre>  </pre>
# Line 443  This is an example of an "atomic group", Line 520  This is an example of an "atomic group",
520  <a href="#atomicgroup">below.</a>  <a href="#atomicgroup">below.</a>
521  This particular group matches either the two-character sequence CR followed by  This particular group matches either the two-character sequence CR followed by
522  LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,  LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
523  U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next  U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
524  line, U+0085). The two-character sequence is treated as a single unit that  line, U+0085). The two-character sequence is treated as a single unit that
525  cannot be split.  cannot be split.
526  </P>  </P>
527  <P>  <P>
528  In UTF-8 mode, two additional characters whose codepoints are greater than 255  In other modes, two additional characters whose codepoints are greater than 255
529  are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).  are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
530  Unicode character property support is not needed for these characters to be  Unicode character property support is not needed for these characters to be
531  recognized.  recognized.
# Line 465  one of the following sequences: Line 542  one of the following sequences:
542    (*BSR_ANYCRLF)   CR, LF, or CRLF only    (*BSR_ANYCRLF)   CR, LF, or CRLF only
543    (*BSR_UNICODE)   any Unicode newline sequence    (*BSR_UNICODE)   any Unicode newline sequence
544  </pre>  </pre>
545  These override the default and the options given to <b>pcre_compile()</b> or  These override the default and the options given to the compiling function, but
546  <b>pcre_compile2()</b>, but they can be overridden by options given to  they can themselves be overridden by options given to a matching function. Note
547  <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. Note that these special settings,  that these special settings, which are not Perl-compatible, are recognized only
548  which are not Perl-compatible, are recognized only at the very start of a  at the very start of a pattern, and that they must be in upper case. If more
549  pattern, and that they must be in upper case. If more than one of them is  than one of them is present, the last one is used. They can be combined with a
550  present, the last one is used. They can be combined with a change of newline  change of newline convention; for example, a pattern can start with:
 convention, for example, a pattern can start with:  
551  <pre>  <pre>
552    (*ANY)(*BSR_ANYCRLF)    (*ANY)(*BSR_ANYCRLF)
553  </pre>  </pre>
554  Inside a character class, \R matches the letter "R".  They can also be combined with the (*UTF8), (*UTF16), or (*UCP) special
555    sequences. Inside a character class, \R is treated as an unrecognized escape
556    sequence, and so matches the letter "R" by default, but causes an error if
557    PCRE_EXTRA is set.
558  <a name="uniextseq"></a></P>  <a name="uniextseq"></a></P>
559  <br><b>  <br><b>
560  Unicode character properties  Unicode character properties
# Line 483  Unicode character properties Line 562  Unicode character properties
562  <P>  <P>
563  When PCRE is built with Unicode character property support, three additional  When PCRE is built with Unicode character property support, three additional
564  escape sequences that match characters with specific properties are available.  escape sequences that match characters with specific properties are available.
565  When not in UTF-8 mode, these sequences are of course limited to testing  When in 8-bit non-UTF-8 mode, these sequences are of course limited to testing
566  characters whose codepoints are less than 256, but they do work in this mode.  characters whose codepoints are less than 256, but they do work in this mode.
567  The extra escape sequences are:  The extra escape sequences are:
568  <pre>  <pre>
# Line 492  The extra escape sequences are: Line 571  The extra escape sequences are:
571    \X       an extended Unicode sequence    \X       an extended Unicode sequence
572  </pre>  </pre>
573  The property names represented by <i>xx</i> above are limited to the Unicode  The property names represented by <i>xx</i> above are limited to the Unicode
574  script names, the general category properties, and "Any", which matches any  script names, the general category properties, "Any", which matches any
575  character (including newline). Other properties such as "InMusicalSymbols" are  character (including newline), and some special PCRE properties (described
576  not currently supported by PCRE. Note that \P{Any} does not match any  in the
577  characters, so always causes a match failure.  <a href="#extraprops">next section).</a>
578    Other Perl properties such as "InMusicalSymbols" are not currently supported by
579    PCRE. Note that \P{Any} does not match any characters, so always causes a
580    match failure.
581  </P>  </P>
582  <P>  <P>
583  Sets of Unicode characters are defined as belonging to certain scripts. A  Sets of Unicode characters are defined as belonging to certain scripts. A
# Line 514  Armenian, Line 596  Armenian,
596  Avestan,  Avestan,
597  Balinese,  Balinese,
598  Bamum,  Bamum,
599    Batak,
600  Bengali,  Bengali,
601  Bopomofo,  Bopomofo,
602    Brahmi,
603  Braille,  Braille,
604  Buginese,  Buginese,
605  Buhid,  Buhid,
606  Canadian_Aboriginal,  Canadian_Aboriginal,
607  Carian,  Carian,
608    Chakma,
609  Cham,  Cham,
610  Cherokee,  Cherokee,
611  Common,  Common,
# Line 563  Lisu, Line 648  Lisu,
648  Lycian,  Lycian,
649  Lydian,  Lydian,
650  Malayalam,  Malayalam,
651    Mandaic,
652  Meetei_Mayek,  Meetei_Mayek,
653    Meroitic_Cursive,
654    Meroitic_Hieroglyphs,
655    Miao,
656  Mongolian,  Mongolian,
657  Myanmar,  Myanmar,
658  New_Tai_Lue,  New_Tai_Lue,
# Line 582  Rejang, Line 671  Rejang,
671  Runic,  Runic,
672  Samaritan,  Samaritan,
673  Saurashtra,  Saurashtra,
674    Sharada,
675  Shavian,  Shavian,
676  Sinhala,  Sinhala,
677    Sora_Sompeng,
678  Sundanese,  Sundanese,
679  Syloti_Nagri,  Syloti_Nagri,
680  Syriac,  Syriac,
# Line 592  Tagbanwa, Line 683  Tagbanwa,
683  Tai_Le,  Tai_Le,
684  Tai_Tham,  Tai_Tham,
685  Tai_Viet,  Tai_Viet,
686    Takri,
687  Tamil,  Tamil,
688  Telugu,  Telugu,
689  Thaana,  Thaana,
# Line 603  Vai, Line 695  Vai,
695  Yi.  Yi.
696  </P>  </P>
697  <P>  <P>
698  Each character has exactly one general category property, specified by a  Each character has exactly one Unicode general category property, specified by
699  two-letter abbreviation. For compatibility with Perl, negation can be specified  a two-letter abbreviation. For compatibility with Perl, negation can be
700  by including a circumflex between the opening brace and the property name. For  specified by including a circumflex between the opening brace and the property
701  example, \p{^Lu} is the same as \P{Lu}.  name. For example, \p{^Lu} is the same as \P{Lu}.
702  </P>  </P>
703  <P>  <P>
704  If only one letter is specified with \p or \P, it includes all the general  If only one letter is specified with \p or \P, it includes all the general
# Line 669  a modifier or "other". Line 761  a modifier or "other".
761  </P>  </P>
762  <P>  <P>
763  The Cs (Surrogate) property applies only to characters in the range U+D800 to  The Cs (Surrogate) property applies only to characters in the range U+D800 to
764  U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so  U+DFFF. Such characters are not valid in Unicode strings and so
765  cannot be tested by PCRE, unless UTF-8 validity checking has been turned off  cannot be tested by PCRE, unless UTF validity checking has been turned off
766  (see the discussion of PCRE_NO_UTF8_CHECK in the  (see the discussion of PCRE_NO_UTF8_CHECK and PCRE_NO_UTF16_CHECK in the
767  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
768  page). Perl does not support the Cs property.  page). Perl does not support the Cs property.
769  </P>  </P>
# Line 701  atomic group Line 793  atomic group
793  <a href="#atomicgroup">(see below).</a>  <a href="#atomicgroup">(see below).</a>
794  Characters with the "mark" property are typically accents that affect the  Characters with the "mark" property are typically accents that affect the
795  preceding character. None of them have codepoints less than 256, so in  preceding character. None of them have codepoints less than 256, so in
796  non-UTF-8 mode \X matches any one character.  8-bit non-UTF-8 mode \X matches any one character.
797    </P>
798    <P>
799    Note that recent versions of Perl have changed \X to match what Unicode calls
800    an "extended grapheme cluster", which has a more complicated definition.
801  </P>  </P>
802  <P>  <P>
803  Matching characters by Unicode property is not fast, because PCRE has to search  Matching characters by Unicode property is not fast, because PCRE has to search
804  a structure that contains data for over fifteen thousand characters. That is  a structure that contains data for over fifteen thousand characters. That is
805  why the traditional escape sequences such as \d and \w do not use Unicode  why the traditional escape sequences such as \d and \w do not use Unicode
806  properties in PCRE.  properties in PCRE by default, though you can make them do so by setting the
807    PCRE_UCP option or by starting the pattern with (*UCP).
808    <a name="extraprops"></a></P>
809    <br><b>
810    PCRE's additional properties
811    </b><br>
812    <P>
813    As well as the standard Unicode properties described in the previous
814    section, PCRE supports four more that make it possible to convert traditional
815    escape sequences such as \w and \s and POSIX character classes to use Unicode
816    properties. PCRE uses these non-standard, non-Perl properties internally when
817    PCRE_UCP is set. They are:
818    <pre>
819      Xan   Any alphanumeric character
820      Xps   Any POSIX space character
821      Xsp   Any Perl space character
822      Xwd   Any Perl "word" character
823    </pre>
824    Xan matches characters that have either the L (letter) or the N (number)
825    property. Xps matches the characters tab, linefeed, vertical tab, form feed, or
826    carriage return, and any other character that has the Z (separator) property.
827    Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the
828    same characters as Xan, plus underscore.
829  <a name="resetmatchstart"></a></P>  <a name="resetmatchstart"></a></P>
830  <br><b>  <br><b>
831  Resetting the match start  Resetting the match start
832  </b><br>  </b><br>
833  <P>  <P>
834  The escape sequence \K, which is a Perl 5.10 feature, causes any previously  The escape sequence \K causes any previously matched characters not to be
835  matched characters not to be included in the final matched sequence. For  included in the final matched sequence. For example, the pattern:
 example, the pattern:  
836  <pre>  <pre>
837    foo\Kbar    foo\Kbar
838  </pre>  </pre>
# Line 756  The backslashed assertions are: Line 873  The backslashed assertions are:
873    \z     matches only at the end of the subject    \z     matches only at the end of the subject
874    \G     matches at the first matching position in the subject    \G     matches at the first matching position in the subject
875  </pre>  </pre>
876  These assertions may not appear in character classes (but note that \b has a  Inside a character class, \b has a different meaning; it matches the backspace
877  different meaning, namely the backspace character, inside a character class).  character. If any other of these assertions appears in a character class, by
878    default it matches the corresponding literal character (for example, \B
879    matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
880    escape sequence" error is generated instead.
881  </P>  </P>
882  <P>  <P>
883  A word boundary is a position in the subject string where the current character  A word boundary is a position in the subject string where the current character
884  and the previous character do not both match \w or \W (i.e. one matches  and the previous character do not both match \w or \W (i.e. one matches
885  \w and the other matches \W), or the start or end of the string if the  \w and the other matches \W), or the start or end of the string if the
886  first or last character matches \w, respectively. Neither PCRE nor Perl has a  first or last character matches \w, respectively. In a UTF mode, the meanings
887  separte "start of word" or "end of word" metasequence. However, whatever  of \w and \W can be changed by setting the PCRE_UCP option. When this is
888  follows \b normally determines which it is. For example, the fragment  done, it also affects \b and \B. Neither PCRE nor Perl has a separate "start
889  \ba matches "a" at the start of a word.  of word" or "end of word" metasequence. However, whatever follows \b normally
890    determines which it is. For example, the fragment \ba matches "a" at the start
891    of a word.
892  </P>  </P>
893  <P>  <P>
894  The \A, \Z, and \z assertions differ from the traditional circumflex and  The \A, \Z, and \z assertions differ from the traditional circumflex and
# Line 853  PCRE_DOLLAR_ENDONLY option is ignored if Line 975  PCRE_DOLLAR_ENDONLY option is ignored if
975  Note that the sequences \A, \Z, and \z can be used to match the start and  Note that the sequences \A, \Z, and \z can be used to match the start and
976  end of the subject in both modes, and if all branches of a pattern start with  end of the subject in both modes, and if all branches of a pattern start with
977  \A it is always anchored, whether or not PCRE_MULTILINE is set.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
978  </P>  <a name="fullstopdot"></a></P>
979  <br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC6" href="#TOC1">FULL STOP (PERIOD, DOT) AND \N</a><br>
980  <P>  <P>
981  Outside a character class, a dot in the pattern matches any one character in  Outside a character class, a dot in the pattern matches any one character in
982  the subject string except (by default) a character that signifies the end of a  the subject string except (by default) a character that signifies the end of a
983  line. In UTF-8 mode, the matched character may be more than one byte long.  line.
984  </P>  </P>
985  <P>  <P>
986  When a line ending is defined as a single character, dot never matches that  When a line ending is defined as a single character, dot never matches that
# Line 879  The handling of dot is entirely independ Line 1001  The handling of dot is entirely independ
1001  dollar, the only relationship being that they both involve newlines. Dot has no  dollar, the only relationship being that they both involve newlines. Dot has no
1002  special meaning in a character class.  special meaning in a character class.
1003  </P>  </P>
 <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  
1004  <P>  <P>
1005  Outside a character class, the escape sequence \C matches any one byte, both  The escape sequence \N behaves like a dot, except that it is not affected by
1006  in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending  the PCRE_DOTALL option. In other words, it matches any character except one
1007  characters. The feature is provided in Perl in order to match individual bytes  that signifies the end of a line. Perl also uses \N to match characters by
1008  in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,  name; PCRE does not support this.
1009  what remains in the string may be a malformed UTF-8 string. For this reason,  </P>
1010  the \C escape sequence is best avoided.  <br><a name="SEC7" href="#TOC1">MATCHING A SINGLE DATA UNIT</a><br>
1011    <P>
1012    Outside a character class, the escape sequence \C matches any one data unit,
1013    whether or not a UTF mode is set. In the 8-bit library, one data unit is one
1014    byte; in the 16-bit library it is a 16-bit unit. Unlike a dot, \C always
1015    matches line-ending characters. The feature is provided in Perl in order to
1016    match individual bytes in UTF-8 mode, but it is unclear how it can usefully be
1017    used. Because \C breaks up characters into individual data units, matching one
1018    unit with \C in a UTF mode means that the rest of the string may start with a
1019    malformed UTF character. This has undefined results, because PCRE assumes that
1020    it is dealing with valid UTF strings (and by default it checks this at the
1021    start of processing unless the PCRE_NO_UTF8_CHECK or PCRE_NO_UTF16_CHECK option
1022    is used).
1023  </P>  </P>
1024  <P>  <P>
1025  PCRE does not allow \C to appear in lookbehind assertions  PCRE does not allow \C to appear in lookbehind assertions
1026  <a href="#lookbehind">(described below),</a>  <a href="#lookbehind">(described below)</a>
1027  because in UTF-8 mode this would make it impossible to calculate the length of  in a UTF mode, because this would make it impossible to calculate the length of
1028  the lookbehind.  the lookbehind.
1029    </P>
1030    <P>
1031    In general, the \C escape sequence is best avoided. However, one
1032    way of using it that avoids the problem of malformed UTF characters is to use a
1033    lookahead to check the length of the next character, as in this pattern, which
1034    could be used with a UTF-8 string (ignore white space and line breaks):
1035    <pre>
1036      (?| (?=[\x00-\x7f])(\C) |
1037          (?=[\x80-\x{7ff}])(\C)(\C) |
1038          (?=[\x{800}-\x{ffff}])(\C)(\C)(\C) |
1039          (?=[\x{10000}-\x{1fffff}])(\C)(\C)(\C)(\C))
1040    </pre>
1041    A group that starts with (?| resets the capturing parentheses numbers in each
1042    alternative (see
1043    <a href="#dupsubpatternnumber">"Duplicate Subpattern Numbers"</a>
1044    below). The assertions at the start of each branch check the next UTF-8
1045    character for values whose encoding uses 1, 2, 3, or 4 bytes, respectively. The
1046    character's individual bytes are then captured by the appropriate number of
1047    groups.
1048  <a name="characterclass"></a></P>  <a name="characterclass"></a></P>
1049  <br><a name="SEC8" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>  <br><a name="SEC8" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
1050  <P>  <P>
# Line 904  a member of the class, it should be the Line 1056  a member of the class, it should be the
1056  (after an initial circumflex, if present) or escaped with a backslash.  (after an initial circumflex, if present) or escaped with a backslash.
1057  </P>  </P>
1058  <P>  <P>
1059  A character class matches a single character in the subject. In UTF-8 mode, the  A character class matches a single character in the subject. In a UTF mode, the
1060  character may be more than one byte long. A matched character must be in the  character may be more than one data unit long. A matched character must be in
1061  set of characters defined by the class, unless the first character in the class  the set of characters defined by the class, unless the first character in the
1062  definition is a circumflex, in which case the subject character must not be in  class definition is a circumflex, in which case the subject character must not
1063  the set defined by the class. If a circumflex is actually required as a member  be in the set defined by the class. If a circumflex is actually required as a
1064  of the class, ensure it is not the first character, or escape it with a  member of the class, ensure it is not the first character, or escape it with a
1065  backslash.  backslash.
1066  </P>  </P>
1067  <P>  <P>
# Line 922  string, and therefore it fails if the cu Line 1074  string, and therefore it fails if the cu
1074  string.  string.
1075  </P>  </P>
1076  <P>  <P>
1077  In UTF-8 mode, characters with values greater than 255 can be included in a  In UTF-8 (UTF-16) mode, characters with values greater than 255 (0xffff) can be
1078  class as a literal string of bytes, or by using the \x{ escaping mechanism.  included in a class as a literal string of data units, or by using the \x{
1079    escaping mechanism.
1080  </P>  </P>
1081  <P>  <P>
1082  When caseless matching is set, any letters in a class represent both their  When caseless matching is set, any letters in a class represent both their
1083  upper case and lower case versions, so for example, a caseless [aeiou] matches  upper case and lower case versions, so for example, a caseless [aeiou] matches
1084  "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a  "A" as well as "a", and a caseless [^aeiou] does not match "A", whereas a
1085  caseful version would. In UTF-8 mode, PCRE always understands the concept of  caseful version would. In a UTF mode, PCRE always understands the concept of
1086  case for characters whose values are less than 128, so caseless matching is  case for characters whose values are less than 128, so caseless matching is
1087  always possible. For characters with higher values, the concept of case is  always possible. For characters with higher values, the concept of case is
1088  supported if PCRE is compiled with Unicode property support, but not otherwise.  supported if PCRE is compiled with Unicode property support, but not otherwise.
1089  If you want to use caseless matching in UTF8-mode for characters 128 and above,  If you want to use caseless matching in a UTF mode for characters 128 and
1090  you must ensure that PCRE is compiled with Unicode property support as well as  above, you must ensure that PCRE is compiled with Unicode property support as
1091  with UTF-8 support.  well as with UTF support.
1092  </P>  </P>
1093  <P>  <P>
1094  Characters that might indicate line breaks are never treated in any special way  Characters that might indicate line breaks are never treated in any special way
# Line 961  followed by two other characters. The oc Line 1114  followed by two other characters. The oc
1114  </P>  </P>
1115  <P>  <P>
1116  Ranges operate in the collating sequence of character values. They can also be  Ranges operate in the collating sequence of character values. They can also be
1117  used for characters specified numerically, for example [\000-\037]. In UTF-8  used for characters specified numerically, for example [\000-\037]. Ranges
1118  mode, ranges can include characters whose values are greater than 255, for  can include any characters that are valid for the current mode.
 example [\x{100}-\x{2ff}].  
1119  </P>  </P>
1120  <P>  <P>
1121  If a range that includes letters is used when caseless matching is set, it  If a range that includes letters is used when caseless matching is set, it
1122  matches the letters in either case. For example, [W-c] is equivalent to  matches the letters in either case. For example, [W-c] is equivalent to
1123  [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character  [][\\^_`wxyzabc], matched caselessly, and in a non-UTF mode, if character
1124  tables for a French locale are in use, [\xc8-\xcb] matches accented E  tables for a French locale are in use, [\xc8-\xcb] matches accented E
1125  characters in both cases. In UTF-8 mode, PCRE supports the concept of case for  characters in both cases. In UTF modes, PCRE supports the concept of case for
1126  characters with values greater than 128 only when it is compiled with Unicode  characters with values greater than 128 only when it is compiled with Unicode
1127  property support.  property support.
1128  </P>  </P>
1129  <P>  <P>
1130  The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear  The character escape sequences \d, \D, \h, \H, \p, \P, \s, \S, \v,
1131  in a character class, and add the characters that they match to the class. For  \V, \w, and \W may appear in a character class, and add the characters that
1132  example, [\dABCDEF] matches any hexadecimal digit. A circumflex can  they match to the class. For example, [\dABCDEF] matches any hexadecimal
1133  conveniently be used with the upper case character types to specify a more  digit. In UTF modes, the PCRE_UCP option affects the meanings of \d, \s, \w
1134  restricted set of characters than the matching lower case type. For example,  and their upper case partners, just as it does when they appear outside a
1135  the class [^\W_] matches any letter or digit, but not underscore.  character class, as described in the section entitled
1136    <a href="#genericchartypes">"Generic character types"</a>
1137    above. The escape sequence \b has a different meaning inside a character
1138    class; it matches the backspace character. The sequences \B, \N, \R, and \X
1139    are not special inside a character class. Like any other unrecognized escape
1140    sequences, they are treated as the literal characters "B", "N", "R", and "X" by
1141    default, but cause an error if the PCRE_EXTRA option is set.
1142    </P>
1143    <P>
1144    A circumflex can conveniently be used with the upper case character types to
1145    specify a more restricted set of characters than the matching lower case type.
1146    For example, the class [^\W_] matches any letter or digit, but not underscore,
1147    whereas [\w] includes underscore. A positive character class should be read as
1148    "something OR something OR ..." and a negative class as "NOT something AND NOT
1149    something AND NOT ...".
1150  </P>  </P>
1151  <P>  <P>
1152  The only metacharacters that are recognized in character classes are backslash,  The only metacharacters that are recognized in character classes are backslash,
# Line 999  this notation. For example, Line 1165  this notation. For example,
1165    [01[:alpha:]%]    [01[:alpha:]%]
1166  </pre>  </pre>
1167  matches "0", "1", any alphabetic character, or "%". The supported class names  matches "0", "1", any alphabetic character, or "%". The supported class names
1168  are  are:
1169  <pre>  <pre>
1170    alnum    letters and digits    alnum    letters and digits
1171    alpha    letters    alpha    letters
# Line 1010  are Line 1176  are
1176    graph    printing characters, excluding space    graph    printing characters, excluding space
1177    lower    lower case letters    lower    lower case letters
1178    print    printing characters, including space    print    printing characters, including space
1179    punct    printing characters, excluding letters and digits    punct    printing characters, excluding letters and digits and space
1180    space    white space (not quite the same as \s)    space    white space (not quite the same as \s)
1181    upper    upper case letters    upper    upper case letters
1182    word     "word" characters (same as \w)    word     "word" characters (same as \w)
# Line 1033  syntax [.ch.] and [=ch=] where "ch" is a Line 1199  syntax [.ch.] and [=ch=] where "ch" is a
1199  supported, and an error is given if they are encountered.  supported, and an error is given if they are encountered.
1200  </P>  </P>
1201  <P>  <P>
1202  In UTF-8 mode, characters with values greater than 128 do not match any of  By default, in UTF modes, characters with values greater than 128 do not match
1203  the POSIX character classes.  any of the POSIX character classes. However, if the PCRE_UCP option is passed
1204    to <b>pcre_compile()</b>, some of the classes are changed so that Unicode
1205    character properties are used. This is achieved by replacing the POSIX classes
1206    by other sequences, as follows:
1207    <pre>
1208      [:alnum:]  becomes  \p{Xan}
1209      [:alpha:]  becomes  \p{L}
1210      [:blank:]  becomes  \h
1211      [:digit:]  becomes  \p{Nd}
1212      [:lower:]  becomes  \p{Ll}
1213      [:space:]  becomes  \p{Xps}
1214      [:upper:]  becomes  \p{Lu}
1215      [:word:]   becomes  \p{Xwd}
1216    </pre>
1217    Negated versions, such as [:^alpha:] use \P instead of \p. The other POSIX
1218    classes are unchanged, and match only characters with code points less than
1219    128.
1220  </P>  </P>
1221  <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC10" href="#TOC1">VERTICAL BAR</a><br>
1222  <P>  <P>
# Line 1084  extracted by the pcre_fullinfo() Line 1266  extracted by the pcre_fullinfo()
1266  </P>  </P>
1267  <P>  <P>
1268  An option change within a subpattern (see below for a description of  An option change within a subpattern (see below for a description of
1269  subpatterns) affects only that part of the current pattern that follows it, so  subpatterns) affects only that part of the subpattern that follows it, so
1270  <pre>  <pre>
1271    (a(?i)b)c    (a(?i)b)c
1272  </pre>  </pre>
# Line 1102  behaviour otherwise. Line 1284  behaviour otherwise.
1284  </P>  </P>
1285  <P>  <P>
1286  <b>Note:</b> There are other PCRE-specific options that can be set by the  <b>Note:</b> There are other PCRE-specific options that can be set by the
1287  application when the compile or match functions are called. In some cases the  application when the compiling or matching functions are called. In some cases
1288  pattern can contain special leading sequences such as (*CRLF) to override what  the pattern can contain special leading sequences such as (*CRLF) to override
1289  the application has set or what has been defaulted. Details are given in the  what the application has set or what has been defaulted. Details are given in
1290  section entitled  the section entitled
1291  <a href="#newlineseq">"Newline sequences"</a>  <a href="#newlineseq">"Newline sequences"</a>
1292  above. There is also the (*UTF8) leading sequence that can be used to set UTF-8  above. There are also the (*UTF8), (*UTF16), and (*UCP) leading sequences that
1293  mode; this is equivalent to setting the PCRE_UTF8 option.  can be used to set UTF and Unicode property modes; they are equivalent to
1294    setting the PCRE_UTF8, PCRE_UTF16, and the PCRE_UCP options, respectively.
1295  <a name="subpattern"></a></P>  <a name="subpattern"></a></P>
1296  <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC12" href="#TOC1">SUBPATTERNS</a><br>
1297  <P>  <P>
# Line 1120  Turning part of a pattern into a subpatt Line 1303  Turning part of a pattern into a subpatt
1303  <pre>  <pre>
1304    cat(aract|erpillar|)    cat(aract|erpillar|)
1305  </pre>  </pre>
1306  matches one of the words "cat", "cataract", or "caterpillar". Without the  matches "cataract", "caterpillar", or "cat". Without the parentheses, it would
1307  parentheses, it would match "cataract", "erpillar" or an empty string.  match "cataract", "erpillar" or an empty string.
1308  <br>  <br>
1309  <br>  <br>
1310  2. It sets up the subpattern as a capturing subpattern. This means that, when  2. It sets up the subpattern as a capturing subpattern. This means that, when
1311  the whole pattern matches, that portion of the subject string that matched the  the whole pattern matches, that portion of the subject string that matched the
1312  subpattern is passed back to the caller via the <i>ovector</i> argument of  subpattern is passed back to the caller via the <i>ovector</i> argument of the
1313  <b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting  matching function. (This applies only to the traditional matching functions;
1314  from 1) to obtain numbers for the capturing subpatterns.  the DFA matching functions do not support capturing.)
1315  </P>  </P>
1316  <P>  <P>
1317  For example, if the string "the red king" is matched against the pattern  Opening parentheses are counted from left to right (starting from 1) to obtain
1318    numbers for the capturing subpatterns. For example, if the string "the red
1319    king" is matched against the pattern
1320  <pre>  <pre>
1321    the ((red|white) (king|queen))    the ((red|white) (king|queen))
1322  </pre>  </pre>
# Line 1179  at captured substring number one, whiche Line 1364  at captured substring number one, whiche
1364  is useful when you want to capture part, but not all, of one of a number of  is useful when you want to capture part, but not all, of one of a number of
1365  alternatives. Inside a (?| group, parentheses are numbered as usual, but the  alternatives. Inside a (?| group, parentheses are numbered as usual, but the
1366  number is reset at the start of each branch. The numbers of any capturing  number is reset at the start of each branch. The numbers of any capturing
1367  buffers that follow the subpattern start after the highest number used in any  parentheses that follow the subpattern start after the highest number used in
1368  branch. The following example is taken from the Perl documentation.  any branch. The following example is taken from the Perl documentation. The
1369  The numbers underneath show in which buffer the captured content will be  numbers underneath show in which buffer the captured content will be stored.
 stored.  
1370  <pre>  <pre>
1371    # before  ---------------branch-reset----------- after    # before  ---------------branch-reset----------- after
1372    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
# Line 1194  or "defdef": Line 1378  or "defdef":
1378  <pre>  <pre>
1379    /(?|(abc)|(def))\1/    /(?|(abc)|(def))\1/
1380  </pre>  </pre>
1381  In contrast, a recursive or "subroutine" call to a numbered subpattern always  In contrast, a subroutine call to a numbered subpattern always refers to the
1382  refers to the first one in the pattern with the given number. The following  first one in the pattern with the given number. The following pattern matches
1383  pattern matches "abcabc" or "defabc":  "abcabc" or "defabc":
1384  <pre>  <pre>
1385    /(?|(abc)|(def))(?1)/    /(?|(abc)|(def))(?1)/
1386  </pre>  </pre>
# Line 1292  items: Line 1476  items:
1476    a literal data character    a literal data character
1477    the dot metacharacter    the dot metacharacter
1478    the \C escape sequence    the \C escape sequence
1479    the \X escape sequence (in UTF-8 mode with Unicode properties)    the \X escape sequence
1480    the \R escape sequence    the \R escape sequence
1481    an escape such as \d that matches a single character    an escape such as \d or \pL that matches a single character
1482    a character class    a character class
1483    a back reference (see next section)    a back reference (see next section)
1484    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (including assertions)
1485    a recursive or "subroutine" call to a subpattern    a subroutine call to a subpattern (recursive or otherwise)
1486  </pre>  </pre>
1487  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1488  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
# Line 1324  quantifier, is taken as a literal charac Line 1508  quantifier, is taken as a literal charac
1508  quantifier, but a literal string of four characters.  quantifier, but a literal string of four characters.
1509  </P>  </P>
1510  <P>  <P>
1511  In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual  In UTF modes, quantifiers apply to characters rather than to individual data
1512  bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of  units. Thus, for example, \x{100}{2} matches two characters, each of
1513  which is represented by a two-byte sequence. Similarly, when Unicode property  which is represented by a two-byte sequence in a UTF-8 string. Similarly,
1514  support is available, \X{3} matches three Unicode extended sequences, each of  \X{3} matches three Unicode extended sequences, each of which may be several
1515  which may be several bytes long (and they may be of different lengths).  data units long (and they may be of different lengths).
1516  </P>  </P>
1517  <P>  <P>
1518  The quantifier {0} is permitted, causing the expression to behave as if the  The quantifier {0} is permitted, causing the expression to behave as if the
1519  previous item and the quantifier were not present. This may be useful for  previous item and the quantifier were not present. This may be useful for
1520  subpatterns that are referenced as  subpatterns that are referenced as
1521  <a href="#subpatternsassubroutines">subroutines</a>  <a href="#subpatternsassubroutines">subroutines</a>
1522  from elsewhere in the pattern. Items other than subpatterns that have a {0}  from elsewhere in the pattern (but see also the section entitled
1523  quantifier are omitted from the compiled pattern.  <a href="#subdefine">"Defining subpatterns for use by reference only"</a>
1524    below). Items other than subpatterns that have a {0} quantifier are omitted
1525    from the compiled pattern.
1526  </P>  </P>
1527  <P>  <P>
1528  For convenience, the three most common quantifiers have single-character  For convenience, the three most common quantifiers have single-character
# Line 1572  subpattern is possible using named paren Line 1758  subpattern is possible using named paren
1758  </P>  </P>
1759  <P>  <P>
1760  Another way of avoiding the ambiguity inherent in the use of digits following a  Another way of avoiding the ambiguity inherent in the use of digits following a
1761  backslash is to use the \g escape sequence, which is a feature introduced in  backslash is to use the \g escape sequence. This escape must be followed by an
1762  Perl 5.10. This escape must be followed by an unsigned number or a negative  unsigned number or a negative number, optionally enclosed in braces. These
1763  number, optionally enclosed in braces. These examples are all identical:  examples are all identical:
1764  <pre>  <pre>
1765    (ring), \1    (ring), \1
1766    (ring), \g1    (ring), \g1
# Line 1588  example: Line 1774  example:
1774    (abc(def)ghi)\g{-1}    (abc(def)ghi)\g{-1}
1775  </pre>  </pre>
1776  The sequence \g{-1} is a reference to the most recently started capturing  The sequence \g{-1} is a reference to the most recently started capturing
1777  subpattern before \g, that is, is it equivalent to \2. Similarly, \g{-2}  subpattern before \g, that is, is it equivalent to \2 in this example.
1778  would be equivalent to \1. The use of relative references can be helpful in  Similarly, \g{-2} would be equivalent to \1. The use of relative references
1779  long patterns, and also in patterns that are created by joining together  can be helpful in long patterns, and also in patterns that are created by
1780  fragments that contain references within themselves.  joining together fragments that contain references within themselves.
1781  </P>  </P>
1782  <P>  <P>
1783  A back reference matches whatever actually matched the capturing subpattern in  A back reference matches whatever actually matched the capturing subpattern in
# Line 1643  Because there may be many capturing pare Line 1829  Because there may be many capturing pare
1829  following a backslash are taken as part of a potential back reference number.  following a backslash are taken as part of a potential back reference number.
1830  If the pattern continues with a digit character, some delimiter must be used to  If the pattern continues with a digit character, some delimiter must be used to
1831  terminate the back reference. If the PCRE_EXTENDED option is set, this can be  terminate the back reference. If the PCRE_EXTENDED option is set, this can be
1832  whitespace. Otherwise, the \g{ syntax or an empty comment (see  white space. Otherwise, the \g{ syntax or an empty comment (see
1833  <a href="#comments">"Comments"</a>  <a href="#comments">"Comments"</a>
1834  below) can be used.  below) can be used.
1835  </P>  </P>
# Line 1686  that look behind it. An assertion subpat Line 1872  that look behind it. An assertion subpat
1872  except that it does not cause the current matching position to be changed.  except that it does not cause the current matching position to be changed.
1873  </P>  </P>
1874  <P>  <P>
1875  Assertion subpatterns are not capturing subpatterns, and may not be repeated,  Assertion subpatterns are not capturing subpatterns. If such an assertion
1876  because it makes no sense to assert the same thing several times. If any kind  contains capturing subpatterns within it, these are counted for the purposes of
1877  of assertion contains capturing subpatterns within it, these are counted for  numbering the capturing subpatterns in the whole pattern. However, substring
1878  the purposes of numbering the capturing subpatterns in the whole pattern.  capturing is carried out only for positive assertions, because it does not make
1879  However, substring capturing is carried out only for positive assertions,  sense for negative assertions.
1880  because it does not make sense for negative assertions.  </P>
1881    <P>
1882    For compatibility with Perl, assertion subpatterns may be repeated; though
1883    it makes no sense to assert the same thing several times, the side effect of
1884    capturing parentheses may occasionally be useful. In practice, there only three
1885    cases:
1886    <br>
1887    <br>
1888    (1) If the quantifier is {0}, the assertion is never obeyed during matching.
1889    However, it may contain internal capturing parenthesized groups that are called
1890    from elsewhere via the
1891    <a href="#subpatternsassubroutines">subroutine mechanism.</a>
1892    <br>
1893    <br>
1894    (2) If quantifier is {0,n} where n is greater than zero, it is treated as if it
1895    were {0,1}. At run time, the rest of the pattern match is tried with and
1896    without the assertion, the order depending on the greediness of the quantifier.
1897    <br>
1898    <br>
1899    (3) If the minimum repetition is greater than zero, the quantifier is ignored.
1900    The assertion is obeyed just once when encountered during matching.
1901  </P>  </P>
1902  <br><b>  <br><b>
1903  Lookahead assertions  Lookahead assertions
# Line 1721  lookbehind assertion is needed to achiev Line 1927  lookbehind assertion is needed to achiev
1927  If you want to force a matching failure at some point in a pattern, the most  If you want to force a matching failure at some point in a pattern, the most
1928  convenient way to do it is with (?!) because an empty string always matches, so  convenient way to do it is with (?!) because an empty string always matches, so
1929  an assertion that requires there not to be an empty string must always fail.  an assertion that requires there not to be an empty string must always fail.
1930  The Perl 5.10 backtracking control verb (*FAIL) or (*F) is essentially a  The backtracking control verb (*FAIL) or (*F) is a synonym for (?!).
 synonym for (?!).  
1931  <a name="lookbehind"></a></P>  <a name="lookbehind"></a></P>
1932  <br><b>  <br><b>
1933  Lookbehind assertions  Lookbehind assertions
# Line 1746  is permitted, but Line 1951  is permitted, but
1951  </pre>  </pre>
1952  causes an error at compile time. Branches that match different length strings  causes an error at compile time. Branches that match different length strings
1953  are permitted only at the top level of a lookbehind assertion. This is an  are permitted only at the top level of a lookbehind assertion. This is an
1954  extension compared with Perl (5.8 and 5.10), which requires all branches to  extension compared with Perl, which requires all branches to match the same
1955  match the same length of string. An assertion such as  length of string. An assertion such as
1956  <pre>  <pre>
1957    (?&#60;=ab(c|de))    (?&#60;=ab(c|de))
1958  </pre>  </pre>
# Line 1757  branches: Line 1962  branches:
1962  <pre>  <pre>
1963    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1964  </pre>  </pre>
1965  In some cases, the Perl 5.10 escape sequence \K  In some cases, the escape sequence \K
1966  <a href="#resetmatchstart">(see above)</a>  <a href="#resetmatchstart">(see above)</a>
1967  can be used instead of a lookbehind assertion to get round the fixed-length  can be used instead of a lookbehind assertion to get round the fixed-length
1968  restriction.  restriction.
# Line 1769  match. If there are insufficient charact Line 1974  match. If there are insufficient charact
1974  assertion fails.  assertion fails.
1975  </P>  </P>
1976  <P>  <P>
1977  PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)  In a UTF mode, PCRE does not allow the \C escape (which matches a single data
1978  to appear in lookbehind assertions, because it makes it impossible to calculate  unit even in a UTF mode) to appear in lookbehind assertions, because it makes
1979  the length of the lookbehind. The \X and \R escapes, which can match  it impossible to calculate the length of the lookbehind. The \X and \R
1980  different numbers of bytes, are also not permitted.  escapes, which can match different numbers of data units, are also not
1981    permitted.
1982  </P>  </P>
1983  <P>  <P>
1984  <a href="#subpatternsassubroutines">"Subroutine"</a>  <a href="#subpatternsassubroutines">"Subroutine"</a>
# Line 1854  already been matched. The two possible f Line 2060  already been matched. The two possible f
2060  </pre>  </pre>
2061  If the condition is satisfied, the yes-pattern is used; otherwise the  If the condition is satisfied, the yes-pattern is used; otherwise the
2062  no-pattern (if present) is used. If there are more than two alternatives in the  no-pattern (if present) is used. If there are more than two alternatives in the
2063  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs. Each of the two alternatives may
2064    itself contain nested subpatterns of any form, including conditional
2065    subpatterns; the restriction to two alternatives applies only at the level of
2066    the condition. This pattern fragment is an example where the alternatives are
2067    complex:
2068    <pre>
2069      (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
2070    
2071    </PRE>
2072  </P>  </P>
2073  <P>  <P>
2074  There are four kinds of condition: references to subpatterns, references to  There are four kinds of condition: references to subpatterns, references to
# Line 1869  condition is true if a capturing subpatt Line 2083  condition is true if a capturing subpatt
2083  matched. If there is more than one capturing subpattern with the same number  matched. If there is more than one capturing subpattern with the same number
2084  (see the earlier  (see the earlier
2085  <a href="#recursion">section about duplicate subpattern numbers),</a>  <a href="#recursion">section about duplicate subpattern numbers),</a>
2086  the condition is true if any of them have been set. An alternative notation is  the condition is true if any of them have matched. An alternative notation is
2087  to precede the digits with a plus or minus sign. In this case, the subpattern  to precede the digits with a plus or minus sign. In this case, the subpattern
2088  number is relative rather than absolute. The most recently opened parentheses  number is relative rather than absolute. The most recently opened parentheses
2089  can be referenced by (?(-1), the next most recent by (?(-2), and so on. In  can be referenced by (?(-1), the next most recent by (?(-2), and so on. Inside
2090  looping constructs it can also make sense to refer to subsequent groups with  loops it can also make sense to refer to subsequent groups. The next
2091  constructs such as (?(+2).  parentheses to be opened can be referenced as (?(+1), and so on. (The value
2092    zero in any of these forms is not used; it provokes a compile-time error.)
2093  </P>  </P>
2094  <P>  <P>
2095  Consider the following pattern, which contains non-significant white space to  Consider the following pattern, which contains non-significant white space to
# Line 1886  three parts for ease of discussion: Line 2101  three parts for ease of discussion:
2101  The first part matches an optional opening parenthesis, and if that  The first part matches an optional opening parenthesis, and if that
2102  character is present, sets it as the first captured substring. The second part  character is present, sets it as the first captured substring. The second part
2103  matches one or more characters that are not parentheses. The third part is a  matches one or more characters that are not parentheses. The third part is a
2104  conditional subpattern that tests whether the first set of parentheses matched  conditional subpattern that tests whether or not the first set of parentheses
2105  or not. If they did, that is, if subject started with an opening parenthesis,  matched. If they did, that is, if subject started with an opening parenthesis,
2106  the condition is true, and so the yes-pattern is executed and a closing  the condition is true, and so the yes-pattern is executed and a closing
2107  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
2108  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
# Line 1944  the most recent recursion. Line 2159  the most recent recursion.
2159  At "top level", all these recursion test conditions are false.  At "top level", all these recursion test conditions are false.
2160  <a href="#recursion">The syntax for recursive patterns</a>  <a href="#recursion">The syntax for recursive patterns</a>
2161  is described below.  is described below.
2162  </P>  <a name="subdefine"></a></P>
2163  <br><b>  <br><b>
2164  Defining subpatterns for use by reference only  Defining subpatterns for use by reference only
2165  </b><br>  </b><br>
# Line 1953  If the condition is the string (DEFINE), Line 2168  If the condition is the string (DEFINE),
2168  name DEFINE, the condition is always false. In this case, there may be only one  name DEFINE, the condition is always false. In this case, there may be only one
2169  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
2170  point in the pattern; the idea of DEFINE is that it can be used to define  point in the pattern; the idea of DEFINE is that it can be used to define
2171  "subroutines" that can be referenced from elsewhere. (The use of  subroutines that can be referenced from elsewhere. (The use of
2172  <a href="#subpatternsassubroutines">"subroutines"</a>  <a href="#subpatternsassubroutines">subroutines</a>
2173  is described below.) For example, a pattern to match an IPv4 address could be  is described below.) For example, a pattern to match an IPv4 address such as
2174  written like this (ignore whitespace and line breaks):  "192.168.23.245" could be written like this (ignore white space and line
2175    breaks):
2176  <pre>  <pre>
2177    (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )    (?(DEFINE) (?&#60;byte&#62; 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )
2178    \b (?&byte) (\.(?&byte)){3} \b    \b (?&byte) (\.(?&byte)){3} \b
# Line 1989  dd-aaa-dd or dd-dd-dd, where aaa are let Line 2205  dd-aaa-dd or dd-dd-dd, where aaa are let
2205  <a name="comments"></a></P>  <a name="comments"></a></P>
2206  <br><a name="SEC20" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC20" href="#TOC1">COMMENTS</a><br>
2207  <P>  <P>
2208  The sequence (?# marks the start of a comment that continues up to the next  There are two ways of including comments in patterns that are processed by
2209  closing parenthesis. Nested parentheses are not permitted. The characters  PCRE. In both cases, the start of the comment must not be in a character class,
2210  that make up a comment play no part in the pattern matching at all.  nor in the middle of any other sequence of related characters such as (?: or a
2211    subpattern name or number. The characters that make up a comment play no part
2212    in the pattern matching.
2213  </P>  </P>
2214  <P>  <P>
2215  If the PCRE_EXTENDED option is set, an unescaped # character outside a  The sequence (?# marks the start of a comment that continues up to the next
2216  character class introduces a comment that continues to immediately after the  closing parenthesis. Nested parentheses are not permitted. If the PCRE_EXTENDED
2217  next newline in the pattern.  option is set, an unescaped # character also introduces a comment, which in
2218    this case continues to immediately after the next newline character or
2219    character sequence in the pattern. Which characters are interpreted as newlines
2220    is controlled by the options passed to a compiling function or by a special
2221    sequence at the start of the pattern, as described in the section entitled
2222    <a href="#newlines">"Newline conventions"</a>
2223    above. Note that the end of this type of comment is a literal newline sequence
2224    in the pattern; escape sequences that happen to represent a newline do not
2225    count. For example, consider this pattern when PCRE_EXTENDED is set, and the
2226    default newline convention is in force:
2227    <pre>
2228      abc #comment \n still comment
2229    </pre>
2230    On encountering the # character, <b>pcre_compile()</b> skips along, looking for
2231    a newline in the pattern. The sequence \n is still literal at this stage, so
2232    it does not terminate the comment. Only an actual character with the code value
2233    0x0a (the default newline) does so.
2234  <a name="recursion"></a></P>  <a name="recursion"></a></P>
2235  <br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC21" href="#TOC1">RECURSIVE PATTERNS</a><br>
2236  <P>  <P>
# Line 2025  this kind of recursion was subsequently Line 2259  this kind of recursion was subsequently
2259  </P>  </P>
2260  <P>  <P>
2261  A special item that consists of (? followed by a number greater than zero and a  A special item that consists of (? followed by a number greater than zero and a
2262  closing parenthesis is a recursive call of the subpattern of the given number,  closing parenthesis is a recursive subroutine call of the subpattern of the
2263  provided that it occurs inside that subpattern. (If not, it is a  given number, provided that it occurs inside that subpattern. (If not, it is a
2264  <a href="#subpatternsassubroutines">"subroutine"</a>  <a href="#subpatternsassubroutines">non-recursive subroutine</a>
2265  call, which is described in the next section.) The special item (?R) or (?0) is  call, which is described in the next section.) The special item (?R) or (?0) is
2266  a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
2267  </P>  </P>
# Line 2054  them instead of the whole pattern. Line 2288  them instead of the whole pattern.
2288  </P>  </P>
2289  <P>  <P>
2290  In a larger pattern, keeping track of parenthesis numbers can be tricky. This  In a larger pattern, keeping track of parenthesis numbers can be tricky. This
2291  is made easier by the use of relative references (a Perl 5.10 feature).  is made easier by the use of relative references. Instead of (?1) in the
2292  Instead of (?1) in the pattern above you can write (?-2) to refer to the second  pattern above you can write (?-2) to refer to the second most recently opened
2293  most recently opened parentheses preceding the recursion. In other words, a  parentheses preceding the recursion. In other words, a negative number counts
2294  negative number counts capturing parentheses leftwards from the point at which  capturing parentheses leftwards from the point at which it is encountered.
 it is encountered.  
2295  </P>  </P>
2296  <P>  <P>
2297  It is also possible to refer to subsequently opened parentheses, by writing  It is also possible to refer to subsequently opened parentheses, by writing
2298  references such as (?+2). However, these cannot be recursive because the  references such as (?+2). However, these cannot be recursive because the
2299  reference is not inside the parentheses that are referenced. They are always  reference is not inside the parentheses that are referenced. They are always
2300  <a href="#subpatternsassubroutines">"subroutine"</a>  <a href="#subpatternsassubroutines">non-recursive subroutine</a>
2301  calls, as described in the next section.  calls, as described in the next section.
2302  </P>  </P>
2303  <P>  <P>
# Line 2101  documentation). If the pattern above is Line 2334  documentation). If the pattern above is
2334  </pre>  </pre>
2335  the value for the inner capturing parentheses (numbered 2) is "ef", which is  the value for the inner capturing parentheses (numbered 2) is "ef", which is
2336  the last value taken on at the top level. If a capturing subpattern is not  the last value taken on at the top level. If a capturing subpattern is not
2337  matched at the top level, its final value is unset, even if it is (temporarily)  matched at the top level, its final captured value is unset, even if it was
2338  set at a deeper level.  (temporarily) set at a deeper level during the matching process.
2339  </P>  </P>
2340  <P>  <P>
2341  If there are more than 15 capturing parentheses in a pattern, PCRE has to  If there are more than 15 capturing parentheses in a pattern, PCRE has to
# Line 2123  different alternatives for the recursive Line 2356  different alternatives for the recursive
2356  is the actual recursive call.  is the actual recursive call.
2357  <a name="recursiondifference"></a></P>  <a name="recursiondifference"></a></P>
2358  <br><b>  <br><b>
2359  Recursion difference from Perl  Differences in recursion processing between PCRE and Perl
2360  </b><br>  </b><br>
2361  <P>  <P>
2362  In PCRE (like Python, but unlike Perl), a recursive subpattern call is always  Recursion processing in PCRE differs from Perl in two important ways. In PCRE
2363  treated as an atomic group. That is, once it has matched some of the subject  (like Python, but unlike Perl), a recursive subpattern call is always treated
2364  string, it is never re-entered, even if it contains untried alternatives and  as an atomic group. That is, once it has matched some of the subject string, it
2365  there is a subsequent matching failure. This can be illustrated by the  is never re-entered, even if it contains untried alternatives and there is a
2366  following pattern, which purports to match a palindromic string that contains  subsequent matching failure. This can be illustrated by the following pattern,
2367  an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):  which purports to match a palindromic string that contains an odd number of
2368    characters (for example, "a", "aba", "abcba", "abcdcba"):
2369  <pre>  <pre>
2370    ^(.|(.)(?1)\2)$    ^(.|(.)(?1)\2)$
2371  </pre>  </pre>
# Line 2164  difference: in the previous case the rem Line 2398  difference: in the previous case the rem
2398  recursion level, which PCRE cannot use.  recursion level, which PCRE cannot use.
2399  </P>  </P>
2400  <P>  <P>
2401  To change the pattern so that matches all palindromic strings, not just those  To change the pattern so that it matches all palindromic strings, not just
2402  with an odd number of characters, it is tempting to change the pattern to this:  those with an odd number of characters, it is tempting to change the pattern to
2403    this:
2404  <pre>  <pre>
2405    ^((.)(?1)\2|.?)$    ^((.)(?1)\2|.?)$
2406  </pre>  </pre>
# Line 2195  For example, although "abcba" is correct Line 2430  For example, although "abcba" is correct
2430  PCRE finds the palindrome "aba" at the start, then fails at top level because  PCRE finds the palindrome "aba" at the start, then fails at top level because
2431  the end of the string does not follow. Once again, it cannot jump back into the  the end of the string does not follow. Once again, it cannot jump back into the
2432  recursion to try other alternatives, so the entire match fails.  recursion to try other alternatives, so the entire match fails.
2433    </P>
2434    <P>
2435    The second way in which PCRE and Perl differ in their recursion processing is
2436    in the handling of captured values. In Perl, when a subpattern is called
2437    recursively or as a subpattern (see the next section), it has no access to any
2438    values that were captured outside the recursion, whereas in PCRE these values
2439    can be referenced. Consider this pattern:
2440    <pre>
2441      ^(.)(\1|a(?2))
2442    </pre>
2443    In PCRE, this pattern matches "bab". The first capturing parentheses match "b",
2444    then in the second group, when the back reference \1 fails to match "b", the
2445    second alternative matches "a" and then recurses. In the recursion, \1 does
2446    now match "b" and so the whole match succeeds. In Perl, the pattern fails to
2447    match because inside the recursive call \1 cannot access the externally set
2448    value.
2449  <a name="subpatternsassubroutines"></a></P>  <a name="subpatternsassubroutines"></a></P>
2450  <br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC22" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
2451  <P>  <P>
2452  If the syntax for a recursive subpattern reference (either by number or by  If the syntax for a recursive subpattern call (either by number or by
2453  name) is used outside the parentheses to which it refers, it operates like a  name) is used outside the parentheses to which it refers, it operates like a
2454  subroutine in a programming language. The "called" subpattern may be defined  subroutine in a programming language. The called subpattern may be defined
2455  before or after the reference. A numbered reference can be absolute or  before or after the reference. A numbered reference can be absolute or
2456  relative, as in these examples:  relative, as in these examples:
2457  <pre>  <pre>
# Line 2221  is used, it does match "sense and respon Line 2472  is used, it does match "sense and respon
2472  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2473  </P>  </P>
2474  <P>  <P>
2475  Like recursive subpatterns, a subroutine call is always treated as an atomic  All subroutine calls, whether recursive or not, are always treated as atomic
2476  group. That is, once it has matched some of the subject string, it is never  groups. That is, once a subroutine has matched some of the subject string, it
2477  re-entered, even if it contains untried alternatives and there is a subsequent  is never re-entered, even if it contains untried alternatives and there is a
2478  matching failure. Any capturing parentheses that are set during the subroutine  subsequent matching failure. Any capturing parentheses that are set during the
2479  call revert to their previous values afterwards.  subroutine call revert to their previous values afterwards.
2480  </P>  </P>
2481  <P>  <P>
2482  When a subpattern is used as a subroutine, processing options such as  Processing options such as case-independence are fixed when a subpattern is
2483  case-independence are fixed when the subpattern is defined. They cannot be  defined, so if it is used as a subroutine, such options cannot be changed for
2484  changed for different calls. For example, consider this pattern:  different calls. For example, consider this pattern:
2485  <pre>  <pre>
2486    (abc)(?i:(?-1))    (abc)(?i:(?-1))
2487  </pre>  </pre>
# Line 2265  same pair of parentheses when there is a Line 2516  same pair of parentheses when there is a
2516  <P>  <P>
2517  PCRE provides a similar feature, but of course it cannot obey arbitrary Perl  PCRE provides a similar feature, but of course it cannot obey arbitrary Perl
2518  code. The feature is called "callout". The caller of PCRE provides an external  code. The feature is called "callout". The caller of PCRE provides an external
2519  function by putting its entry point in the global variable <i>pcre_callout</i>.  function by putting its entry point in the global variable <i>pcre_callout</i>
2520  By default, this variable contains NULL, which disables all calling out.  (8-bit library) or <i>pcre16_callout</i> (16-bit library). By default, this
2521    variable contains NULL, which disables all calling out.
2522  </P>  </P>
2523  <P>  <P>
2524  Within a regular expression, (?C) indicates the points at which the external  Within a regular expression, (?C) indicates the points at which the external
# Line 2276  For example, this pattern has two callou Line 2528  For example, this pattern has two callou
2528  <pre>  <pre>
2529    (?C1)abc(?C2)def    (?C1)abc(?C2)def
2530  </pre>  </pre>
2531  If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are  If the PCRE_AUTO_CALLOUT flag is passed to a compiling function, callouts are
2532  automatically installed before each item in the pattern. They are all numbered  automatically installed before each item in the pattern. They are all numbered
2533  255.  255.
2534  </P>  </P>
2535  <P>  <P>
2536  During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is  During matching, when PCRE reaches a callout point, the external function is
2537  set), the external function is called. It is provided with the number of the  called. It is provided with the number of the callout, the position in the
2538  callout, the position in the pattern, and, optionally, one item of data  pattern, and, optionally, one item of data originally supplied by the caller of
2539  originally supplied by the caller of <b>pcre_exec()</b>. The callout function  the matching function. The callout function may cause matching to proceed, to
2540  may cause matching to proceed, to backtrack, or to fail altogether. A complete  backtrack, or to fail altogether. A complete description of the interface to
2541  description of the interface to the callout function is given in the  the callout function is given in the
2542  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
2543  documentation.  documentation.
2544  </P>  <a name="backtrackcontrol"></a></P>
2545  <br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br>  <br><a name="SEC25" href="#TOC1">BACKTRACKING CONTROL</a><br>
2546  <P>  <P>
2547  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which  Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
# Line 2300  remarks apply to the PCRE features descr Line 2552  remarks apply to the PCRE features descr
2552  </P>  </P>
2553  <P>  <P>
2554  Since these verbs are specifically related to backtracking, most of them can be  Since these verbs are specifically related to backtracking, most of them can be
2555  used only when the pattern is to be matched using <b>pcre_exec()</b>, which uses  used only when the pattern is to be matched using one of the traditional
2556  a backtracking algorithm. With the exception of (*FAIL), which behaves like a  matching functions, which use a backtracking algorithm. With the exception of
2557  failing negative assertion, they cause an error if encountered by  (*FAIL), which behaves like a failing negative assertion, they cause an error
2558  <b>pcre_dfa_exec()</b>.  if encountered by a DFA matching function.
2559  </P>  </P>
2560  <P>  <P>
2561  If any of these verbs are used in an assertion or subroutine subpattern  If any of these verbs are used in an assertion or in a subpattern that is
2562  (including recursive subpatterns), their effect is confined to that subpattern;  called as a subroutine (whether or not recursively), their effect is confined
2563  it does not extend to the surrounding pattern. Note that such subpatterns are  to that subpattern; it does not extend to the surrounding pattern, with one
2564  processed as anchored at the point where they are tested.  exception: the name from a *(MARK), (*PRUNE), or (*THEN) that is encountered in
2565    a successful positive assertion <i>is</i> passed back when a match succeeds
2566    (compare capturing parentheses in assertions). Note that such subpatterns are
2567    processed as anchored at the point where they are tested. Note also that Perl's
2568    treatment of subroutines and assertions is different in some cases.
2569  </P>  </P>
2570  <P>  <P>
2571  The new verbs make use of what was previously invalid syntax: an opening  The new verbs make use of what was previously invalid syntax: an opening
2572  parenthesis followed by an asterisk. In Perl, they are generally of the form  parenthesis followed by an asterisk. They are generally of the form
2573  (*VERB:ARG) but PCRE does not support the use of arguments, so its general  (*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour,
2574  form is just (*VERB). Any number of these verbs may occur in a pattern. There  depending on whether or not an argument is present. A name is any sequence of
2575  are two kinds:  characters that does not include a closing parenthesis. The maximum length of
2576    name is 255 in the 8-bit library and 65535 in the 16-bit library. If the name
2577    is empty, that is, if the closing parenthesis immediately follows the colon,
2578    the effect is as if the colon were not there. Any number of these verbs may
2579    occur in a pattern.
2580    <a name="nooptimize"></a></P>
2581    <br><b>
2582    Optimizations that affect backtracking verbs
2583    </b><br>
2584    <P>
2585    PCRE contains some optimizations that are used to speed up matching by running
2586    some checks at the start of each match attempt. For example, it may know the
2587    minimum length of matching subject, or that a particular character must be
2588    present. When one of these optimizations suppresses the running of a match, any
2589    included backtracking verbs will not, of course, be processed. You can suppress
2590    the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2591    when calling <b>pcre_compile()</b> or <b>pcre_exec()</b>, or by starting the
2592    pattern with (*NO_START_OPT). There is more discussion of this option in the
2593    section entitled
2594    <a href="pcreapi.html#execoptions">"Option bits for <b>pcre_exec()</b>"</a>
2595    in the
2596    <a href="pcreapi.html"><b>pcreapi</b></a>
2597    documentation.
2598    </P>
2599    <P>
2600    Experiments with Perl suggest that it too has similar optimizations, sometimes
2601    leading to anomalous results.
2602  </P>  </P>
2603  <br><b>  <br><b>
2604  Verbs that act immediately  Verbs that act immediately
2605  </b><br>  </b><br>
2606  <P>  <P>
2607  The following verbs act as soon as they are encountered:  The following verbs act as soon as they are encountered. They may not be
2608    followed by a name.
2609  <pre>  <pre>
2610     (*ACCEPT)     (*ACCEPT)
2611  </pre>  </pre>
2612  This verb causes the match to end successfully, skipping the remainder of the  This verb causes the match to end successfully, skipping the remainder of the
2613  pattern. When inside a recursion, only the innermost pattern is ended  pattern. However, when it is inside a subpattern that is called as a
2614  immediately. If (*ACCEPT) is inside capturing parentheses, the data so far is  subroutine, only that subpattern is ended successfully. Matching then continues
2615  captured. (This feature was added to PCRE at release 8.00.) For example:  at the outer level. If (*ACCEPT) is inside capturing parentheses, the data so
2616    far is captured. For example:
2617  <pre>  <pre>
2618    A((?:A|B(*ACCEPT)|C)D)    A((?:A|B(*ACCEPT)|C)D)
2619  </pre>  </pre>
# Line 2338  the outer parentheses. Line 2622  the outer parentheses.
2622  <pre>  <pre>
2623    (*FAIL) or (*F)    (*FAIL) or (*F)
2624  </pre>  </pre>
2625  This verb causes the match to fail, forcing backtracking to occur. It is  This verb causes a matching failure, forcing backtracking to occur. It is
2626  equivalent to (?!) but easier to read. The Perl documentation notes that it is  equivalent to (?!) but easier to read. The Perl documentation notes that it is
2627  probably useful only when combined with (?{}) or (??{}). Those are, of course,  probably useful only when combined with (?{}) or (??{}). Those are, of course,
2628  Perl features that are not present in PCRE. The nearest equivalent is the  Perl features that are not present in PCRE. The nearest equivalent is the
# Line 2350  A match with the string "aaaa" always fa Line 2634  A match with the string "aaaa" always fa
2634  each backtrack happens (in this example, 10 times).  each backtrack happens (in this example, 10 times).
2635  </P>  </P>
2636  <br><b>  <br><b>
2637    Recording which path was taken
2638    </b><br>
2639    <P>
2640    There is one verb whose main purpose is to track how a match was arrived at,
2641    though it also has a secondary use in conjunction with advancing the match
2642    starting point (see (*SKIP) below).
2643    <pre>
2644      (*MARK:NAME) or (*:NAME)
2645    </pre>
2646    A name is always required with this verb. There may be as many instances of
2647    (*MARK) as you like in a pattern, and their names do not have to be unique.
2648    </P>
2649    <P>
2650    When a match succeeds, the name of the last-encountered (*MARK) on the matching
2651    path is passed back to the caller as described in the section entitled
2652    <a href="pcreapi.html#extradata">"Extra data for <b>pcre_exec()</b>"</a>
2653    in the
2654    <a href="pcreapi.html"><b>pcreapi</b></a>
2655    documentation. Here is an example of <b>pcretest</b> output, where the /K
2656    modifier requests the retrieval and outputting of (*MARK) data:
2657    <pre>
2658        re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/K
2659      data&#62; XY
2660       0: XY
2661      MK: A
2662      XZ
2663       0: XZ
2664      MK: B
2665    </pre>
2666    The (*MARK) name is tagged with "MK:" in this output, and in this example it
2667    indicates which of the two alternatives matched. This is a more efficient way
2668    of obtaining this information than putting each alternative in its own
2669    capturing parentheses.
2670    </P>
2671    <P>
2672    If (*MARK) is encountered in a positive assertion, its name is recorded and
2673    passed back if it is the last-encountered. This does not happen for negative
2674    assertions.
2675    </P>
2676    <P>
2677    After a partial match or a failed match, the name of the last encountered
2678    (*MARK) in the entire match process is returned. For example:
2679    <pre>
2680        re&#62; /X(*MARK:A)Y|X(*MARK:B)Z/K
2681      data&#62; XP
2682      No match, mark = B
2683    </pre>
2684    Note that in this unanchored example the mark is retained from the match
2685    attempt that started at the letter "X" in the subject. Subsequent match
2686    attempts starting at "P" and then with an empty string do not get as far as the
2687    (*MARK) item, but nevertheless do not reset it.
2688    </P>
2689    <P>
2690    If you are interested in (*MARK) values after failed matches, you should
2691    probably set the PCRE_NO_START_OPTIMIZE option
2692    <a href="#nooptimize">(see above)</a>
2693    to ensure that the match is always attempted.
2694    </P>
2695    <br><b>
2696  Verbs that act after backtracking  Verbs that act after backtracking
2697  </b><br>  </b><br>
2698  <P>  <P>
2699  The following verbs do nothing when they are encountered. Matching continues  The following verbs do nothing when they are encountered. Matching continues
2700  with what follows, but if there is no subsequent match, a failure is forced.  with what follows, but if there is no subsequent match, causing a backtrack to
2701  The verbs differ in exactly what kind of failure occurs.  the verb, a failure is forced. That is, backtracking cannot pass to the left of
2702    the verb. However, when one of these verbs appears inside an atomic group, its
2703    effect is confined to that group, because once the group has been matched,
2704    there is never any backtracking into it. In this situation, backtracking can
2705    "jump back" to the left of the entire atomic group. (Remember also, as stated
2706    above, that this localization also applies in subroutine calls and assertions.)
2707    </P>
2708    <P>
2709    These verbs differ in exactly what kind of failure occurs when backtracking
2710    reaches them.
2711  <pre>  <pre>
2712    (*COMMIT)    (*COMMIT)
2713  </pre>  </pre>
2714  This verb causes the whole match to fail outright if the rest of the pattern  This verb, which may not be followed by a name, causes the whole match to fail
2715  does not match. Even if the pattern is unanchored, no further attempts to find  outright if the rest of the pattern does not match. Even if the pattern is
2716  a match by advancing the starting point take place. Once (*COMMIT) has been  unanchored, no further attempts to find a match by advancing the starting point
2717  passed, <b>pcre_exec()</b> is committed to finding a match at the current  take place. Once (*COMMIT) has been passed, <b>pcre_exec()</b> is committed to
2718  starting point, or not at all. For example:  finding a match at the current starting point, or not at all. For example:
2719  <pre>  <pre>
2720    a+(*COMMIT)b    a+(*COMMIT)b
2721  </pre>  </pre>
2722  This matches "xxaab" but not "aacaab". It can be thought of as a kind of  This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2723  dynamic anchor, or "I've started, so I must finish."  dynamic anchor, or "I've started, so I must finish." The name of the most
2724  <pre>  recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
2725    (*PRUNE)  match failure.
2726  </pre>  </P>
2727  This verb causes the match to fail at the current position if the rest of the  <P>
2728  pattern does not match. If the pattern is unanchored, the normal "bumpalong"  Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
2729  advance to the next starting character then happens. Backtracking can occur as  unless PCRE's start-of-match optimizations are turned off, as shown in this
2730  usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but  <b>pcretest</b> example:
2731  if there is no match to the right, backtracking cannot cross (*PRUNE).  <pre>
2732  In simple cases, the use of (*PRUNE) is just an alternative to an atomic      re&#62; /(*COMMIT)abc/
2733  group or possessive quantifier, but there are some uses of (*PRUNE) that cannot    data&#62; xyzabc
2734  be expressed in any other way.     0: abc
2735      xyzabc\Y
2736      No match
2737    </pre>
2738    PCRE knows that any match must start with "a", so the optimization skips along
2739    the subject to "a" before running the first match attempt, which succeeds. When
2740    the optimization is disabled by the \Y escape in the second subject, the match
2741    starts at "x" and so the (*COMMIT) causes it to fail without trying any other
2742    starting points.
2743    <pre>
2744      (*PRUNE) or (*PRUNE:NAME)
2745    </pre>
2746    This verb causes the match to fail at the current starting position in the
2747    subject if the rest of the pattern does not match. If the pattern is
2748    unanchored, the normal "bumpalong" advance to the next starting character then
2749    happens. Backtracking can occur as usual to the left of (*PRUNE), before it is
2750    reached, or when matching to the right of (*PRUNE), but if there is no match to
2751    the right, backtracking cannot cross (*PRUNE). In simple cases, the use of
2752    (*PRUNE) is just an alternative to an atomic group or possessive quantifier,
2753    but there are some uses of (*PRUNE) that cannot be expressed in any other way.
2754    The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE). In an
2755    anchored pattern (*PRUNE) has the same effect as (*COMMIT).
2756  <pre>  <pre>
2757    (*SKIP)    (*SKIP)
2758  </pre>  </pre>
2759  This verb is like (*PRUNE), except that if the pattern is unanchored, the  This verb, when given without a name, is like (*PRUNE), except that if the
2760  "bumpalong" advance is not to the next character, but to the position in the  pattern is unanchored, the "bumpalong" advance is not to the next character,
2761  subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text  but to the position in the subject where (*SKIP) was encountered. (*SKIP)
2762  was matched leading up to it cannot be part of a successful match. Consider:  signifies that whatever text was matched leading up to it cannot be part of a
2763    successful match. Consider:
2764  <pre>  <pre>
2765    a+(*SKIP)b    a+(*SKIP)b
2766  </pre>  </pre>
# Line 2397  effect as this example; although it woul Line 2771  effect as this example; although it woul
2771  first match attempt, the second attempt would start at the second character  first match attempt, the second attempt would start at the second character
2772  instead of skipping on to "c".  instead of skipping on to "c".
2773  <pre>  <pre>
2774    (*THEN)    (*SKIP:NAME)
2775  </pre>  </pre>
2776  This verb causes a skip to the next alternation if the rest of the pattern does  When (*SKIP) has an associated name, its behaviour is modified. If the
2777  not match. That is, it cancels pending backtracking, but only within the  following pattern fails to match, the previous path through the pattern is
2778  current alternation. Its name comes from the observation that it can be used  searched for the most recent (*MARK) that has the same name. If one is found,
2779  for a pattern-based if-then-else block:  the "bumpalong" advance is to the subject position that corresponds to that
2780    (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a
2781    matching name is found, the (*SKIP) is ignored.
2782    <pre>
2783      (*THEN) or (*THEN:NAME)
2784    </pre>
2785    This verb causes a skip to the next innermost alternative if the rest of the
2786    pattern does not match. That is, it cancels pending backtracking, but only
2787    within the current alternative. Its name comes from the observation that it can
2788    be used for a pattern-based if-then-else block:
2789  <pre>  <pre>
2790    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2791  </pre>  </pre>
2792  If the COND1 pattern matches, FOO is tried (and possibly further items after  If the COND1 pattern matches, FOO is tried (and possibly further items after
2793  the end of the group if FOO succeeds); on failure the matcher skips to the  the end of the group if FOO succeeds); on failure, the matcher skips to the
2794  second alternative and tries COND2, without backtracking into COND1. If (*THEN)  second alternative and tries COND2, without backtracking into COND1. The
2795  is used outside of any alternation, it acts exactly like (*PRUNE).  behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN).
2796    If (*THEN) is not inside an alternation, it acts like (*PRUNE).
2797    </P>
2798    <P>
2799    Note that a subpattern that does not contain a | character is just a part of
2800    the enclosing alternative; it is not a nested alternation with only one
2801    alternative. The effect of (*THEN) extends beyond such a subpattern to the
2802    enclosing alternative. Consider this pattern, where A, B, etc. are complex
2803    pattern fragments that do not contain any | characters at this level:
2804    <pre>
2805      A (B(*THEN)C) | D
2806    </pre>
2807    If A and B are matched, but there is a failure in C, matching does not
2808    backtrack into A; instead it moves to the next alternative, that is, D.
2809    However, if the subpattern containing (*THEN) is given an alternative, it
2810    behaves differently:
2811    <pre>
2812      A (B(*THEN)C | (*FAIL)) | D
2813    </pre>
2814    The effect of (*THEN) is now confined to the inner subpattern. After a failure
2815    in C, matching moves to (*FAIL), which causes the whole subpattern to fail
2816    because there are no more alternatives to try. In this case, matching does now
2817    backtrack into A.
2818    </P>
2819    <P>
2820    Note also that a conditional subpattern is not considered as having two
2821    alternatives, because only one is ever used. In other words, the | character in
2822    a conditional subpattern has a different meaning. Ignoring white space,
2823    consider:
2824    <pre>
2825      ^.*? (?(?=a) a | b(*THEN)c )
2826    </pre>
2827    If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
2828    it initially matches zero characters. The condition (?=a) then fails, the
2829    character "b" is matched, but "c" is not. At this point, matching does not
2830    backtrack to .*? as might perhaps be expected from the presence of the |
2831    character. The conditional subpattern is part of the single alternative that
2832    comprises the whole pattern, and so the match fails. (If there was a backtrack
2833    into .*?, allowing it to match "b", the match would succeed.)
2834    </P>
2835    <P>
2836    The verbs just described provide four different "strengths" of control when
2837    subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
2838    next alternative. (*PRUNE) comes next, failing the match at the current
2839    starting position, but allowing an advance to the next character (for an
2840    unanchored pattern). (*SKIP) is similar, except that the advance may be more
2841    than one character. (*COMMIT) is the strongest, causing the entire match to
2842    fail.
2843    </P>
2844    <P>
2845    If more than one such verb is present in a pattern, the "strongest" one wins.
2846    For example, consider this pattern, where A, B, etc. are complex pattern
2847    fragments:
2848    <pre>
2849      (A(*COMMIT)B(*THEN)C|D)
2850    </pre>
2851    Once A has matched, PCRE is committed to this match, at the current starting
2852    position. If subsequently B matches, but C does not, the normal (*THEN) action
2853    of trying the next alternative (that is, D) does not happen because (*COMMIT)
2854    overrides.
2855  </P>  </P>
2856  <br><a name="SEC26" href="#TOC1">SEE ALSO</a><br>  <br><a name="SEC26" href="#TOC1">SEE ALSO</a><br>
2857  <P>  <P>
2858  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3),  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3),
2859  <b>pcresyntax</b>(3), <b>pcre</b>(3).  <b>pcresyntax</b>(3), <b>pcre</b>(3), <b>pcre16(3)</b>.
2860  </P>  </P>
2861  <br><a name="SEC27" href="#TOC1">AUTHOR</a><br>  <br><a name="SEC27" href="#TOC1">AUTHOR</a><br>
2862  <P>  <P>
# Line 2427  Cambridge CB2 3QH, England. Line 2869  Cambridge CB2 3QH, England.
2869  </P>  </P>
2870  <br><a name="SEC28" href="#TOC1">REVISION</a><br>  <br><a name="SEC28" href="#TOC1">REVISION</a><br>
2871  <P>  <P>
2872  Last updated: 06 March 2010  Last updated: 01 June 2012
2873  <br>  <br>
2874  Copyright &copy; 1997-2010 University of Cambridge.  Copyright &copy; 1997-2012 University of Cambridge.
2875  <br>  <br>
2876  <p>  <p>
2877  Return to the <a href="index.html">PCRE index page</a>.  Return to the <a href="index.html">PCRE index page</a>.

Legend:
Removed from v.507  
changed lines
  Added in v.975

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12