/[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 73 by nigel, Sat Feb 24 21:40:30 2007 UTC revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC
# Line 3  Line 3 
3  <title>pcrepattern specification</title>  <title>pcrepattern specification</title>
4  </head>  </head>
5  <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">  <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6  This HTML document has been generated automatically from the original man page.  <h1>pcrepattern man page</h1>
7  If there is any nonsense in it, please consult the man page, in case the  <p>
8  conversion went wrong.<br>  Return to the <a href="index.html">PCRE index page</a>.
9    </p>
10    <p>
11    This page is part of the PCRE HTML documentation. It was generated automatically
12    from the original man page. If there is any nonsense in it, please consult the
13    man page, in case the conversion went wrong.
14    <br>
15  <ul>  <ul>
16  <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>  <li><a name="TOC1" href="#SEC1">PCRE REGULAR EXPRESSION DETAILS</a>
17  <li><a name="TOC2" href="#SEC2">BACKSLASH</a>  <li><a name="TOC2" href="#SEC2">BACKSLASH</a>
18  <li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a>  <li><a name="TOC3" href="#SEC3">CIRCUMFLEX AND DOLLAR</a>
19  <li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a>  <li><a name="TOC4" href="#SEC4">FULL STOP (PERIOD, DOT)</a>
20  <li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a>  <li><a name="TOC5" href="#SEC5">MATCHING A SINGLE BYTE</a>
21  <li><a name="TOC6" href="#SEC6">SQUARE BRACKETS</a>  <li><a name="TOC6" href="#SEC6">SQUARE BRACKETS AND CHARACTER CLASSES</a>
22  <li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a>  <li><a name="TOC7" href="#SEC7">POSIX CHARACTER CLASSES</a>
23  <li><a name="TOC8" href="#SEC8">VERTICAL BAR</a>  <li><a name="TOC8" href="#SEC8">VERTICAL BAR</a>
24  <li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a>  <li><a name="TOC9" href="#SEC9">INTERNAL OPTION SETTING</a>
# Line 32  conversion went wrong.<br> Line 38  conversion went wrong.<br>
38  <P>  <P>
39  The syntax and semantics of the regular expressions supported by PCRE are  The syntax and semantics of the regular expressions supported by PCRE are
40  described below. Regular expressions are also described in the Perl  described below. Regular expressions are also described in the Perl
41  documentation and in a number of other books, some of which have copious  documentation and in a number of books, some of which have copious examples.
42  examples. Jeffrey Friedl's "Mastering Regular Expressions", published by  Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers
43  O'Reilly, covers them in great detail. The description here is intended as  regular expressions in great detail. This description of PCRE's regular
44  reference documentation.  expressions is intended as reference material.
45  </P>  </P>
46  <P>  <P>
47  The basic operation of PCRE is on strings of bytes. However, there is also  The original operation of PCRE was on strings of one-byte characters. However,
48  support for UTF-8 character strings. To use this support you must build PCRE to  there is now also support for UTF-8 character strings. To use this, you must
49  include UTF-8 support, and then call <b>pcre_compile()</b> with the PCRE_UTF8  build PCRE to include UTF-8 support, and then call <b>pcre_compile()</b> with
50  option. How this affects the pattern matching is mentioned in several places  the PCRE_UTF8 option. How this affects pattern matching is mentioned in several
51  below. There is also a summary of UTF-8 features in the  places below. There is also a summary of UTF-8 features in the
52  <a href="pcre.html#utf8support">section on UTF-8 support</a>  <a href="pcre.html#utf8support">section on UTF-8 support</a>
53  in the main  in the main
54  <a href="pcre.html"><b>pcre</b></a>  <a href="pcre.html"><b>pcre</b></a>
55  page.  page.
56  </P>  </P>
57  <P>  <P>
58    The remainder of this document discusses the patterns that are supported by
59    PCRE when its main matching function, <b>pcre_exec()</b>, is used.
60    From release 6.0, PCRE offers a second matching function,
61    <b>pcre_dfa_exec()</b>, which matches using a different algorithm that is not
62    Perl-compatible. The advantages and disadvantages of the alternative function,
63    and how it differs from the normal function, are discussed in the
64    <a href="pcrematching.html"><b>pcrematching</b></a>
65    page.
66    </P>
67    <P>
68  A regular expression is a pattern that is matched against a subject string from  A regular expression is a pattern that is matched against a subject string from
69  left to right. Most characters stand for themselves in a pattern, and match the  left to right. Most characters stand for themselves in a pattern, and match the
70  corresponding characters in the subject. As a trivial example, the pattern  corresponding characters in the subject. As a trivial example, the pattern
 </P>  
 <P>  
71  <pre>  <pre>
72    The quick brown fox    The quick brown fox
73  </PRE>  </pre>
74    matches a portion of a subject string that is identical to itself. When
75    caseless matching is specified (the PCRE_CASELESS option), letters are matched
76    independently of case. In UTF-8 mode, PCRE always understands the concept of
77    case for characters whose values are less than 128, so caseless matching is
78    always possible. For characters with higher values, the concept of case is
79    supported if PCRE is compiled with Unicode property support, but not otherwise.
80    If you want to use caseless matching for characters 128 and above, you must
81    ensure that PCRE is compiled with Unicode property support as well as with
82    UTF-8 support.
83  </P>  </P>
84  <P>  <P>
85  matches a portion of a subject string that is identical to itself. The power of  The power of regular expressions comes from the ability to include alternatives
86  regular expressions comes from the ability to include alternatives and  and repetitions in the pattern. These are encoded in the pattern by the use of
87  repetitions in the pattern. These are encoded in the pattern by the use of  <i>metacharacters</i>, which do not stand for themselves but instead are
 <i>meta-characters</i>, which do not stand for themselves but instead are  
88  interpreted in some special way.  interpreted in some special way.
89  </P>  </P>
90  <P>  <P>
91  There are two different sets of meta-characters: those that are recognized  There are two different sets of metacharacters: those that are recognized
92  anywhere in the pattern except within square brackets, and those that are  anywhere in the pattern except within square brackets, and those that are
93  recognized in square brackets. Outside square brackets, the meta-characters are  recognized in square brackets. Outside square brackets, the metacharacters are
94  as follows:  as follows:
 </P>  
 <P>  
95  <pre>  <pre>
96    \      general escape character with several uses    \      general escape character with several uses
97    ^      assert start of string (or line, in multiline mode)    ^      assert start of string (or line, in multiline mode)
# Line 88  as follows: Line 108  as follows:
108    +      1 or more quantifier    +      1 or more quantifier
109           also "possessive quantifier"           also "possessive quantifier"
110    {      start min/max quantifier    {      start min/max quantifier
111  </PRE>  </pre>
 </P>  
 <P>  
112  Part of a pattern that is in square brackets is called a "character class". In  Part of a pattern that is in square brackets is called a "character class". In
113  a character class the only meta-characters are:  a character class the only metacharacters are:
 </P>  
 <P>  
114  <pre>  <pre>
115    \      general escape character    \      general escape character
116    ^      negate the class, but only if the first character    ^      negate the class, but only if the first character
117    -      indicates character range    -      indicates character range
118    [      POSIX character class (only if followed by POSIX    [      POSIX character class (only if followed by POSIX syntax)
            syntax)  
119    ]      terminates the character class    ]      terminates the character class
120  </PRE>  </pre>
121  </P>  The following sections describe the use of each of the metacharacters.
 <P>  
 The following sections describe the use of each of the meta-characters.  
122  </P>  </P>
123  <br><a name="SEC2" href="#TOC1">BACKSLASH</a><br>  <br><a name="SEC2" href="#TOC1">BACKSLASH</a><br>
124  <P>  <P>
125  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
126  non-alphameric character, it takes away any special meaning that character may  non-alphanumeric character, it takes away any special meaning that character
127  have. This use of backslash as an escape character applies both inside and  may have. This use of backslash as an escape character applies both inside and
128  outside character classes.  outside character classes.
129  </P>  </P>
130  <P>  <P>
131  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.
132  This escaping action applies whether or not the following character would  This escaping action applies whether or not the following character would
133  otherwise be interpreted as a meta-character, so it is always safe to precede a  otherwise be interpreted as a metacharacter, so it is always safe to precede a
134  non-alphameric with backslash to specify that it stands for itself. In  non-alphanumeric with backslash to specify that it stands for itself. In
135  particular, if you want to match a backslash, you write \\.  particular, if you want to match a backslash, you write \\.
136  </P>  </P>
137  <P>  <P>
138  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
139  pattern (other than in a character class) and characters between a # outside  pattern (other than in a character class) and characters between a # outside
140  a character class and the next newline character are ignored. An escaping  a character class and the next newline are ignored. An escaping backslash can
141  backslash can be used to include a whitespace or # character as part of the  be used to include a whitespace or # character as part of the pattern.
 pattern.  
142  </P>  </P>
143  <P>  <P>
144  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
145  can do so by putting them between \Q and \E. This is different from Perl in  can do so by putting them between \Q and \E. This is different from Perl in
146  that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in  that $ and @ are handled as literals in \Q...\E sequences in PCRE, whereas in
147  Perl, $ and @ cause variable interpolation. Note the following examples:  Perl, $ and @ cause variable interpolation. Note the following examples:
 </P>  
 <P>  
148  <pre>  <pre>
149    Pattern            PCRE matches   Perl matches    Pattern            PCRE matches   Perl matches
150  </PRE>  
151  </P>    \Qabc$xyz\E        abc$xyz        abc followed by the contents of $xyz
 <P>  
 <pre>  
   \Qabc$xyz\E        abc$xyz        abc followed by the  
                                       contents of $xyz  
152    \Qabc\$xyz\E       abc\$xyz       abc\$xyz    \Qabc\$xyz\E       abc\$xyz       abc\$xyz
153    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz    \Qabc\E\$\Qxyz\E   abc$xyz        abc$xyz
154  </PRE>  </pre>
 </P>  
 <P>  
155  The \Q...\E sequence is recognized both inside and outside character classes.  The \Q...\E sequence is recognized both inside and outside character classes.
156  </P>  <a name="digitsafterbackslash"></a></P>
157    <br><b>
158    Non-printing characters
159    </b><br>
160  <P>  <P>
161  A second use of backslash provides a way of encoding non-printing characters  A second use of backslash provides a way of encoding non-printing characters
162  in patterns in a visible manner. There is no restriction on the appearance of  in patterns in a visible manner. There is no restriction on the appearance of
# Line 157  non-printing characters, apart from the Line 164  non-printing characters, apart from the
164  but when a pattern is being prepared by text editing, it is usually easier to  but when a pattern is being prepared by text editing, it is usually easier to
165  use one of the following escape sequences than the binary character it  use one of the following escape sequences than the binary character it
166  represents:  represents:
 </P>  
 <P>  
167  <pre>  <pre>
168    \a        alarm, that is, the BEL character (hex 07)    \a        alarm, that is, the BEL character (hex 07)
169    \cx       "control-x", where x is any character    \cx       "control-x", where x is any character
# Line 169  represents: Line 174  represents:
174    \t        tab (hex 09)    \t        tab (hex 09)
175    \ddd      character with octal code ddd, or backreference    \ddd      character with octal code ddd, or backreference
176    \xhh      character with hex code hh    \xhh      character with hex code hh
177    \x{hhh..} character with hex code hhh... (UTF-8 mode only)    \x{hhh..} character with hex code hhh..
178  </PRE>  </pre>
 </P>  
 <P>  
179  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
180  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.
181  Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex  Thus \cz becomes hex 1A, but \c{ becomes hex 3B, while \c; becomes hex
# Line 180  Thus \cz becomes hex 1A, but \c{ becomes Line 183  Thus \cz becomes hex 1A, but \c{ becomes
183  </P>  </P>
184  <P>  <P>
185  After \x, from zero to two hexadecimal digits are read (letters can be in  After \x, from zero to two hexadecimal digits are read (letters can be in
186  upper or lower case). In UTF-8 mode, any number of hexadecimal digits may  upper or lower case). Any number of hexadecimal digits may appear between \x{
187  appear between \x{ and }, but the value of the character code must be less  and }, but the value of the character code must be less than 256 in non-UTF-8
188  than 2**31 (that is, the maximum hexadecimal value is 7FFFFFFF). If characters  mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value
189  other than hexadecimal digits appear between \x{ and }, or if there is no  is 7FFFFFFF). If characters other than hexadecimal digits appear between \x{
190  terminating }, this form of escape is not recognized. Instead, the initial  and }, or if there is no terminating }, this form of escape is not recognized.
191  \x will be interpreted as a basic hexadecimal escape, with no following  Instead, the initial \x will be interpreted as a basic hexadecimal escape,
192  digits, giving a byte whose value is zero.  with no following digits, giving a character whose value is zero.
193  </P>  </P>
194  <P>  <P>
195  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
196  syntaxes for \x when PCRE is in UTF-8 mode. There is no difference in the  syntaxes for \x. There is no difference in the way they are handled. For
197  way they are handled. For example, \xdc is exactly the same as \x{dc}.  example, \xdc is exactly the same as \x{dc}.
198  </P>  </P>
199  <P>  <P>
200  After \0 up to two further octal digits are read. In both cases, if there  After \0 up to two further octal digits are read. If there are fewer than two
201  are fewer than two digits, just those that are present are used. Thus the  digits, just those that are present are used. Thus the sequence \0\x\07
202  sequence \0\x\07 specifies two binary zeros followed by a BEL character  specifies two binary zeros followed by a BEL character (code value 7). Make
203  (code value 7). Make sure you supply two digits after the initial zero if the  sure you supply two digits after the initial zero if the pattern character that
204  character that follows is itself an octal digit.  follows is itself an octal digit.
205  </P>  </P>
206  <P>  <P>
207  The handling of a backslash followed by a digit other than 0 is complicated.  The handling of a backslash followed by a digit other than 0 is complicated.
# Line 206  Outside a character class, PCRE reads it Line 209  Outside a character class, PCRE reads it
209  number. If the number is less than 10, or if there have been at least that many  number. If the number is less than 10, or if there have been at least that many
210  previous capturing left parentheses in the expression, the entire sequence is  previous capturing left parentheses in the expression, the entire sequence is
211  taken as a <i>back reference</i>. A description of how this works is given  taken as a <i>back reference</i>. A description of how this works is given
212  later, following the discussion of parenthesized subpatterns.  <a href="#backreferences">later,</a>
213    following the discussion of
214    <a href="#subpattern">parenthesized subpatterns.</a>
215  </P>  </P>
216  <P>  <P>
217  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
218  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
219  digits following the backslash, and generates a single byte from the least  digits following the backslash, ane uses them to generate a data character. Any
220  significant 8 bits of the value. Any subsequent digits stand for themselves.  subsequent digits stand for themselves. In non-UTF-8 mode, the value of a
221  For example:  character specified in octal must be less than \400. In UTF-8 mode, values up
222  </P>  to \777 are permitted. For example:
 <P>  
223  <pre>  <pre>
224    \040   is another way of writing a space    \040   is another way of writing a space
225    \40    is the same, provided there are fewer than 40    \40    is the same, provided there are fewer than 40 previous capturing subpatterns
             previous capturing subpatterns  
226    \7     is always a back reference    \7     is always a back reference
227    \11    might be a back reference, or another way of    \11    might be a back reference, or another way of writing a tab
             writing a tab  
228    \011   is always a tab    \011   is always a tab
229    \0113  is a tab followed by the character "3"    \0113  is a tab followed by the character "3"
230    \113   might be a back reference, otherwise the    \113   might be a back reference, otherwise the character with octal code 113
231              character with octal code 113    \377   might be a back reference, otherwise the byte consisting entirely of 1 bits
232    \377   might be a back reference, otherwise    \81    is either a back reference, or a binary zero followed by the two characters "8" and "1"
233              the byte consisting entirely of 1 bits  </pre>
   \81    is either a back reference, or a binary zero  
             followed by the two characters "8" and "1"  
 </PRE>  
 </P>  
 <P>  
234  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
235  zero, because no more than three octal digits are ever read.  zero, because no more than three octal digits are ever read.
236  </P>  </P>
237  <P>  <P>
238  All the sequences that define a single byte value or a single UTF-8 character  All the sequences that define a single character value can be used both inside
239  (in UTF-8 mode) can be used both inside and outside character classes. In  and outside character classes. In addition, inside a character class, the
240  addition, inside a character class, the sequence \b is interpreted as the  sequence \b is interpreted as the backspace character (hex 08), and the
241  backspace character (hex 08). Outside a character class it has a different  sequence \X is interpreted as the character "X". Outside a character class,
242  meaning (see below).  these sequences have different meanings
243  </P>  <a href="#uniextseq">(see below).</a>
244  <P>  </P>
245  The third use of backslash is for specifying generic character types:  <br><b>
246  </P>  Generic character types
247    </b><br>
248  <P>  <P>
249    The third use of backslash is for specifying generic character types. The
250    following are always recognized:
251  <pre>  <pre>
252    \d     any decimal digit    \d     any decimal digit
253    \D     any character that is not a decimal digit    \D     any character that is not a decimal digit
# Line 255  The third use of backslash is for specif Line 255  The third use of backslash is for specif
255    \S     any character that is not a whitespace character    \S     any character that is not a whitespace character
256    \w     any "word" character    \w     any "word" character
257    \W     any "non-word" character    \W     any "non-word" character
258  </PRE>  </pre>
 </P>  
 <P>  
259  Each pair of escape sequences partitions the complete set of characters into  Each pair of escape sequences partitions the complete set of characters into
260  two disjoint sets. Any given character matches one, and only one, of each pair.  two disjoint sets. Any given character matches one, and only one, of each pair.
261  </P>  </P>
262  <P>  <P>
263  In UTF-8 mode, characters with values greater than 255 never match \d, \s, or  These character type sequences can appear both inside and outside character
264  \w, and always match \D, \S, and \W.  classes. They each match one character of the appropriate type. If the current
265    matching point is at the end of the subject string, all of them fail, since
266    there is no character to match.
267  </P>  </P>
268  <P>  <P>
269  For compatibility with Perl, \s does not match the VT character (code 11).  For compatibility with Perl, \s does not match the VT character (code 11).
270  This makes it different from the the POSIX "space" class. The \s characters  This makes it different from the the POSIX "space" class. The \s characters
271  are HT (9), LF (10), FF (12), CR (13), and space (32).  are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is
272    included in a Perl script, \s may match the VT character. In PCRE, it never
273    does.)
274  </P>  </P>
275  <P>  <P>
276  A "word" character is any letter or digit or the underscore character, that is,  A "word" character is an underscore or any character less than 256 that is a
277  any character which can be part of a Perl "word". The definition of letters and  letter or digit. The definition of letters and digits is controlled by PCRE's
278  digits is controlled by PCRE's character tables, and may vary if locale-  low-valued character tables, and may vary if locale-specific matching is taking
279  specific matching is taking place (see  place (see
280  <a href="pcreapi.html#localesupport">"Locale support"</a>  <a href="pcreapi.html#localesupport">"Locale support"</a>
281  in the  in the
282  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
283  page). For example, in the "fr" (French) locale, some character codes greater  page). For example, in the "fr_FR" (French) locale, some character codes
284  than 128 are used for accented letters, and these are matched by \w.  greater than 128 are used for accented letters, and these are matched by \w.
285  </P>  </P>
286  <P>  <P>
287  These character type sequences can appear both inside and outside character  In UTF-8 mode, characters with values greater than 128 never match \d, \s, or
288  classes. They each match one character of the appropriate type. If the current  \w, and always match \D, \S, and \W. This is true even when Unicode
289  matching point is at the end of the subject string, all of them fail, since  character property support is available. The use of locales with Unicode is
290  there is no character to match.  discouraged.
291  </P>  <a name="uniextseq"></a></P>
292    <br><b>
293    Unicode character properties
294    </b><br>
295    <P>
296    When PCRE is built with Unicode character property support, three additional
297    escape sequences to match character properties are available when UTF-8 mode
298    is selected. They are:
299    <pre>
300      \p{<i>xx</i>}   a character with the <i>xx</i> property
301      \P{<i>xx</i>}   a character without the <i>xx</i> property
302      \X       an extended Unicode sequence
303    </pre>
304    The property names represented by <i>xx</i> above are limited to the Unicode
305    script names, the general category properties, and "Any", which matches any
306    character (including newline). Other properties such as "InMusicalSymbols" are
307    not currently supported by PCRE. Note that \P{Any} does not match any
308    characters, so always causes a match failure.
309    </P>
310    <P>
311    Sets of Unicode characters are defined as belonging to certain scripts. A
312    character from one of these sets can be matched using a script name. For
313    example:
314    <pre>
315      \p{Greek}
316      \P{Han}
317    </pre>
318    Those that are not part of an identified script are lumped together as
319    "Common". The current list of scripts is:
320    </P>
321    <P>
322    Arabic,
323    Armenian,
324    Bengali,
325    Bopomofo,
326    Braille,
327    Buginese,
328    Buhid,
329    Canadian_Aboriginal,
330    Cherokee,
331    Common,
332    Coptic,
333    Cypriot,
334    Cyrillic,
335    Deseret,
336    Devanagari,
337    Ethiopic,
338    Georgian,
339    Glagolitic,
340    Gothic,
341    Greek,
342    Gujarati,
343    Gurmukhi,
344    Han,
345    Hangul,
346    Hanunoo,
347    Hebrew,
348    Hiragana,
349    Inherited,
350    Kannada,
351    Katakana,
352    Kharoshthi,
353    Khmer,
354    Lao,
355    Latin,
356    Limbu,
357    Linear_B,
358    Malayalam,
359    Mongolian,
360    Myanmar,
361    New_Tai_Lue,
362    Ogham,
363    Old_Italic,
364    Old_Persian,
365    Oriya,
366    Osmanya,
367    Runic,
368    Shavian,
369    Sinhala,
370    Syloti_Nagri,
371    Syriac,
372    Tagalog,
373    Tagbanwa,
374    Tai_Le,
375    Tamil,
376    Telugu,
377    Thaana,
378    Thai,
379    Tibetan,
380    Tifinagh,
381    Ugaritic,
382    Yi.
383    </P>
384    <P>
385    Each character has exactly one general category property, specified by a
386    two-letter abbreviation. For compatibility with Perl, negation can be specified
387    by including a circumflex between the opening brace and the property name. For
388    example, \p{^Lu} is the same as \P{Lu}.
389    </P>
390    <P>
391    If only one letter is specified with \p or \P, it includes all the general
392    category properties that start with that letter. In this case, in the absence
393    of negation, the curly brackets in the escape sequence are optional; these two
394    examples have the same effect:
395    <pre>
396      \p{L}
397      \pL
398    </pre>
399    The following general category property codes are supported:
400    <pre>
401      C     Other
402      Cc    Control
403      Cf    Format
404      Cn    Unassigned
405      Co    Private use
406      Cs    Surrogate
407    
408      L     Letter
409      Ll    Lower case letter
410      Lm    Modifier letter
411      Lo    Other letter
412      Lt    Title case letter
413      Lu    Upper case letter
414    
415      M     Mark
416      Mc    Spacing mark
417      Me    Enclosing mark
418      Mn    Non-spacing mark
419    
420      N     Number
421      Nd    Decimal number
422      Nl    Letter number
423      No    Other number
424    
425      P     Punctuation
426      Pc    Connector punctuation
427      Pd    Dash punctuation
428      Pe    Close punctuation
429      Pf    Final punctuation
430      Pi    Initial punctuation
431      Po    Other punctuation
432      Ps    Open punctuation
433    
434      S     Symbol
435      Sc    Currency symbol
436      Sk    Modifier symbol
437      Sm    Mathematical symbol
438      So    Other symbol
439    
440      Z     Separator
441      Zl    Line separator
442      Zp    Paragraph separator
443      Zs    Space separator
444    </pre>
445    The special property L& is also supported: it matches a character that has
446    the Lu, Ll, or Lt property, in other words, a letter that is not classified as
447    a modifier or "other".
448    </P>
449    <P>
450    The long synonyms for these properties that Perl supports (such as \p{Letter})
451    are not supported by PCRE, nor is it permitted to prefix any of these
452    properties with "Is".
453    </P>
454    <P>
455    No character that is in the Unicode table has the Cn (unassigned) property.
456    Instead, this property is assumed for any code point that is not in the
457    Unicode table.
458    </P>
459    <P>
460    Specifying caseless matching does not affect these escape sequences. For
461    example, \p{Lu} always matches only upper case letters.
462    </P>
463    <P>
464    The \X escape matches any number of Unicode characters that form an extended
465    Unicode sequence. \X is equivalent to
466    <pre>
467      (?&#62;\PM\pM*)
468    </pre>
469    That is, it matches a character without the "mark" property, followed by zero
470    or more characters with the "mark" property, and treats the sequence as an
471    atomic group
472    <a href="#atomicgroup">(see below).</a>
473    Characters with the "mark" property are typically accents that affect the
474    preceding character.
475    </P>
476    <P>
477    Matching characters by Unicode property is not fast, because PCRE has to search
478    a structure that contains data for over fifteen thousand characters. That is
479    why the traditional escape sequences such as \d and \w do not use Unicode
480    properties in PCRE.
481    <a name="smallassertions"></a></P>
482    <br><b>
483    Simple assertions
484    </b><br>
485  <P>  <P>
486  The fourth use of backslash is for certain simple assertions. An assertion  The fourth use of backslash is for certain simple assertions. An assertion
487  specifies a condition that has to be met at a particular point in a match,  specifies a condition that has to be met at a particular point in a match,
488  without consuming any characters from the subject string. The use of  without consuming any characters from the subject string. The use of
489  subpatterns for more complicated assertions is described below. The backslashed  subpatterns for more complicated assertions is described
490  assertions are  <a href="#bigassertions">below.</a>
491  </P>  The backslashed assertions are:
 <P>  
492  <pre>  <pre>
493    \b     matches at a word boundary    \b     matches at a word boundary
494    \B     matches when not at a word boundary    \B     matches when not at a word boundary
# Line 302  assertions are Line 496  assertions are
496    \Z     matches at end of subject or before newline at end    \Z     matches at end of subject or before newline at end
497    \z     matches at end of subject    \z     matches at end of subject
498    \G     matches at first matching position in subject    \G     matches at first matching position in subject
499  </PRE>  </pre>
 </P>  
 <P>  
500  These assertions may not appear in character classes (but note that \b has a  These assertions may not appear in character classes (but note that \b has a
501  different meaning, namely the backspace character, inside a character class).  different meaning, namely the backspace character, inside a character class).
502  </P>  </P>
# Line 316  first or last character matches \w, resp Line 508  first or last character matches \w, resp
508  </P>  </P>
509  <P>  <P>
510  The \A, \Z, and \z assertions differ from the traditional circumflex and  The \A, \Z, and \z assertions differ from the traditional circumflex and
511  dollar (described below) in that they only ever match at the very start and end  dollar (described in the next section) in that they only ever match at the very
512  of the subject string, whatever options are set. Thus, they are independent of  start and end of the subject string, whatever options are set. Thus, they are
513  multiline mode.  independent of multiline mode. These three assertions are not affected by the
514  </P>  PCRE_NOTBOL or PCRE_NOTEOL options, which affect only the behaviour of the
515  <P>  circumflex and dollar metacharacters. However, if the <i>startoffset</i>
516  They are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options. If the  argument of <b>pcre_exec()</b> is non-zero, indicating that matching is to start
517  <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero, indicating that  at a point other than the beginning of the subject, \A can never match. The
518  matching is to start at a point other than the beginning of the subject, \A  difference between \Z and \z is that \Z matches before a newline at the end
519  can never match. The difference between \Z and \z is that \Z matches before  of the string as well as at the very end, whereas \z matches only at the end.
 a newline that is the last character of the string as well as at the end of the  
 string, whereas \z matches only at the end.  
520  </P>  </P>
521  <P>  <P>
522  The \G assertion is true only when the current matching position is at the  The \G assertion is true only when the current matching position is at the
# Line 351  regular expression. Line 541  regular expression.
541  <br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>  <br><a name="SEC3" href="#TOC1">CIRCUMFLEX AND DOLLAR</a><br>
542  <P>  <P>
543  Outside a character class, in the default matching mode, the circumflex  Outside a character class, in the default matching mode, the circumflex
544  character is an assertion which is true only if the current matching point is  character is an assertion that is true only if the current matching point is
545  at the start of the subject string. If the <i>startoffset</i> argument of  at the start of the subject string. If the <i>startoffset</i> argument of
546  <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE  <b>pcre_exec()</b> is non-zero, circumflex can never match if the PCRE_MULTILINE
547  option is unset. Inside a character class, circumflex has an entirely different  option is unset. Inside a character class, circumflex has an entirely different
548  meaning (see below).  meaning
549    <a href="#characterclass">(see below).</a>
550  </P>  </P>
551  <P>  <P>
552  Circumflex need not be the first character of the pattern if a number of  Circumflex need not be the first character of the pattern if a number of
# Line 367  constrained to match only at the start o Line 558  constrained to match only at the start o
558  to be anchored.)  to be anchored.)
559  </P>  </P>
560  <P>  <P>
561  A dollar character is an assertion which is true only if the current matching  A dollar character is an assertion that is true only if the current matching
562  point is at the end of the subject string, or immediately before a newline  point is at the end of the subject string, or immediately before a newline
563  character that is the last character in the string (by default). Dollar need  at the end of the string (by default). Dollar need not be the last character of
564  not be the last character of the pattern if a number of alternatives are  the pattern if a number of alternatives are involved, but it should be the last
565  involved, but it should be the last item in any branch in which it appears.  item in any branch in which it appears. Dollar has no special meaning in a
566  Dollar has no special meaning in a character class.  character class.
567  </P>  </P>
568  <P>  <P>
569  The meaning of dollar can be changed so that it matches only at the very end of  The meaning of dollar can be changed so that it matches only at the very end of
# Line 381  does not affect the \Z assertion. Line 572  does not affect the \Z assertion.
572  </P>  </P>
573  <P>  <P>
574  The meanings of the circumflex and dollar characters are changed if the  The meanings of the circumflex and dollar characters are changed if the
575  PCRE_MULTILINE option is set. When this is the case, they match immediately  PCRE_MULTILINE option is set. When this is the case, a circumflex matches
576  after and immediately before an internal newline character, respectively, in  immediately after internal newlines as well as at the start of the subject
577  addition to matching at the start and end of the subject string. For example,  string. It does not match after a newline that ends the string. A dollar
578  the pattern /^abc$/ matches the subject string "def\nabc" in multiline mode,  matches before any newlines in the string, as well as at the very end, when
579  but not otherwise. Consequently, patterns that are anchored in single line mode  PCRE_MULTILINE is set. When newline is specified as the two-character
580  because all branches start with ^ are not anchored in multiline mode, and a  sequence CRLF, isolated CR and LF characters do not indicate newlines.
581  match for circumflex is possible when the <i>startoffset</i> argument of  </P>
582  <b>pcre_exec()</b> is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if  <P>
583  PCRE_MULTILINE is set.  For example, the pattern /^abc$/ matches the subject string "def\nabc" (where
584    \n represents a newline) in multiline mode, but not otherwise. Consequently,
585    patterns that are anchored in single line mode because all branches start with
586    ^ are not anchored in multiline mode, and a match for circumflex is possible
587    when the <i>startoffset</i> argument of <b>pcre_exec()</b> is non-zero. The
588    PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
589  </P>  </P>
590  <P>  <P>
591  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
592  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
593  \A it is always anchored, whether PCRE_MULTILINE is set or not.  \A it is always anchored, whether or not PCRE_MULTILINE is set.
594  </P>  </P>
595  <br><a name="SEC4" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>  <br><a name="SEC4" href="#TOC1">FULL STOP (PERIOD, DOT)</a><br>
596  <P>  <P>
597  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
598  the subject, including a non-printing character, but not (by default) newline.  the subject string except (by default) a character that signifies the end of a
599  In UTF-8 mode, a dot matches any UTF-8 character, which might be more than one  line. In UTF-8 mode, the matched character may be more than one byte long. When
600  byte long, except (by default) for newline. If the PCRE_DOTALL option is set,  a line ending is defined as a single character (CR or LF), dot never matches
601  dots match newlines as well. The handling of dot is entirely independent of the  that character; when the two-character sequence CRLF is used, dot does not
602  handling of circumflex and dollar, the only relationship being that they both  match CR if it is immediately followed by LF, but otherwise it matches all
603  involve newline characters. Dot has no special meaning in a character class.  characters (including isolated CRs and LFs).
604    </P>
605    <P>
606    The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
607    option is set, a dot matches any one character, without exception. If newline
608    is defined as the two-character sequence CRLF, it takes two dots to match it.
609    </P>
610    <P>
611    The handling of dot is entirely independent of the handling of circumflex and
612    dollar, the only relationship being that they both involve newlines. Dot has no
613    special meaning in a character class.
614  </P>  </P>
615  <br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br>  <br><a name="SEC5" href="#TOC1">MATCHING A SINGLE BYTE</a><br>
616  <P>  <P>
617  Outside a character class, the escape sequence \C matches any one byte, both  Outside a character class, the escape sequence \C matches any one byte, both
618  in and out of UTF-8 mode. Unlike a dot, it always matches a newline. The  in and out of UTF-8 mode. Unlike a dot, it always matches CR and LF. The
619  feature is provided in Perl in order to match individual bytes in UTF-8 mode.  feature is provided in Perl in order to match individual bytes in UTF-8 mode.
620  Because it breaks up UTF-8 characters into individual bytes, what remains in  Because it breaks up UTF-8 characters into individual bytes, what remains in
621  the string may be a malformed UTF-8 string. For this reason it is best avoided.  the string may be a malformed UTF-8 string. For this reason, the \C escape
622    sequence is best avoided.
623  </P>  </P>
624  <P>  <P>
625  PCRE does not allow \C to appear in lookbehind assertions (see below), because  PCRE does not allow \C to appear in lookbehind assertions
626  in UTF-8 mode it makes it impossible to calculate the length of the lookbehind.  <a href="#lookbehind">(described below),</a>
627  </P>  because in UTF-8 mode this would make it impossible to calculate the length of
628  <br><a name="SEC6" href="#TOC1">SQUARE BRACKETS</a><br>  the lookbehind.
629    <a name="characterclass"></a></P>
630    <br><a name="SEC6" href="#TOC1">SQUARE BRACKETS AND CHARACTER CLASSES</a><br>
631  <P>  <P>
632  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
633  square bracket. A closing square bracket on its own is not special. If a  square bracket. A closing square bracket on its own is not special. If a
# Line 438  backslash. Line 647  backslash.
647  <P>  <P>
648  For example, the character class [aeiou] matches any lower case vowel, while  For example, the character class [aeiou] matches any lower case vowel, while
649  [^aeiou] matches any character that is not a lower case vowel. Note that a  [^aeiou] matches any character that is not a lower case vowel. Note that a
650  circumflex is just a convenient notation for specifying the characters which  circumflex is just a convenient notation for specifying the characters that
651  are in the class by enumerating those that are not. It is not an assertion: it  are in the class by enumerating those that are not. A class that starts with a
652  still consumes a character from the subject string, and fails if the current  circumflex is not an assertion: it still consumes a character from the subject
653  pointer is at the end of the string.  string, and therefore it fails if the current pointer is at the end of the
654    string.
655  </P>  </P>
656  <P>  <P>
657  In UTF-8 mode, characters with values greater than 255 can be included in a  In UTF-8 mode, characters with values greater than 255 can be included in a
# Line 451  class as a literal string of bytes, or b Line 661  class as a literal string of bytes, or b
661  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
662  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
663  "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
664  caseful version would. PCRE does not support the concept of case for characters  caseful version would. In UTF-8 mode, PCRE always understands the concept of
665  with values greater than 255.  case for characters whose values are less than 128, so caseless matching is
666    always possible. For characters with higher values, the concept of case is
667    supported if PCRE is compiled with Unicode property support, but not otherwise.
668    If you want to use caseless matching for characters 128 and above, you must
669    ensure that PCRE is compiled with Unicode property support as well as with
670    UTF-8 support.
671  </P>  </P>
672  <P>  <P>
673  The newline character is never treated in any special way in character classes,  Characters that might indicate line breaks (CR and LF) are never treated in any
674  whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class  special way when matching character classes, whatever line-ending sequence is
675  such as [^a] will always match a newline.  in use, and whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is
676    used. A class such as [^a] always matches one of these characters.
677  </P>  </P>
678  <P>  <P>
679  The minus (hyphen) character can be used to specify a range of characters in a  The minus (hyphen) character can be used to specify a range of characters in a
# Line 471  It is not possible to have the literal c Line 687  It is not possible to have the literal c
687  range. A pattern such as [W-]46] is interpreted as a class of two characters  range. A pattern such as [W-]46] is interpreted as a class of two characters
688  ("W" and "-") followed by a literal string "46]", so it would match "W46]" or  ("W" and "-") followed by a literal string "46]", so it would match "W46]" or
689  "-46]". However, if the "]" is escaped with a backslash it is interpreted as  "-46]". However, if the "]" is escaped with a backslash it is interpreted as
690  the end of range, so [W-\]46] is interpreted as a single class containing a  the end of range, so [W-\]46] is interpreted as a class containing a range
691  range followed by two separate characters. The octal or hexadecimal  followed by two other characters. The octal or hexadecimal representation of
692  representation of "]" can also be used to end a range.  "]" can also be used to end a range.
693  </P>  </P>
694  <P>  <P>
695  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
# Line 484  example [\x{100}-\x{2ff}]. Line 700  example [\x{100}-\x{2ff}].
700  <P>  <P>
701  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
702  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
703  [][\^_`wxyzabc], matched caselessly, and if character tables for the "fr"  [][\\^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
704  locale are in use, [\xc8-\xcb] matches accented E characters in both cases.  tables for the "fr_FR" locale are in use, [\xc8-\xcb] matches accented E
705    characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
706    characters with values greater than 128 only when it is compiled with Unicode
707    property support.
708  </P>  </P>
709  <P>  <P>
710  The character types \d, \D, \s, \S, \w, and \W may also appear in a  The character types \d, \D, \p, \P, \s, \S, \w, and \W may also appear
711  character class, and add the characters that they match to the class. For  in a character class, and add the characters that they match to the class. For
712  example, [\dABCDEF] matches any hexadecimal digit. A circumflex can  example, [\dABCDEF] matches any hexadecimal digit. A circumflex can
713  conveniently be used with the upper case character types to specify a more  conveniently be used with the upper case character types to specify a more
714  restricted set of characters than the matching lower case type. For example,  restricted set of characters than the matching lower case type. For example,
715  the class [^\W_] matches any letter or digit, but not underscore.  the class [^\W_] matches any letter or digit, but not underscore.
716  </P>  </P>
717  <P>  <P>
718  All non-alphameric characters other than \, -, ^ (at the start) and the  The only metacharacters that are recognized in character classes are backslash,
719  terminating ] are non-special in character classes, but it does no harm if they  hyphen (only where it can be interpreted as specifying a range), circumflex
720  are escaped.  (only at the start), opening square bracket (only when it can be interpreted as
721    introducing a POSIX class name - see the next section), and the terminating
722    closing square bracket. However, escaping other non-alphanumeric characters
723    does no harm.
724  </P>  </P>
725  <br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br>  <br><a name="SEC7" href="#TOC1">POSIX CHARACTER CLASSES</a><br>
726  <P>  <P>
727  Perl supports the POSIX notation for character classes, which uses names  Perl supports the POSIX notation for character classes. This uses names
728  enclosed by [: and :] within the enclosing square brackets. PCRE also supports  enclosed by [: and :] within the enclosing square brackets. PCRE also supports
729  this notation. For example,  this notation. For example,
 </P>  
 <P>  
730  <pre>  <pre>
731    [01[:alpha:]%]    [01[:alpha:]%]
732  </PRE>  </pre>
 </P>  
 <P>  
733  matches "0", "1", any alphabetic character, or "%". The supported class names  matches "0", "1", any alphabetic character, or "%". The supported class names
734  are  are
 </P>  
 <P>  
735  <pre>  <pre>
736    alnum    letters and digits    alnum    letters and digits
737    alpha    letters    alpha    letters
# Line 531  are Line 747  are
747    upper    upper case letters    upper    upper case letters
748    word     "word" characters (same as \w)    word     "word" characters (same as \w)
749    xdigit   hexadecimal digits    xdigit   hexadecimal digits
750  </PRE>  </pre>
 </P>  
 <P>  
751  The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and  The "space" characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
752  space (32). Notice that this list includes the VT character (code 11). This  space (32). Notice that this list includes the VT character (code 11). This
753  makes "space" different to \s, which does not include VT (for Perl  makes "space" different to \s, which does not include VT (for Perl
# Line 543  compatibility). Line 757  compatibility).
757  The name "word" is a Perl extension, and "blank" is a GNU extension from Perl  The name "word" is a Perl extension, and "blank" is a GNU extension from Perl
758  5.8. Another Perl extension is negation, which is indicated by a ^ character  5.8. Another Perl extension is negation, which is indicated by a ^ character
759  after the colon. For example,  after the colon. For example,
 </P>  
 <P>  
760  <pre>  <pre>
761    [12[:^digit:]]    [12[:^digit:]]
762  </PRE>  </pre>
 </P>  
 <P>  
763  matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX  matches "1", "2", or any non-digit. PCRE (and Perl) also recognize the POSIX
764  syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not  syntax [.ch.] and [=ch=] where "ch" is a "collating element", but these are not
765  supported, and an error is given if they are encountered.  supported, and an error is given if they are encountered.
766  </P>  </P>
767  <P>  <P>
768  In UTF-8 mode, characters with values greater than 255 do not match any of  In UTF-8 mode, characters with values greater than 128 do not match any of
769  the POSIX character classes.  the POSIX character classes.
770  </P>  </P>
771  <br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br>  <br><a name="SEC8" href="#TOC1">VERTICAL BAR</a><br>
772  <P>  <P>
773  Vertical bar characters are used to separate alternative patterns. For example,  Vertical bar characters are used to separate alternative patterns. For example,
774  the pattern  the pattern
 </P>  
 <P>  
775  <pre>  <pre>
776    gilbert|sullivan    gilbert|sullivan
777  </PRE>  </pre>
 </P>  
 <P>  
778  matches either "gilbert" or "sullivan". Any number of alternatives may appear,  matches either "gilbert" or "sullivan". Any number of alternatives may appear,
779  and an empty alternative is permitted (matching the empty string).  and an empty alternative is permitted (matching the empty string). The matching
780  The matching process tries each alternative in turn, from left to right,  process tries each alternative in turn, from left to right, and the first one
781  and the first one that succeeds is used. If the alternatives are within a  that succeeds is used. If the alternatives are within a subpattern
782  subpattern (defined below), "succeeds" means matching the rest of the main  <a href="#subpattern">(defined below),</a>
783  pattern as well as the alternative in the subpattern.  "succeeds" means matching the rest of the main pattern as well as the
784    alternative in the subpattern.
785  </P>  </P>
786  <br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br>  <br><a name="SEC9" href="#TOC1">INTERNAL OPTION SETTING</a><br>
787  <P>  <P>
788  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
789  PCRE_EXTENDED options can be changed from within the pattern by a sequence of  PCRE_EXTENDED options can be changed from within the pattern by a sequence of
790  Perl option letters enclosed between "(?" and ")". The option letters are  Perl option letters enclosed between "(?" and ")". The option letters are
 </P>  
 <P>  
791  <pre>  <pre>
792    i  for PCRE_CASELESS    i  for PCRE_CASELESS
793    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
794    s  for PCRE_DOTALL    s  for PCRE_DOTALL
795    x  for PCRE_EXTENDED    x  for PCRE_EXTENDED
796  </PRE>  </pre>
 </P>  
 <P>  
797  For example, (?im) sets caseless, multiline matching. It is also possible to  For example, (?im) sets caseless, multiline matching. It is also possible to
798  unset these options by preceding the letter with a hyphen, and a combined  unset these options by preceding the letter with a hyphen, and a combined
799  setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and  setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and
# Line 608  the global options (and it will therefor Line 811  the global options (and it will therefor
811  <P>  <P>
812  An option change within a subpattern affects only that part of the current  An option change within a subpattern affects only that part of the current
813  pattern that follows it, so  pattern that follows it, so
 </P>  
 <P>  
814  <pre>  <pre>
815    (a(?i)b)c    (a(?i)b)c
816  </PRE>  </pre>
 </P>  
 <P>  
817  matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).  matches abc and aBc and no other strings (assuming PCRE_CASELESS is not used).
818  By this means, options can be made to have different settings in different  By this means, options can be made to have different settings in different
819  parts of the pattern. Any changes made in one alternative do carry on  parts of the pattern. Any changes made in one alternative do carry on
820  into subsequent branches within the same subpattern. For example,  into subsequent branches within the same subpattern. For example,
 </P>  
 <P>  
821  <pre>  <pre>
822    (a(?i)b|c)    (a(?i)b|c)
823  </PRE>  </pre>
 </P>  
 <P>  
824  matches "ab", "aB", "c", and "C", even though when matching "C" the first  matches "ab", "aB", "c", and "C", even though when matching "C" the first
825  branch is abandoned before the option setting. This is because the effects of  branch is abandoned before the option setting. This is because the effects of
826  option settings happen at compile time. There would be some very weird  option settings happen at compile time. There would be some very weird
827  behaviour otherwise.  behaviour otherwise.
828  </P>  </P>
829  <P>  <P>
830  The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changed in the  The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
831  same way as the Perl-compatible options by using the characters U and X  changed in the same way as the Perl-compatible options by using the characters
832  respectively. The (?X) flag setting is special in that it must always occur  J, U and X respectively.
833  earlier in the pattern than any of the additional features it turns on, even  <a name="subpattern"></a></P>
 when it is at top level. It is best put at the start.  
 </P>  
834  <br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br>  <br><a name="SEC10" href="#TOC1">SUBPATTERNS</a><br>
835  <P>  <P>
836  Subpatterns are delimited by parentheses (round brackets), which can be nested.  Subpatterns are delimited by parentheses (round brackets), which can be nested.
837  Marking part of a pattern as a subpattern does two things:  Turning part of a pattern into a subpattern does two things:
838  </P>  <br>
839  <P>  <br>
840  1. It localizes a set of alternatives. For example, the pattern  1. It localizes a set of alternatives. For example, the pattern
 </P>  
 <P>  
841  <pre>  <pre>
842    cat(aract|erpillar|)    cat(aract|erpillar|)
843  </PRE>  </pre>
 </P>  
 <P>  
844  matches one of the words "cat", "cataract", or "caterpillar". Without the  matches one of the words "cat", "cataract", or "caterpillar". Without the
845  parentheses, it would match "cataract", "erpillar" or the empty string.  parentheses, it would match "cataract", "erpillar" or the empty string.
846  </P>  <br>
847  <P>  <br>
848  2. It sets up the subpattern as a capturing subpattern (as defined above).  2. It sets up the subpattern as a capturing subpattern. This means that, when
849  When the whole pattern matches, that portion of the subject string that matched  the whole pattern matches, that portion of the subject string that matched the
850  the 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
851  <b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting  <b>pcre_exec()</b>. Opening parentheses are counted from left to right (starting
852  from 1) to obtain the numbers of the capturing subpatterns.  from 1) to obtain numbers for the capturing subpatterns.
853  </P>  </P>
854  <P>  <P>
855  For example, if the string "the red king" is matched against the pattern  For example, if the string "the red king" is matched against the pattern
 </P>  
 <P>  
856  <pre>  <pre>
857    the ((red|white) (king|queen))    the ((red|white) (king|queen))
858  </PRE>  </pre>
 </P>  
 <P>  
859  the captured substrings are "red king", "red", and "king", and are numbered 1,  the captured substrings are "red king", "red", and "king", and are numbered 1,
860  2, and 3, respectively.  2, and 3, respectively.
861  </P>  </P>
# Line 681  capturing requirement. If an opening par Line 866  capturing requirement. If an opening par
866  and a colon, the subpattern does not do any capturing, and is not counted when  and a colon, the subpattern does not do any capturing, and is not counted when
867  computing the number of any subsequent capturing subpatterns. For example, if  computing the number of any subsequent capturing subpatterns. For example, if
868  the string "the white queen" is matched against the pattern  the string "the white queen" is matched against the pattern
 </P>  
 <P>  
869  <pre>  <pre>
870    the ((?:red|white) (king|queen))    the ((?:red|white) (king|queen))
871  </PRE>  </pre>
 </P>  
 <P>  
872  the captured substrings are "white queen" and "queen", and are numbered 1 and  the captured substrings are "white queen" and "queen", and are numbered 1 and
873  2. The maximum number of capturing subpatterns is 65535, and the maximum depth  2. The maximum number of capturing subpatterns is 65535, and the maximum depth
874  of nesting of all subpatterns, both capturing and non-capturing, is 200.  of nesting of all subpatterns, both capturing and non-capturing, is 200.
# Line 696  of nesting of all subpatterns, both capt Line 877  of nesting of all subpatterns, both capt
877  As a convenient shorthand, if any option settings are required at the start of  As a convenient shorthand, if any option settings are required at the start of
878  a non-capturing subpattern, the option letters may appear between the "?" and  a non-capturing subpattern, the option letters may appear between the "?" and
879  the ":". Thus the two patterns  the ":". Thus the two patterns
 </P>  
 <P>  
880  <pre>  <pre>
881    (?i:saturday|sunday)    (?i:saturday|sunday)
882    (?:(?i)saturday|sunday)    (?:(?i)saturday|sunday)
883  </PRE>  </pre>
 </P>  
 <P>  
884  match exactly the same set of strings. Because alternative branches are tried  match exactly the same set of strings. Because alternative branches are tried
885  from left to right, and options are not reset until the end of the subpattern  from left to right, and options are not reset until the end of the subpattern
886  is reached, an option setting in one branch does affect subsequent branches, so  is reached, an option setting in one branch does affect subsequent branches, so
# Line 713  the above patterns match "SUNDAY" as wel Line 890  the above patterns match "SUNDAY" as wel
890  <P>  <P>
891  Identifying capturing parentheses by number is simple, but it can be very hard  Identifying capturing parentheses by number is simple, but it can be very hard
892  to keep track of the numbers in complicated regular expressions. Furthermore,  to keep track of the numbers in complicated regular expressions. Furthermore,
893  if an expression is modified, the numbers may change. To help with the  if an expression is modified, the numbers may change. To help with this
894  difficulty, PCRE supports the naming of subpatterns, something that Perl does  difficulty, PCRE supports the naming of subpatterns, something that Perl does
895  not provide. The Python syntax (?P&#60;name&#62;...) is used. Names consist of  not provide. The Python syntax (?P&#60;name&#62;...) is used. References to capturing
896  alphanumeric characters and underscores, and must be unique within a pattern.  parentheses from other parts of the pattern, such as
897  </P>  <a href="#backreferences">backreferences,</a>
898  <P>  <a href="#recursion">recursion,</a>
899  Named capturing parentheses are still allocated numbers as well as names. The  and
900  PCRE API provides function calls for extracting the name-to-number translation  <a href="#conditions">conditions,</a>
901  table from a compiled pattern. For further details see the  can be made by name as well as by number.
902    </P>
903    <P>
904    Names consist of up to 32 alphanumeric characters and underscores. Named
905    capturing parentheses are still allocated numbers as well as names. The PCRE
906    API provides function calls for extracting the name-to-number translation table
907    from a compiled pattern. There is also a convenience function for extracting a
908    captured substring by name.
909    </P>
910    <P>
911    By default, a name must be unique within a pattern, but it is possible to relax
912    this constraint by setting the PCRE_DUPNAMES option at compile time. This can
913    be useful for patterns where only one instance of the named parentheses can
914    match. Suppose you want to match the name of a weekday, either as a 3-letter
915    abbreviation or as the full name, and in both cases you want to extract the
916    abbreviation. This pattern (ignoring the line breaks) does the job:
917    <pre>
918      (?P&#60;DN&#62;Mon|Fri|Sun)(?:day)?|
919      (?P&#60;DN&#62;Tue)(?:sday)?|
920      (?P&#60;DN&#62;Wed)(?:nesday)?|
921      (?P&#60;DN&#62;Thu)(?:rsday)?|
922      (?P&#60;DN&#62;Sat)(?:urday)?
923    </pre>
924    There are five capturing substrings, but only one is ever set after a match.
925    The convenience function for extracting the data by name returns the substring
926    for the first, and in this example, the only, subpattern of that name that
927    matched. This saves searching to find which numbered subpattern it was. If you
928    make a reference to a non-unique named subpattern from elsewhere in the
929    pattern, the one that corresponds to the lowest number is used. For further
930    details of the interfaces for handling named subpatterns, see the
931  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
932  documentation.  documentation.
933  </P>  </P>
# Line 729  documentation. Line 935  documentation.
935  <P>  <P>
936  Repetition is specified by quantifiers, which can follow any of the following  Repetition is specified by quantifiers, which can follow any of the following
937  items:  items:
 </P>  
 <P>  
938  <pre>  <pre>
939    a literal data character    a literal data character
940    the . metacharacter    the . metacharacter
941    the \C escape sequence    the \C escape sequence
942    escapes such as \d that match single characters    the \X escape sequence (in UTF-8 mode with Unicode properties)
943      an escape such as \d that matches a single character
944    a character class    a character class
945    a back reference (see next section)    a back reference (see next section)
946    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (unless it is an assertion)
947  </PRE>  </pre>
 </P>  
 <P>  
948  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
949  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
950  separated by a comma. The numbers must be less than 65536, and the first must  separated by a comma. The numbers must be less than 65536, and the first must
951  be less than or equal to the second. For example:  be less than or equal to the second. For example:
 </P>  
 <P>  
952  <pre>  <pre>
953    z{2,4}    z{2,4}
954  </PRE>  </pre>
 </P>  
 <P>  
955  matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special  matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special
956  character. If the second number is omitted, but the comma is present, there is  character. If the second number is omitted, but the comma is present, there is
957  no upper limit; if the second number and the comma are both omitted, the  no upper limit; if the second number and the comma are both omitted, the
958  quantifier specifies an exact number of required matches. Thus  quantifier specifies an exact number of required matches. Thus
 </P>  
 <P>  
959  <pre>  <pre>
960    [aeiou]{3,}    [aeiou]{3,}
961  </PRE>  </pre>
 </P>  
 <P>  
962  matches at least 3 successive vowels, but may match many more, while  matches at least 3 successive vowels, but may match many more, while
 </P>  
 <P>  
963  <pre>  <pre>
964    \d{8}    \d{8}
965  </PRE>  </pre>
 </P>  
 <P>  
966  matches exactly 8 digits. An opening curly bracket that appears in a position  matches exactly 8 digits. An opening curly bracket that appears in a position
967  where a quantifier is not allowed, or one that does not match the syntax of a  where a quantifier is not allowed, or one that does not match the syntax of a
968  quantifier, is taken as a literal character. For example, {,6} is not a  quantifier, is taken as a literal character. For example, {,6} is not a
# Line 780  quantifier, but a literal string of four Line 971  quantifier, but a literal string of four
971  <P>  <P>
972  In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual  In UTF-8 mode, quantifiers apply to UTF-8 characters rather than to individual
973  bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of  bytes. Thus, for example, \x{100}{2} matches two UTF-8 characters, each of
974  which is represented by a two-byte sequence.  which is represented by a two-byte sequence. Similarly, when Unicode property
975    support is available, \X{3} matches three Unicode extended sequences, each of
976    which may be several bytes long (and they may be of different lengths).
977  </P>  </P>
978  <P>  <P>
979  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
# Line 789  previous item and the quantifier were no Line 982  previous item and the quantifier were no
982  <P>  <P>
983  For convenience (and historical compatibility) the three most common  For convenience (and historical compatibility) the three most common
984  quantifiers have single-character abbreviations:  quantifiers have single-character abbreviations:
 </P>  
 <P>  
985  <pre>  <pre>
986    *    is equivalent to {0,}    *    is equivalent to {0,}
987    +    is equivalent to {1,}    +    is equivalent to {1,}
988    ?    is equivalent to {0,1}    ?    is equivalent to {0,1}
989  </PRE>  </pre>
 </P>  
 <P>  
990  It is possible to construct infinite loops by following a subpattern that can  It is possible to construct infinite loops by following a subpattern that can
991  match no characters with a quantifier that has no upper limit, for example:  match no characters with a quantifier that has no upper limit, for example:
 </P>  
 <P>  
992  <pre>  <pre>
993    (a?)*    (a?)*
994  </PRE>  </pre>
 </P>  
 <P>  
995  Earlier versions of Perl and PCRE used to give an error at compile time for  Earlier versions of Perl and PCRE used to give an error at compile time for
996  such patterns. However, because there are cases where this can be useful, such  such patterns. However, because there are cases where this can be useful, such
997  patterns are now accepted, but if any repetition of the subpattern does in fact  patterns are now accepted, but if any repetition of the subpattern does in fact
# Line 816  match no characters, the loop is forcibl Line 1001  match no characters, the loop is forcibl
1001  By default, the quantifiers are "greedy", that is, they match as much as  By default, the quantifiers are "greedy", that is, they match as much as
1002  possible (up to the maximum number of permitted times), without causing the  possible (up to the maximum number of permitted times), without causing the
1003  rest of the pattern to fail. The classic example of where this gives problems  rest of the pattern to fail. The classic example of where this gives problems
1004  is in trying to match comments in C programs. These appear between the  is in trying to match comments in C programs. These appear between /* and */
1005  sequences /* and */ and within the sequence, individual * and / characters may  and within the comment, individual * and / characters may appear. An attempt to
1006  appear. An attempt to match C comments by applying the pattern  match C comments by applying the pattern
 </P>  
 <P>  
1007  <pre>  <pre>
1008    /\*.*\*/    /\*.*\*/
1009  </PRE>  </pre>
 </P>  
 <P>  
1010  to the string  to the string
 </P>  
 <P>  
1011  <pre>  <pre>
1012    /* first command */  not comment  /* second comment */    /* first comment */  not comment  /* second comment */
1013  </PRE>  </pre>
 </P>  
 <P>  
1014  fails, because it matches the entire string owing to the greediness of the .*  fails, because it matches the entire string owing to the greediness of the .*
1015  item.  item.
1016  </P>  </P>
# Line 841  item. Line 1018  item.
1018  However, if a quantifier is followed by a question mark, it ceases to be  However, if a quantifier is followed by a question mark, it ceases to be
1019  greedy, and instead matches the minimum number of times possible, so the  greedy, and instead matches the minimum number of times possible, so the
1020  pattern  pattern
 </P>  
 <P>  
1021  <pre>  <pre>
1022    /\*.*?\*/    /\*.*?\*/
1023  </PRE>  </pre>
 </P>  
 <P>  
1024  does the right thing with the C comments. The meaning of the various  does the right thing with the C comments. The meaning of the various
1025  quantifiers is not otherwise changed, just the preferred number of matches.  quantifiers is not otherwise changed, just the preferred number of matches.
1026  Do not confuse this use of question mark with its use as a quantifier in its  Do not confuse this use of question mark with its use as a quantifier in its
1027  own right. Because it has two uses, it can sometimes appear doubled, as in  own right. Because it has two uses, it can sometimes appear doubled, as in
 </P>  
 <P>  
1028  <pre>  <pre>
1029    \d??\d    \d??\d
1030  </PRE>  </pre>
 </P>  
 <P>  
1031  which matches one digit by preference, but can match two if that is the only  which matches one digit by preference, but can match two if that is the only
1032  way the rest of the pattern matches.  way the rest of the pattern matches.
1033  </P>  </P>
# Line 870  default behaviour. Line 1039  default behaviour.
1039  </P>  </P>
1040  <P>  <P>
1041  When a parenthesized subpattern is quantified with a minimum repeat count that  When a parenthesized subpattern is quantified with a minimum repeat count that
1042  is greater than 1 or with a limited maximum, more store is required for the  is greater than 1 or with a limited maximum, more memory is required for the
1043  compiled pattern, in proportion to the size of the minimum or maximum.  compiled pattern, in proportion to the size of the minimum or maximum.
1044  </P>  </P>
1045  <P>  <P>
# Line 891  However, there is one situation where th Line 1060  However, there is one situation where th
1060  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a backreference
1061  elsewhere in the pattern, a match at the start may fail, and a later one  elsewhere in the pattern, a match at the start may fail, and a later one
1062  succeed. Consider, for example:  succeed. Consider, for example:
 </P>  
 <P>  
1063  <pre>  <pre>
1064    (.*)abc\1    (.*)abc\1
1065  </PRE>  </pre>
 </P>  
 <P>  
1066  If the subject is "xyz123abc123" the match point is the fourth character. For  If the subject is "xyz123abc123" the match point is the fourth character. For
1067  this reason, such a pattern is not implicitly anchored.  this reason, such a pattern is not implicitly anchored.
1068  </P>  </P>
1069  <P>  <P>
1070  When a capturing subpattern is repeated, the value captured is the substring  When a capturing subpattern is repeated, the value captured is the substring
1071  that matched the final iteration. For example, after  that matched the final iteration. For example, after
 </P>  
 <P>  
1072  <pre>  <pre>
1073    (tweedle[dume]{3}\s*)+    (tweedle[dume]{3}\s*)+
1074  </PRE>  </pre>
 </P>  
 <P>  
1075  has matched "tweedledum tweedledee" the value of the captured substring is  has matched "tweedledum tweedledee" the value of the captured substring is
1076  "tweedledee". However, if there are nested capturing subpatterns, the  "tweedledee". However, if there are nested capturing subpatterns, the
1077  corresponding captured values may have been set in previous iterations. For  corresponding captured values may have been set in previous iterations. For
1078  example, after  example, after
 </P>  
 <P>  
1079  <pre>  <pre>
1080    /(a|(b))+/    /(a|(b))+/
1081  </PRE>  </pre>
 </P>  
 <P>  
1082  matches "aba" the value of the second captured substring is "b".  matches "aba" the value of the second captured substring is "b".
1083  </P>  <a name="atomicgroup"></a></P>
1084  <br><a name="SEC13" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>  <br><a name="SEC13" href="#TOC1">ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS</a><br>
1085  <P>  <P>
1086  With both maximizing and minimizing repetition, failure of what follows  With both maximizing and minimizing repetition, failure of what follows
# Line 935  there is no point in carrying on. Line 1092  there is no point in carrying on.
1092  </P>  </P>
1093  <P>  <P>
1094  Consider, for example, the pattern \d+foo when applied to the subject line  Consider, for example, the pattern \d+foo when applied to the subject line
 </P>  
 <P>  
1095  <pre>  <pre>
1096    123456bar    123456bar
1097  </PRE>  </pre>
 </P>  
 <P>  
1098  After matching all 6 digits and then failing to match "foo", the normal  After matching all 6 digits and then failing to match "foo", the normal
1099  action of the matcher is to try again with only 5 digits matching the \d+  action of the matcher is to try again with only 5 digits matching the \d+
1100  item, and then with 4, and so on, before ultimately failing. "Atomic grouping"  item, and then with 4, and so on, before ultimately failing. "Atomic grouping"
# Line 952  that once a subpattern has matched, it i Line 1105  that once a subpattern has matched, it i
1105  If we use atomic grouping for the previous example, the matcher would give up  If we use atomic grouping for the previous example, the matcher would give up
1106  immediately on failing to match "foo" the first time. The notation is a kind of  immediately on failing to match "foo" the first time. The notation is a kind of
1107  special parenthesis, starting with (?&#62; as in this example:  special parenthesis, starting with (?&#62; as in this example:
 </P>  
 <P>  
1108  <pre>  <pre>
1109    (?&#62;\d+)foo    (?&#62;\d+)foo
1110  </PRE>  </pre>
 </P>  
 <P>  
1111  This kind of parenthesis "locks up" the  part of the pattern it contains once  This kind of parenthesis "locks up" the  part of the pattern it contains once
1112  it has matched, and a failure further into the pattern is prevented from  it has matched, and a failure further into the pattern is prevented from
1113  backtracking into it. Backtracking past it to previous items, however, works as  backtracking into it. Backtracking past it to previous items, however, works as
# Line 983  group is just a single repeated item, as Line 1132  group is just a single repeated item, as
1132  notation, called a "possessive quantifier" can be used. This consists of an  notation, called a "possessive quantifier" can be used. This consists of an
1133  additional + character following a quantifier. Using this notation, the  additional + character following a quantifier. Using this notation, the
1134  previous example can be rewritten as  previous example can be rewritten as
 </P>  
 <P>  
1135  <pre>  <pre>
1136    \d++bar    \d++foo
1137  </PRE>  </pre>
 </P>  
 <P>  
1138  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1139  option is ignored. They are a convenient notation for the simpler forms of  option is ignored. They are a convenient notation for the simpler forms of
1140  atomic group. However, there is no difference in the meaning or processing of a  atomic group. However, there is no difference in the meaning or processing of a
1141  possessive quantifier and the equivalent atomic group.  possessive quantifier and the equivalent atomic group.
1142  </P>  </P>
1143  <P>  <P>
1144  The possessive quantifier syntax is an extension to the Perl syntax. It  The possessive quantifier syntax is an extension to the Perl syntax. Jeffrey
1145  originates in Sun's Java package.  Friedl originated the idea (and the name) in the first edition of his book.
1146    Mike McCloskey liked it, so implemented it when he built Sun's Java package,
1147    and PCRE copied it from there.
1148  </P>  </P>
1149  <P>  <P>
1150  When a pattern contains an unlimited repeat inside a subpattern that can itself  When a pattern contains an unlimited repeat inside a subpattern that can itself
1151  be repeated an unlimited number of times, the use of an atomic group is the  be repeated an unlimited number of times, the use of an atomic group is the
1152  only way to avoid some failing matches taking a very long time indeed. The  only way to avoid some failing matches taking a very long time indeed. The
1153  pattern  pattern
 </P>  
 <P>  
1154  <pre>  <pre>
1155    (\D+|&#60;\d+&#62;)*[!?]    (\D+|&#60;\d+&#62;)*[!?]
1156  </PRE>  </pre>
 </P>  
 <P>  
1157  matches an unlimited number of substrings that either consist of non-digits, or  matches an unlimited number of substrings that either consist of non-digits, or
1158  digits enclosed in &#60;&#62;, followed by either ! or ?. When it matches, it runs  digits enclosed in &#60;&#62;, followed by either ! or ?. When it matches, it runs
1159  quickly. However, if it is applied to  quickly. However, if it is applied to
 </P>  
 <P>  
1160  <pre>  <pre>
1161    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1162  </PRE>  </pre>
 </P>  
 <P>  
1163  it takes a long time before reporting failure. This is because the string can  it takes a long time before reporting failure. This is because the string can
1164  be divided between the two repeats in a large number of ways, and all have to  be divided between the internal \D+ repeat and the external * repeat in a
1165  be tried. (The example used [!?] rather than a single character at the end,  large number of ways, and all have to be tried. (The example uses [!?] rather
1166  because both PCRE and Perl have an optimization that allows for fast failure  than a single character at the end, because both PCRE and Perl have an
1167  when a single character is used. They remember the last single character that  optimization that allows for fast failure when a single character is used. They
1168  is required for a match, and fail early if it is not present in the string.)  remember the last single character that is required for a match, and fail early
1169  If the pattern is changed to  if it is not present in the string.) If the pattern is changed so that it uses
1170  </P>  an atomic group, like this:
 <P>  
1171  <pre>  <pre>
1172    ((?&#62;\D+)|&#60;\d+&#62;)*[!?]    ((?&#62;\D+)|&#60;\d+&#62;)*[!?]
1173  </PRE>  </pre>
 </P>  
 <P>  
1174  sequences of non-digits cannot be broken, and failure happens quickly.  sequences of non-digits cannot be broken, and failure happens quickly.
1175  </P>  <a name="backreferences"></a></P>
1176  <br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br>  <br><a name="SEC14" href="#TOC1">BACK REFERENCES</a><br>
1177  <P>  <P>
1178  Outside a character class, a backslash followed by a digit greater than 0 (and  Outside a character class, a backslash followed by a digit greater than 0 (and
# Line 1049  However, if the decimal number following Line 1185  However, if the decimal number following
1185  always taken as a back reference, and causes an error only if there are not  always taken as a back reference, and causes an error only if there are not
1186  that many capturing left parentheses in the entire pattern. In other words, the  that many capturing left parentheses in the entire pattern. In other words, the
1187  parentheses that are referenced need not be to the left of the reference for  parentheses that are referenced need not be to the left of the reference for
1188  numbers less than 10. See the section entitled "Backslash" above for further  numbers less than 10. A "forward back reference" of this type can make sense
1189  details of the handling of digits following a backslash.  when a repetition is involved and the subpattern to the right has participated
1190    in an earlier iteration.
1191    </P>
1192    <P>
1193    It is not possible to have a numerical "forward back reference" to subpattern
1194    whose number is 10 or more. However, a back reference to any subpattern is
1195    possible using named parentheses (see below). See also the subsection entitled
1196    "Non-printing characters"
1197    <a href="#digitsafterbackslash">above</a>
1198    for further details of the handling of digits following a backslash.
1199  </P>  </P>
1200  <P>  <P>
1201  A back reference matches whatever actually matched the capturing subpattern in  A back reference matches whatever actually matched the capturing subpattern in
# Line 1058  the current subject string, rather than Line 1203  the current subject string, rather than
1203  itself (see  itself (see
1204  <a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a>  <a href="#subpatternsassubroutines">"Subpatterns as subroutines"</a>
1205  below for a way of doing that). So the pattern  below for a way of doing that). So the pattern
 </P>  
 <P>  
1206  <pre>  <pre>
1207    (sens|respons)e and \1ibility    (sens|respons)e and \1ibility
1208  </PRE>  </pre>
 </P>  
 <P>  
1209  matches "sense and sensibility" and "response and responsibility", but not  matches "sense and sensibility" and "response and responsibility", but not
1210  "sense and responsibility". If caseful matching is in force at the time of the  "sense and responsibility". If caseful matching is in force at the time of the
1211  back reference, the case of letters is relevant. For example,  back reference, the case of letters is relevant. For example,
 </P>  
 <P>  
1212  <pre>  <pre>
1213    ((?i)rah)\s+\1    ((?i)rah)\s+\1
1214  </PRE>  </pre>
 </P>  
 <P>  
1215  matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original  matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original
1216  capturing subpattern is matched caselessly.  capturing subpattern is matched caselessly.
1217  </P>  </P>
1218  <P>  <P>
1219  Back references to named subpatterns use the Python syntax (?P=name). We could  Back references to named subpatterns use the Python syntax (?P=name). We could
1220  rewrite the above example as follows:  rewrite the above example as follows:
 </P>  
 <P>  
1221  <pre>  <pre>
1222    (?&#60;p1&#62;(?i)rah)\s+(?P=p1)    (?P&#60;p1&#62;(?i)rah)\s+(?P=p1)
1223  </PRE>  </pre>
1224    A subpattern that is referenced by name may appear in the pattern before or
1225    after the reference.
1226  </P>  </P>
1227  <P>  <P>
1228  There may be more than one back reference to the same subpattern. If a  There may be more than one back reference to the same subpattern. If a
1229  subpattern has not actually been used in a particular match, any back  subpattern has not actually been used in a particular match, any back
1230  references to it always fail. For example, the pattern  references to it always fail. For example, the pattern
 </P>  
 <P>  
1231  <pre>  <pre>
1232    (a|(bc))\2    (a|(bc))\2
1233  </PRE>  </pre>
 </P>  
 <P>  
1234  always fails if it starts to match "a" rather than "bc". Because there may be  always fails if it starts to match "a" rather than "bc". Because there may be
1235  many capturing parentheses in a pattern, all digits following the backslash are  many capturing parentheses in a pattern, all digits following the backslash are
1236  taken as part of a potential back reference number. If the pattern continues  taken as part of a potential back reference number. If the pattern continues
1237  with a digit character, some delimiter must be used to terminate the back  with a digit character, some delimiter must be used to terminate the back
1238  reference. If the PCRE_EXTENDED option is set, this can be whitespace.  reference. If the PCRE_EXTENDED option is set, this can be whitespace.
1239  Otherwise an empty comment can be used.  Otherwise an empty comment (see
1240    <a href="#comments">"Comments"</a>
1241    below) can be used.
1242  </P>  </P>
1243  <P>  <P>
1244  A back reference that occurs inside the parentheses to which it refers fails  A back reference that occurs inside the parentheses to which it refers fails
1245  when the subpattern is first used, so, for example, (a\1) never matches.  when the subpattern is first used, so, for example, (a\1) never matches.
1246  However, such references can be useful inside repeated subpatterns. For  However, such references can be useful inside repeated subpatterns. For
1247  example, the pattern  example, the pattern
 </P>  
 <P>  
1248  <pre>  <pre>
1249    (a|b\1)+    (a|b\1)+
1250  </PRE>  </pre>
 </P>  
 <P>  
1251  matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of  matches any number of "a"s and also "aba", "ababbaa" etc. At each iteration of
1252  the subpattern, the back reference matches the character string corresponding  the subpattern, the back reference matches the character string corresponding
1253  to the previous iteration. In order for this to work, the pattern must be such  to the previous iteration. In order for this to work, the pattern must be such
1254  that the first iteration does not need to match the back reference. This can be  that the first iteration does not need to match the back reference. This can be
1255  done using alternation, as in the example above, or by a quantifier with a  done using alternation, as in the example above, or by a quantifier with a
1256  minimum of zero.  minimum of zero.
1257  </P>  <a name="bigassertions"></a></P>
1258  <br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br>  <br><a name="SEC15" href="#TOC1">ASSERTIONS</a><br>
1259  <P>  <P>
1260  An assertion is a test on the characters following or preceding the current  An assertion is a test on the characters following or preceding the current
1261  matching point that does not actually consume any characters. The simple  matching point that does not actually consume any characters. The simple
1262  assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described above.  assertions coded as \b, \B, \A, \G, \Z, \z, ^ and $ are described
1263    <a href="#smallassertions">above.</a>
1264    </P>
1265    <P>
1266  More complicated assertions are coded as subpatterns. There are two kinds:  More complicated assertions are coded as subpatterns. There are two kinds:
1267  those that look ahead of the current position in the subject string, and those  those that look ahead of the current position in the subject string, and those
1268  that look behind it.  that look behind it. An assertion subpattern is matched in the normal way,
1269    except that it does not cause the current matching position to be changed.
1270  </P>  </P>
1271  <P>  <P>
1272  An assertion subpattern is matched in the normal way, except that it does not  Assertion subpatterns are not capturing subpatterns, and may not be repeated,
1273  cause the current matching position to be changed. Lookahead assertions start  because it makes no sense to assert the same thing several times. If any kind
1274  with (?= for positive assertions and (?! for negative assertions. For example,  of assertion contains capturing subpatterns within it, these are counted for
1275    the purposes of numbering the capturing subpatterns in the whole pattern.
1276    However, substring capturing is carried out only for positive assertions,
1277    because it does not make sense for negative assertions.
1278  </P>  </P>
1279    <br><b>
1280    Lookahead assertions
1281    </b><br>
1282  <P>  <P>
1283    Lookahead assertions start with (?= for positive assertions and (?! for
1284    negative assertions. For example,
1285  <pre>  <pre>
1286    \w+(?=;)    \w+(?=;)
1287  </PRE>  </pre>
 </P>  
 <P>  
1288  matches a word followed by a semicolon, but does not include the semicolon in  matches a word followed by a semicolon, but does not include the semicolon in
1289  the match, and  the match, and
 </P>  
 <P>  
1290  <pre>  <pre>
1291    foo(?!bar)    foo(?!bar)
1292  </PRE>  </pre>
 </P>  
 <P>  
1293  matches any occurrence of "foo" that is not followed by "bar". Note that the  matches any occurrence of "foo" that is not followed by "bar". Note that the
1294  apparently similar pattern  apparently similar pattern
 </P>  
 <P>  
1295  <pre>  <pre>
1296    (?!foo)bar    (?!foo)bar
1297  </PRE>  </pre>
 </P>  
 <P>  
1298  does not find an occurrence of "bar" that is preceded by something other than  does not find an occurrence of "bar" that is preceded by something other than
1299  "foo"; it finds any occurrence of "bar" whatsoever, because the assertion  "foo"; it finds any occurrence of "bar" whatsoever, because the assertion
1300  (?!foo) is always true when the next three characters are "bar". A  (?!foo) is always true when the next three characters are "bar". A
1301  lookbehind assertion is needed to achieve this effect.  lookbehind assertion is needed to achieve the other effect.
1302  </P>  </P>
1303  <P>  <P>
1304  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
1305  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
1306  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.
1307  </P>  <a name="lookbehind"></a></P>
1308    <br><b>
1309    Lookbehind assertions
1310    </b><br>
1311  <P>  <P>
1312  Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for  Lookbehind assertions start with (?&#60;= for positive assertions and (?&#60;! for
1313  negative assertions. For example,  negative assertions. For example,
 </P>  
 <P>  
1314  <pre>  <pre>
1315    (?&#60;!foo)bar    (?&#60;!foo)bar
1316  </PRE>  </pre>
 </P>  
 <P>  
1317  does find an occurrence of "bar" that is not preceded by "foo". The contents of  does find an occurrence of "bar" that is not preceded by "foo". The contents of
1318  a lookbehind assertion are restricted such that all the strings it matches must  a lookbehind assertion are restricted such that all the strings it matches must
1319  have a fixed length. However, if there are several alternatives, they do not  have a fixed length. However, if there are several top-level alternatives, they
1320  all have to have the same fixed length. Thus  do not all have to have the same fixed length. Thus
 </P>  
 <P>  
1321  <pre>  <pre>
1322    (?&#60;=bullock|donkey)    (?&#60;=bullock|donkey)
1323  </PRE>  </pre>
 </P>  
 <P>  
1324  is permitted, but  is permitted, but
 </P>  
 <P>  
1325  <pre>  <pre>
1326    (?&#60;!dogs?|cats?)    (?&#60;!dogs?|cats?)
1327  </PRE>  </pre>
 </P>  
 <P>  
1328  causes an error at compile time. Branches that match different length strings  causes an error at compile time. Branches that match different length strings
1329  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
1330  extension compared with Perl (at least for 5.8), which requires all branches to  extension compared with Perl (at least for 5.8), which requires all branches to
1331  match the same length of string. An assertion such as  match the same length of string. An assertion such as
 </P>  
 <P>  
1332  <pre>  <pre>
1333    (?&#60;=ab(c|de))    (?&#60;=ab(c|de))
1334  </PRE>  </pre>
 </P>  
 <P>  
1335  is not permitted, because its single top-level branch can match two different  is not permitted, because its single top-level branch can match two different
1336  lengths, but it is acceptable if rewritten to use two top-level branches:  lengths, but it is acceptable if rewritten to use two top-level branches:
 </P>  
 <P>  
1337  <pre>  <pre>
1338    (?&#60;=abc|abde)    (?&#60;=abc|abde)
1339  </PRE>  </pre>
 </P>  
 <P>  
1340  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1341  temporarily move the current position back by the fixed width and then try to  temporarily move the current position back by the fixed width and then try to
1342  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
# Line 1229  match is deemed to fail. Line 1345  match is deemed to fail.
1345  <P>  <P>
1346  PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)  PCRE does not allow the \C escape (which matches a single byte in UTF-8 mode)
1347  to appear in lookbehind assertions, because it makes it impossible to calculate  to appear in lookbehind assertions, because it makes it impossible to calculate
1348  the length of the lookbehind.  the length of the lookbehind. The \X escape, which can match different numbers
1349    of bytes, is also not permitted.
1350  </P>  </P>
1351  <P>  <P>
1352  Atomic groups can be used in conjunction with lookbehind assertions to specify  Atomic groups can be used in conjunction with lookbehind assertions to specify
1353  efficient matching at the end of the subject string. Consider a simple pattern  efficient matching at the end of the subject string. Consider a simple pattern
1354  such as  such as
 </P>  
 <P>  
1355  <pre>  <pre>
1356    abcd$    abcd$
1357  </PRE>  </pre>
 </P>  
 <P>  
1358  when applied to a long string that does not match. Because matching proceeds  when applied to a long string that does not match. Because matching proceeds
1359  from left to right, PCRE will look for each "a" in the subject and then see if  from left to right, PCRE will look for each "a" in the subject and then see if
1360  what follows matches the rest of the pattern. If the pattern is specified as  what follows matches the rest of the pattern. If the pattern is specified as
 </P>  
 <P>  
1361  <pre>  <pre>
1362    ^.*abcd$    ^.*abcd$
1363  </PRE>  </pre>
 </P>  
 <P>  
1364  the initial .* matches the entire string at first, but when this fails (because  the initial .* matches the entire string at first, but when this fails (because
1365  there is no following "a"), it backtracks to match all but the last character,  there is no following "a"), it backtracks to match all but the last character,
1366  then all but the last two characters, and so on. Once again the search for "a"  then all but the last two characters, and so on. Once again the search for "a"
1367  covers the entire string, from right to left, so we are no better off. However,  covers the entire string, from right to left, so we are no better off. However,
1368  if the pattern is written as  if the pattern is written as
 </P>  
 <P>  
1369  <pre>  <pre>
1370    ^(?&#62;.*)(?&#60;=abcd)    ^(?&#62;.*)(?&#60;=abcd)
1371  </PRE>  </pre>
1372  </P>  or, equivalently, using the possessive quantifier syntax,
 <P>  
 or, equivalently,  
 </P>  
 <P>  
1373  <pre>  <pre>
1374    ^.*+(?&#60;=abcd)    ^.*+(?&#60;=abcd)
1375  </PRE>  </pre>
 </P>  
 <P>  
1376  there can be no backtracking for the .* item; it can match only the entire  there can be no backtracking for the .* item; it can match only the entire
1377  string. The subsequent lookbehind assertion does a single test on the last four  string. The subsequent lookbehind assertion does a single test on the last four
1378  characters. If it fails, the match fails immediately. For long strings, this  characters. If it fails, the match fails immediately. For long strings, this
1379  approach makes a significant difference to the processing time.  approach makes a significant difference to the processing time.
1380  </P>  </P>
1381    <br><b>
1382    Using multiple assertions
1383    </b><br>
1384  <P>  <P>
1385  Several assertions (of any sort) may occur in succession. For example,  Several assertions (of any sort) may occur in succession. For example,
 </P>  
 <P>  
1386  <pre>  <pre>
1387    (?&#60;=\d{3})(?&#60;!999)foo    (?&#60;=\d{3})(?&#60;!999)foo
1388  </PRE>  </pre>
 </P>  
 <P>  
1389  matches "foo" preceded by three digits that are not "999". Notice that each of  matches "foo" preceded by three digits that are not "999". Notice that each of
1390  the assertions is applied independently at the same point in the subject  the assertions is applied independently at the same point in the subject
1391  string. First there is a check that the previous three characters are all  string. First there is a check that the previous three characters are all
# Line 1293  digits, and then there is a check that t Line 1393  digits, and then there is a check that t
1393  This pattern does <i>not</i> match "foo" preceded by six characters, the first  This pattern does <i>not</i> match "foo" preceded by six characters, the first
1394  of which are digits and the last three of which are not "999". For example, it  of which are digits and the last three of which are not "999". For example, it
1395  doesn't match "123abcfoo". A pattern to do that is  doesn't match "123abcfoo". A pattern to do that is
 </P>  
 <P>  
1396  <pre>  <pre>
1397    (?&#60;=\d{3}...)(?&#60;!999)foo    (?&#60;=\d{3}...)(?&#60;!999)foo
1398  </PRE>  </pre>
 </P>  
 <P>  
1399  This time the first assertion looks at the preceding six characters, checking  This time the first assertion looks at the preceding six characters, checking
1400  that the first three are digits, and then the second assertion checks that the  that the first three are digits, and then the second assertion checks that the
1401  preceding three characters are not "999".  preceding three characters are not "999".
1402  </P>  </P>
1403  <P>  <P>
1404  Assertions can be nested in any combination. For example,  Assertions can be nested in any combination. For example,
 </P>  
 <P>  
1405  <pre>  <pre>
1406    (?&#60;=(?&#60;!foo)bar)baz    (?&#60;=(?&#60;!foo)bar)baz
1407  </PRE>  </pre>
 </P>  
 <P>  
1408  matches an occurrence of "baz" that is preceded by "bar" which in turn is not  matches an occurrence of "baz" that is preceded by "bar" which in turn is not
1409  preceded by "foo", while  preceded by "foo", while
 </P>  
 <P>  
1410  <pre>  <pre>
1411    (?&#60;=\d{3}(?!999)...)foo    (?&#60;=\d{3}(?!999)...)foo
1412  </PRE>  </pre>
1413  </P>  is another pattern that matches "foo" preceded by three digits and any three
 <P>  
 is another pattern which matches "foo" preceded by three digits and any three  
1414  characters that are not "999".  characters that are not "999".
1415  </P>  <a name="conditions"></a></P>
 <P>  
 Assertion subpatterns are not capturing subpatterns, and may not be repeated,  
 because it makes no sense to assert the same thing several times. If any kind  
 of assertion contains capturing subpatterns within it, these are counted for  
 the purposes of numbering the capturing subpatterns in the whole pattern.  
 However, substring capturing is carried out only for positive assertions,  
 because it does not make sense for negative assertions.  
 </P>  
1416  <br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>  <br><a name="SEC16" href="#TOC1">CONDITIONAL SUBPATTERNS</a><br>
1417  <P>  <P>
1418  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1419  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
1420  the result of an assertion, or whether a previous capturing subpattern matched  the result of an assertion, or whether a previous capturing subpattern matched
1421  or not. The two possible forms of conditional subpattern are  or not. The two possible forms of conditional subpattern are
 </P>  
 <P>  
1422  <pre>  <pre>
1423    (?(condition)yes-pattern)    (?(condition)yes-pattern)
1424    (?(condition)yes-pattern|no-pattern)    (?(condition)yes-pattern|no-pattern)
1425  </PRE>  </pre>
 </P>  
 <P>  
1426  If the condition is satisfied, the yes-pattern is used; otherwise the  If the condition is satisfied, the yes-pattern is used; otherwise the
1427  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
1428  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs.
1429  </P>  </P>
1430  <P>  <P>
1431  There are three kinds of condition. If the text between the parentheses  There are three kinds of condition. If the text between the parentheses
1432  consists of a sequence of digits, the condition is satisfied if the capturing  consists of a sequence of digits, or a sequence of alphanumeric characters and
1433  subpattern of that number has previously matched. The number must be greater  underscores, the condition is satisfied if the capturing subpattern of that
1434  than zero. Consider the following pattern, which contains non-significant white  number or name has previously matched. There is a possible ambiguity here,
1435  space to make it more readable (assume the PCRE_EXTENDED option) and to divide  because subpattern names may consist entirely of digits. PCRE looks first for a
1436  it into three parts for ease of discussion:  named subpattern; if it cannot find one and the text consists entirely of
1437    digits, it looks for a subpattern of that number, which must be greater than
1438    zero. Using subpattern names that consist entirely of digits is not
1439    recommended.
1440  </P>  </P>
1441  <P>  <P>
1442    Consider the following pattern, which contains non-significant white space to
1443    make it more readable (assume the PCRE_EXTENDED option) and to divide it into
1444    three parts for ease of discussion:
1445  <pre>  <pre>
1446    ( \( )?    [^()]+    (?(1) \) )    ( \( )?    [^()]+    (?(1) \) )
1447  </PRE>  </pre>
 </P>  
 <P>  
1448  The first part matches an optional opening parenthesis, and if that  The first part matches an optional opening parenthesis, and if that
1449  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
1450  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
# Line 1373  or not. If they did, that is, if subject Line 1453  or not. If they did, that is, if subject
1453  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
1454  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
1455  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1456  non-parentheses, optionally enclosed in parentheses.  non-parentheses, optionally enclosed in parentheses. Rewriting it to use a
1457  </P>  named subpattern gives this:
1458  <P>  <pre>
1459  If the condition is the string (R), it is satisfied if a recursive call to the    (?P&#60;OPEN&#62; \( )?    [^()]+    (?(OPEN) \) )
1460  pattern or subpattern has been made. At "top level", the condition is false.  </pre>
1461  This is a PCRE extension. Recursive patterns are described in the next section.  If the condition is the string (R), and there is no subpattern with the name R,
1462    the condition is satisfied if a recursive call to the pattern or subpattern has
1463    been made. At "top level", the condition is false. This is a PCRE extension.
1464    Recursive patterns are described in the next section.
1465  </P>  </P>
1466  <P>  <P>
1467  If the condition is not a sequence of digits or (R), it must be an assertion.  If the condition is not a sequence of digits or (R), it must be an assertion.
1468  This may be a positive or negative lookahead or lookbehind assertion. Consider  This may be a positive or negative lookahead or lookbehind assertion. Consider
1469  this pattern, again containing non-significant white space, and with the two  this pattern, again containing non-significant white space, and with the two
1470  alternatives on the second line:  alternatives on the second line:
 </P>  
 <P>  
1471  <pre>  <pre>
1472    (?(?=[^a-z]*[a-z])    (?(?=[^a-z]*[a-z])
1473    \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )    \d{2}-[a-z]{3}-\d{2}  |  \d{2}-\d{2}-\d{2} )
1474  </PRE>  </pre>
 </P>  
 <P>  
1475  The condition is a positive lookahead assertion that matches an optional  The condition is a positive lookahead assertion that matches an optional
1476  sequence of non-letters followed by a letter. In other words, it tests for the  sequence of non-letters followed by a letter. In other words, it tests for the
1477  presence of at least one letter in the subject. If a letter is found, the  presence of at least one letter in the subject. If a letter is found, the
1478  subject is matched against the first alternative; otherwise it is matched  subject is matched against the first alternative; otherwise it is matched
1479  against the second. This pattern matches strings in one of the two forms  against the second. This pattern matches strings in one of the two forms
1480  dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.  dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits.
1481  </P>  <a name="comments"></a></P>
1482  <br><a name="SEC17" href="#TOC1">COMMENTS</a><br>  <br><a name="SEC17" href="#TOC1">COMMENTS</a><br>
1483  <P>  <P>
1484  The sequence (?# marks the start of a comment which continues up to the next  The sequence (?# marks the start of a comment that continues up to the next
1485  closing parenthesis. Nested parentheses are not permitted. The characters  closing parenthesis. Nested parentheses are not permitted. The characters
1486  that make up a comment play no part in the pattern matching at all.  that make up a comment play no part in the pattern matching at all.
1487  </P>  </P>
1488  <P>  <P>
1489  If the PCRE_EXTENDED option is set, an unescaped # character outside a  If the PCRE_EXTENDED option is set, an unescaped # character outside a
1490  character class introduces a comment that continues up to the next newline  character class introduces a comment that continues to immediately after the
1491  character in the pattern.  next newline in the pattern.
1492  </P>  <a name="recursion"></a></P>
1493  <br><a name="SEC18" href="#TOC1">RECURSIVE PATTERNS</a><br>  <br><a name="SEC18" href="#TOC1">RECURSIVE PATTERNS</a><br>
1494  <P>  <P>
1495  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
1496  unlimited nested parentheses. Without the use of recursion, the best that can  unlimited nested parentheses. Without the use of recursion, the best that can
1497  be done is to use a pattern that matches up to some fixed depth of nesting. It  be done is to use a pattern that matches up to some fixed depth of nesting. It
1498  is not possible to handle an arbitrary nesting depth. Perl has provided an  is not possible to handle an arbitrary nesting depth. Perl provides a facility
1499  experimental facility that allows regular expressions to recurse (amongst other  that allows regular expressions to recurse (amongst other things). It does this
1500  things). It does this by interpolating Perl code in the expression at run time,  by interpolating Perl code in the expression at run time, and the code can
1501  and the code can refer to the expression itself. A Perl pattern to solve the  refer to the expression itself. A Perl pattern to solve the parentheses problem
1502  parentheses problem can be created like this:  can be created like this:
 </P>  
 <P>  
1503  <pre>  <pre>
1504    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;    $re = qr{\( (?: (?&#62;[^()]+) | (?p{$re}) )* \)}x;
1505  </PRE>  </pre>
 </P>  
 <P>  
1506  The (?p{...}) item interpolates Perl code at run time, and in this case refers  The (?p{...}) item interpolates Perl code at run time, and in this case refers
1507  recursively to the pattern in which it appears. Obviously, PCRE cannot support  recursively to the pattern in which it appears. Obviously, PCRE cannot support
1508  the interpolation of Perl code. Instead, it supports some special syntax for  the interpolation of Perl code. Instead, it supports some special syntax for
# Line 1441  number, provided that it occurs inside t Line 1516  number, provided that it occurs inside t
1516  (?R) is a recursive call of the entire regular expression.  (?R) is a recursive call of the entire regular expression.
1517  </P>  </P>
1518  <P>  <P>
1519  For example, this PCRE pattern solves the nested parentheses problem (assume  A recursive subpattern call is always treated as an atomic group. That is, once
1520  the PCRE_EXTENDED option is set so that white space is ignored):  it has matched some of the subject string, it is never re-entered, even if
1521    it contains untried alternatives and there is a subsequent matching failure.
1522  </P>  </P>
1523  <P>  <P>
1524    This PCRE pattern solves the nested parentheses problem (assume the
1525    PCRE_EXTENDED option is set so that white space is ignored):
1526  <pre>  <pre>
1527    \( ( (?&#62;[^()]+) | (?R) )* \)    \( ( (?&#62;[^()]+) | (?R) )* \)
1528  </PRE>  </pre>
 </P>  
 <P>  
1529  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
1530  substrings which can either be a sequence of non-parentheses, or a recursive  substrings which can either be a sequence of non-parentheses, or a recursive
1531  match of the pattern itself (that is a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
1532  Finally there is a closing parenthesis.  Finally there is a closing parenthesis.
1533  </P>  </P>
1534  <P>  <P>
1535  If this were part of a larger pattern, you would not want to recurse the entire  If this were part of a larger pattern, you would not want to recurse the entire
1536  pattern, so instead you could use this:  pattern, so instead you could use this:
 </P>  
 <P>  
1537  <pre>  <pre>
1538    ( \( ( (?&#62;[^()]+) | (?1) )* \) )    ( \( ( (?&#62;[^()]+) | (?1) )* \) )
1539  </PRE>  </pre>
 </P>  
 <P>  
1540  We have put the pattern into parentheses, and caused the recursion to refer to  We have put the pattern into parentheses, and caused the recursion to refer to
1541  them instead of the whole pattern. In a larger pattern, keeping track of  them instead of the whole pattern. In a larger pattern, keeping track of
1542  parenthesis numbers can be tricky. It may be more convenient to use named  parenthesis numbers can be tricky. It may be more convenient to use named
1543  parentheses instead. For this, PCRE uses (?P&#62;name), which is an extension to  parentheses instead. For this, PCRE uses (?P&#62;name), which is an extension to
1544  the Python syntax that PCRE uses for named parentheses (Perl does not provide  the Python syntax that PCRE uses for named parentheses (Perl does not provide
1545  named parentheses). We could rewrite the above example as follows:  named parentheses). We could rewrite the above example as follows:
 </P>  
 <P>  
1546  <pre>  <pre>
1547    (?P&#60;pn&#62; \( ( (?&#62;[^()]+) | (?P&#62;pn) )* \) )    (?P&#60;pn&#62; \( ( (?&#62;[^()]+) | (?P&#62;pn) )* \) )
1548  </PRE>  </pre>
 </P>  
 <P>  
1549  This particular example pattern contains nested unlimited repeats, and so the  This particular example pattern contains nested unlimited repeats, and so the
1550  use of atomic grouping for matching strings of non-parentheses is important  use of atomic grouping for matching strings of non-parentheses is important
1551  when applying the pattern to strings that do not match. For example, when this  when applying the pattern to strings that do not match. For example, when this
1552  pattern is applied to  pattern is applied to
 </P>  
 <P>  
1553  <pre>  <pre>
1554    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1555  </PRE>  </pre>
 </P>  
 <P>  
1556  it yields "no match" quickly. However, if atomic grouping is not used,  it yields "no match" quickly. However, if atomic grouping is not used,
1557  the match runs for a very long time indeed because there are so many different  the match runs for a very long time indeed because there are so many different
1558  ways the + and * repeats can carve up the subject, and all have to be tested  ways the + and * repeats can carve up the subject, and all have to be tested
# Line 1498  before failure can be reported. Line 1562  before failure can be reported.
1562  At the end of a match, the values set for any capturing subpatterns are those  At the end of a match, the values set for any capturing subpatterns are those
1563  from the outermost level of the recursion at which the subpattern value is set.  from the outermost level of the recursion at which the subpattern value is set.
1564  If you want to obtain intermediate values, a callout function can be used (see  If you want to obtain intermediate values, a callout function can be used (see
1565  below and the  the next section and the
1566  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1567  documentation). If the pattern above is matched against  documentation). If the pattern above is matched against
 </P>  
 <P>  
1568  <pre>  <pre>
1569    (ab(cd)ef)    (ab(cd)ef)
1570  </PRE>  </pre>
 </P>  
 <P>  
1571  the value for the capturing parentheses is "ef", which is the last value taken  the value for the capturing parentheses is "ef", which is the last value taken
1572  on at the top level. If additional parentheses are added, giving  on at the top level. If additional parentheses are added, giving
 </P>  
 <P>  
1573  <pre>  <pre>
1574    \( ( ( (?&#62;[^()]+) | (?R) )* ) \)    \( ( ( (?&#62;[^()]+) | (?R) )* ) \)
1575       ^                        ^       ^                        ^
1576       ^                        ^       ^                        ^
1577  </PRE>  </pre>
 </P>  
 <P>  
1578  the string they capture is "ab(cd)ef", the contents of the top level  the string they capture is "ab(cd)ef", the contents of the top level
1579  parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE  parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE
1580  has to obtain extra memory to store data during a recursion, which it does by  has to obtain extra memory to store data during a recursion, which it does by
# Line 1530  Do not confuse the (?R) item with the co Line 1586  Do not confuse the (?R) item with the co
1586  Consider this pattern, which matches text in angle brackets, allowing for  Consider this pattern, which matches text in angle brackets, allowing for
1587  arbitrary nesting. Only digits are allowed in nested brackets (that is, when  arbitrary nesting. Only digits are allowed in nested brackets (that is, when
1588  recursing), whereas any characters are permitted at the outer level.  recursing), whereas any characters are permitted at the outer level.
 </P>  
 <P>  
1589  <pre>  <pre>
1590    &#60; (?: (?(R) \d++  | [^&#60;&#62;]*+) | (?R)) * &#62;    &#60; (?: (?(R) \d++  | [^&#60;&#62;]*+) | (?R)) * &#62;
1591  </PRE>  </pre>
 </P>  
 <P>  
1592  In this pattern, (?(R) is the start of a conditional subpattern, with two  In this pattern, (?(R) is the start of a conditional subpattern, with two
1593  different alternatives for the recursive and non-recursive cases. The (?R) item  different alternatives for the recursive and non-recursive cases. The (?R) item
1594  is the actual recursive call.  is the actual recursive call.
1595  </P>  <a name="subpatternsassubroutines"></a></P>
1596  <a name="subpatternsassubroutines"></a><br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>  <br><a name="SEC19" href="#TOC1">SUBPATTERNS AS SUBROUTINES</a><br>
1597  <P>  <P>
1598  If the syntax for a recursive subpattern reference (either by number or by  If the syntax for a recursive subpattern reference (either by number or by
1599  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
1600  subroutine in a programming language. An earlier example pointed out that the  subroutine in a programming language. An earlier example pointed out that the
1601  pattern  pattern
 </P>  
 <P>  
1602  <pre>  <pre>
1603    (sens|respons)e and \1ibility    (sens|respons)e and \1ibility
1604  </PRE>  </pre>
 </P>  
 <P>  
1605  matches "sense and sensibility" and "response and responsibility", but not  matches "sense and sensibility" and "response and responsibility", but not
1606  "sense and responsibility". If instead the pattern  "sense and responsibility". If instead the pattern
 </P>  
 <P>  
1607  <pre>  <pre>
1608    (sens|respons)e and (?1)ibility    (sens|respons)e and (?1)ibility
1609  </PRE>  </pre>
1610    is used, it does match "sense and responsibility" as well as the other two
1611    strings. Such references, if given numerically, must follow the subpattern to
1612    which they refer. However, named references can refer to later subpatterns.
1613  </P>  </P>
1614  <P>  <P>
1615  is used, it does match "sense and responsibility" as well as the other two  Like recursive subpatterns, a "subroutine" call is always treated as an atomic
1616  strings. Such references must, however, follow the subpattern to which they  group. That is, once it has matched some of the subject string, it is never
1617  refer.  re-entered, even if it contains untried alternatives and there is a subsequent
1618    matching failure.
1619  </P>  </P>
1620  <br><a name="SEC20" href="#TOC1">CALLOUTS</a><br>  <br><a name="SEC20" href="#TOC1">CALLOUTS</a><br>
1621  <P>  <P>
# Line 1585  Within a regular expression, (?C) indica Line 1635  Within a regular expression, (?C) indica
1635  function is to be called. If you want to identify different callout points, you  function is to be called. If you want to identify different callout points, you
1636  can put a number less than 256 after the letter C. The default value is zero.  can put a number less than 256 after the letter C. The default value is zero.
1637  For example, this pattern has two callout points:  For example, this pattern has two callout points:
 </P>  
 <P>  
1638  <pre>  <pre>
1639    (?C1)\dabc(?C2)def    (?C1)\dabc(?C2)def
1640  </PRE>  </pre>
1641    If the PCRE_AUTO_CALLOUT flag is passed to <b>pcre_compile()</b>, callouts are
1642    automatically installed before each item in the pattern. They are all numbered
1643    255.
1644  </P>  </P>
1645  <P>  <P>
1646  During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is  During matching, when PCRE reaches a callout point (and <i>pcre_callout</i> is
1647  set), the external function is called. It is provided with the number of the  set), the external function is called. It is provided with the number of the
1648  callout, and, optionally, one item of data originally supplied by the caller of  callout, the position in the pattern, and, optionally, one item of data
1649  <b>pcre_exec()</b>. The callout function may cause matching to backtrack, or to  originally supplied by the caller of <b>pcre_exec()</b>. The callout function
1650  fail altogether. A complete description of the interface to the callout  may cause matching to proceed, to backtrack, or to fail altogether. A complete
1651  function is given in the  description of the interface to the callout function is given in the
1652  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1653  documentation.  documentation.
1654  </P>  </P>
1655  <P>  <P>
1656  Last updated: 03 February 2003  Last updated: 06 June 2006
1657  <br>  <br>
1658  Copyright &copy; 1997-2003 University of Cambridge.  Copyright &copy; 1997-2006 University of Cambridge.
1659    <p>
1660    Return to the <a href="index.html">PCRE index page</a>.
1661    </p>

Legend:
Removed from v.73  
changed lines
  Added in v.91

  ViewVC Help
Powered by ViewVC 1.1.5