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

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

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

revision 79 by nigel, Sat Feb 24 21:40:52 2007 UTC revision 211 by ph10, Thu Aug 9 09:52:43 2007 UTC
# Line 4  PCRE - Perl-compatible regular expressio Line 4  PCRE - Perl-compatible regular expressio
4  .SH "PCRE REGULAR EXPRESSION DETAILS"  .SH "PCRE REGULAR EXPRESSION DETAILS"
5  .rs  .rs
6  .sp  .sp
7  The syntax and semantics of the regular expressions supported by PCRE are  The syntax and semantics of the regular expressions that are supported by PCRE
8  described below. Regular expressions are also described in the Perl  are described in detail below. There is a quick-reference syntax summary in the
9  documentation and in a number of books, some of which have copious examples.  .\" HREF
10  Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers  \fBpcresyntax\fP
11  regular expressions in great detail. This description of PCRE's regular  .\"
12  expressions is intended as reference material.  page. Perl's regular expressions are described in its own documentation, and
13    regular expressions in general are covered in a number of books, some of which
14    have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
15    published by O'Reilly, covers regular expressions in great detail. This
16    description of PCRE's regular expressions is intended as reference material.
17  .P  .P
18  The original operation of PCRE was on strings of one-byte characters. However,  The original operation of PCRE was on strings of one-byte characters. However,
19  there is now also support for UTF-8 character strings. To use this, you must  there is now also support for UTF-8 character strings. To use this, you must
# Line 30  The remainder of this document discusses Line 34  The remainder of this document discusses
34  PCRE when its main matching function, \fBpcre_exec()\fP, is used.  PCRE when its main matching function, \fBpcre_exec()\fP, is used.
35  From release 6.0, PCRE offers a second matching function,  From release 6.0, PCRE offers a second matching function,
36  \fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not  \fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not
37  Perl-compatible. The advantages and disadvantages of the alternative function,  Perl-compatible. Some of the features discussed below are not available when
38  and how it differs from the normal function, are discussed in the  \fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the
39    alternative function, and how it differs from the normal function, are
40    discussed in the
41  .\" HREF  .\" HREF
42  \fBpcrematching\fP  \fBpcrematching\fP
43  .\"  .\"
44  page.  page.
45  .P  .
46    .
47    .SH "CHARACTERS AND METACHARACTERS"
48    .rs
49    .sp
50  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
51  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
52  corresponding characters in the subject. As a trivial example, the pattern  corresponding characters in the subject. As a trivial example, the pattern
# Line 60  interpreted in some special way. Line 70  interpreted in some special way.
70  .P  .P
71  There are two different sets of metacharacters: those that are recognized  There are two different sets of metacharacters: those that are recognized
72  anywhere in the pattern except within square brackets, and those that are  anywhere in the pattern except within square brackets, and those that are
73  recognized in square brackets. Outside square brackets, the metacharacters are  recognized within square brackets. Outside square brackets, the metacharacters
74  as follows:  are as follows:
75  .sp  .sp
76    \e      general escape character with several uses    \e      general escape character with several uses
77    ^      assert start of string (or line, in multiline mode)    ^      assert start of string (or line, in multiline mode)
# Line 92  a character class the only metacharacter Line 102  a character class the only metacharacter
102  .sp  .sp
103  The following sections describe the use of each of the metacharacters.  The following sections describe the use of each of the metacharacters.
104  .  .
105    .
106  .SH BACKSLASH  .SH BACKSLASH
107  .rs  .rs
108  .sp  .sp
109  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
110  non-alphanumeric character, it takes away any special meaning that character may  non-alphanumeric character, it takes away any special meaning that character
111  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
112  outside character classes.  outside character classes.
113  .P  .P
114  For example, if you want to match a * character, you write \e* in the pattern.  For example, if you want to match a * character, you write \e* in the pattern.
# Line 108  particular, if you want to match a backs Line 119  particular, if you want to match a backs
119  .P  .P
120  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
121  pattern (other than in a character class) and characters between a # outside  pattern (other than in a character class) and characters between a # outside
122  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
123  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.  
124  .P  .P
125  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
126  can do so by putting them between \eQ and \eE. This is different from Perl in  can do so by putting them between \eQ and \eE. This is different from Perl in
# Line 148  represents: Line 158  represents:
158    \et        tab (hex 09)    \et        tab (hex 09)
159    \eddd      character with octal code ddd, or backreference    \eddd      character with octal code ddd, or backreference
160    \exhh      character with hex code hh    \exhh      character with hex code hh
161    \ex{hhh..} character with hex code hhh... (UTF-8 mode only)    \ex{hhh..} character with hex code hhh..
162  .sp  .sp
163  The precise effect of \ecx is as follows: if x is a lower case letter, it  The precise effect of \ecx is as follows: if x is a lower case letter, it
164  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.
# Line 156  Thus \ecz becomes hex 1A, but \ec{ becom Line 166  Thus \ecz becomes hex 1A, but \ec{ becom
166  7B.  7B.
167  .P  .P
168  After \ex, from zero to two hexadecimal digits are read (letters can be in  After \ex, from zero to two hexadecimal digits are read (letters can be in
169  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 \ex{
170  appear between \ex{ 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
171  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 value in
172  other than hexadecimal digits appear between \ex{ and }, or if there is no  hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code
173  terminating }, this form of escape is not recognized. Instead, the initial  point, which is 10FFFF.
174  \ex will be interpreted as a basic hexadecimal escape, with no following  .P
175  digits, giving a character whose value is zero.  If characters other than hexadecimal digits appear between \ex{ and }, or if
176    there is no terminating }, this form of escape is not recognized. Instead, the
177    initial \ex will be interpreted as a basic hexadecimal escape, with no
178    following digits, giving a character whose value is zero.
179  .P  .P
180  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
181  syntaxes for \ex when PCRE is in UTF-8 mode. There is no difference in the  syntaxes for \ex. There is no difference in the way they are handled. For
182  way they are handled. For example, \exdc is exactly the same as \ex{dc}.  example, \exdc is exactly the same as \ex{dc}.
183  .P  .P
184  After \e0 up to two further octal digits are read. In both cases, if there  After \e0 up to two further octal digits are read. If there are fewer than two
185  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 \e0\ex\e07
186  sequence \e0\ex\e07 specifies two binary zeros followed by a BEL character  specifies two binary zeros followed by a BEL character (code value 7). Make
187  (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
188  pattern character that follows is itself an octal digit.  follows is itself an octal digit.
189  .P  .P
190  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.
191  Outside a character class, PCRE reads it and any following digits as a decimal  Outside a character class, PCRE reads it and any following digits as a decimal
# Line 191  parenthesized subpatterns. Line 204  parenthesized subpatterns.
204  .P  .P
205  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
206  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
207  digits following the backslash, and generates a single byte from the least  digits following the backslash, and uses them to generate a data character. Any
208  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
209  For example:  character specified in octal must be less than \e400. In UTF-8 mode, values up
210    to \e777 are permitted. For example:
211  .sp  .sp
212    \e040   is another way of writing a space    \e040   is another way of writing a space
213  .\" JOIN  .\" JOIN
# Line 218  For example: Line 232  For example:
232  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
233  zero, because no more than three octal digits are ever read.  zero, because no more than three octal digits are ever read.
234  .P  .P
235  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
236  (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
237  addition, inside a character class, the sequence \eb is interpreted as the  sequence \eb is interpreted as the backspace character (hex 08), and the
238  backspace character (hex 08), and the sequence \eX is interpreted as the  sequences \eR and \eX are interpreted as the characters "R" and "X",
239  character "X". Outside a character class, these sequences have different  respectively. Outside a character class, these sequences have different
240  meanings  meanings
241  .\" HTML <a href="#uniextseq">  .\" HTML <a href="#uniextseq">
242  .\" </a>  .\" </a>
# Line 230  meanings Line 244  meanings
244  .\"  .\"
245  .  .
246  .  .
247    .SS "Absolute and relative back references"
248    .rs
249    .sp
250    The sequence \eg followed by an unsigned or a negative number, optionally
251    enclosed in braces, is an absolute or relative back reference. A named back
252    reference can be coded as \eg{name}. Back references are discussed
253    .\" HTML <a href="#backreferences">
254    .\" </a>
255    later,
256    .\"
257    following the discussion of
258    .\" HTML <a href="#subpattern">
259    .\" </a>
260    parenthesized subpatterns.
261    .\"
262    .
263    .
264  .SS "Generic character types"  .SS "Generic character types"
265  .rs  .rs
266  .sp  .sp
267  The third use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types. The
268  following are always recognized:  following are always recognized:
269  .sp  .sp
270    \ed     any decimal digit    \ed     any decimal digit
271    \eD     any character that is not a decimal digit    \eD     any character that is not a decimal digit
272      \eh     any horizontal whitespace character
273      \eH     any character that is not a horizontal whitespace character
274    \es     any whitespace character    \es     any whitespace character
275    \eS     any character that is not a whitespace character    \eS     any character that is not a whitespace character
276      \ev     any vertical whitespace character
277      \eV     any character that is not a vertical whitespace character
278    \ew     any "word" character    \ew     any "word" character
279    \eW     any "non-word" character    \eW     any "non-word" character
280  .sp  .sp
# Line 253  there is no character to match. Line 288  there is no character to match.
288  .P  .P
289  For compatibility with Perl, \es does not match the VT character (code 11).  For compatibility with Perl, \es does not match the VT character (code 11).
290  This makes it different from the the POSIX "space" class. The \es characters  This makes it different from the the POSIX "space" class. The \es characters
291  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
292    included in a Perl script, \es may match the VT character. In PCRE, it never
293    does.
294    .P
295    In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or
296    \ew, and always match \eD, \eS, and \eW. This is true even when Unicode
297    character property support is available. These sequences retain their original
298    meanings from before UTF-8 support was available, mainly for efficiency
299    reasons.
300    .P
301    The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the
302    other sequences, these do match certain high-valued codepoints in UTF-8 mode.
303    The horizontal space characters are:
304    .sp
305      U+0009     Horizontal tab
306      U+0020     Space
307      U+00A0     Non-break space
308      U+1680     Ogham space mark
309      U+180E     Mongolian vowel separator
310      U+2000     En quad
311      U+2001     Em quad
312      U+2002     En space
313      U+2003     Em space
314      U+2004     Three-per-em space
315      U+2005     Four-per-em space
316      U+2006     Six-per-em space
317      U+2007     Figure space
318      U+2008     Punctuation space
319      U+2009     Thin space
320      U+200A     Hair space
321      U+202F     Narrow no-break space
322      U+205F     Medium mathematical space
323      U+3000     Ideographic space
324    .sp
325    The vertical space characters are:
326    .sp
327      U+000A     Linefeed
328      U+000B     Vertical tab
329      U+000C     Formfeed
330      U+000D     Carriage return
331      U+0085     Next line
332      U+2028     Line separator
333      U+2029     Paragraph separator
334  .P  .P
335  A "word" character is an underscore or any character less than 256 that is a  A "word" character is an underscore or any character less than 256 that is a
336  letter or digit. The definition of letters and digits is controlled by PCRE's  letter or digit. The definition of letters and digits is controlled by PCRE's
# Line 267  in the Line 344  in the
344  .\" HREF  .\" HREF
345  \fBpcreapi\fP  \fBpcreapi\fP
346  .\"  .\"
347  page). For example, in the "fr_FR" (French) locale, some character codes  page). For example, in a French locale such as "fr_FR" in Unix-like systems,
348  greater than 128 are used for accented letters, and these are matched by \ew.  or "french" in Windows, some character codes greater than 128 are used for
349    accented letters, and these are matched by \ew. The use of locales with Unicode
350    is discouraged.
351    .
352    .
353    .SS "Newline sequences"
354    .rs
355    .sp
356    Outside a character class, the escape sequence \eR matches any Unicode newline
357    sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \eR is equivalent to
358    the following:
359    .sp
360      (?>\er\en|\en|\ex0b|\ef|\er|\ex85)
361    .sp
362    This is an example of an "atomic group", details of which are given
363    .\" HTML <a href="#atomicgroup">
364    .\" </a>
365    below.
366    .\"
367    This particular group matches either the two-character sequence CR followed by
368    LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
369    U+000B), FF (formfeed, U+000C), CR (carriage return, U+000D), or NEL (next
370    line, U+0085). The two-character sequence is treated as a single unit that
371    cannot be split.
372    .P
373    In UTF-8 mode, two additional characters whose codepoints are greater than 255
374    are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
375    Unicode character property support is not needed for these characters to be
376    recognized.
377  .P  .P
378  In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or  Inside a character class, \eR matches the letter "R".
 \ew, and always match \eD, \eS, and \eW. This is true even when Unicode  
 character property support is available.  
379  .  .
380  .  .
381  .\" HTML <a name="uniextseq"></a>  .\" HTML <a name="uniextseq"></a>
# Line 280  character property support is available. Line 383  character property support is available.
383  .rs  .rs
384  .sp  .sp
385  When PCRE is built with Unicode character property support, three additional  When PCRE is built with Unicode character property support, three additional
386  escape sequences to match generic character types are available when UTF-8 mode  escape sequences that match characters with specific properties are available.
387  is selected. They are:  When not in UTF-8 mode, these sequences are of course limited to testing
388  .sp  characters whose codepoints are less than 256, but they do work in this mode.
389   \ep{\fIxx\fP}   a character with the \fIxx\fP property  The extra escape sequences are:
390   \eP{\fIxx\fP}   a character without the \fIxx\fP property  .sp
391   \eX       an extended Unicode sequence    \ep{\fIxx\fP}   a character with the \fIxx\fP property
392  .sp    \eP{\fIxx\fP}   a character without the \fIxx\fP property
393  The property names represented by \fIxx\fP above are limited to the    \eX       an extended Unicode sequence
394  Unicode general category properties. Each character has exactly one such  .sp
395  property, specified by a two-letter abbreviation. For compatibility with Perl,  The property names represented by \fIxx\fP above are limited to the Unicode
396  negation can be specified by including a circumflex between the opening brace  script names, the general category properties, and "Any", which matches any
397  and the property name. For example, \ep{^Lu} is the same as \eP{Lu}.  character (including newline). Other properties such as "InMusicalSymbols" are
398  .P  not currently supported by PCRE. Note that \eP{Any} does not match any
399  If only one letter is specified with \ep or \eP, it includes all the properties  characters, so always causes a match failure.
400  that start with that letter. In this case, in the absence of negation, the  .P
401  curly brackets in the escape sequence are optional; these two examples have  Sets of Unicode characters are defined as belonging to certain scripts. A
402  the same effect:  character from one of these sets can be matched using a script name. For
403    example:
404    .sp
405      \ep{Greek}
406      \eP{Han}
407    .sp
408    Those that are not part of an identified script are lumped together as
409    "Common". The current list of scripts is:
410    .P
411    Arabic,
412    Armenian,
413    Balinese,
414    Bengali,
415    Bopomofo,
416    Braille,
417    Buginese,
418    Buhid,
419    Canadian_Aboriginal,
420    Cherokee,
421    Common,
422    Coptic,
423    Cuneiform,
424    Cypriot,
425    Cyrillic,
426    Deseret,
427    Devanagari,
428    Ethiopic,
429    Georgian,
430    Glagolitic,
431    Gothic,
432    Greek,
433    Gujarati,
434    Gurmukhi,
435    Han,
436    Hangul,
437    Hanunoo,
438    Hebrew,
439    Hiragana,
440    Inherited,
441    Kannada,
442    Katakana,
443    Kharoshthi,
444    Khmer,
445    Lao,
446    Latin,
447    Limbu,
448    Linear_B,
449    Malayalam,
450    Mongolian,
451    Myanmar,
452    New_Tai_Lue,
453    Nko,
454    Ogham,
455    Old_Italic,
456    Old_Persian,
457    Oriya,
458    Osmanya,
459    Phags_Pa,
460    Phoenician,
461    Runic,
462    Shavian,
463    Sinhala,
464    Syloti_Nagri,
465    Syriac,
466    Tagalog,
467    Tagbanwa,
468    Tai_Le,
469    Tamil,
470    Telugu,
471    Thaana,
472    Thai,
473    Tibetan,
474    Tifinagh,
475    Ugaritic,
476    Yi.
477    .P
478    Each character has exactly one general category property, specified by a
479    two-letter abbreviation. For compatibility with Perl, negation can be specified
480    by including a circumflex between the opening brace and the property name. For
481    example, \ep{^Lu} is the same as \eP{Lu}.
482    .P
483    If only one letter is specified with \ep or \eP, it includes all the general
484    category properties that start with that letter. In this case, in the absence
485    of negation, the curly brackets in the escape sequence are optional; these two
486    examples have the same effect:
487  .sp  .sp
488    \ep{L}    \ep{L}
489    \epL    \epL
490  .sp  .sp
491  The following property codes are supported:  The following general category property codes are supported:
492  .sp  .sp
493    C     Other    C     Other
494    Cc    Control    Cc    Control
# Line 347  The following property codes are support Line 534  The following property codes are support
534    Zp    Paragraph separator    Zp    Paragraph separator
535    Zs    Space separator    Zs    Space separator
536  .sp  .sp
537  Extended properties such as "Greek" or "InMusicalSymbols" are not supported by  The special property L& is also supported: it matches a character that has
538  PCRE.  the Lu, Ll, or Lt property, in other words, a letter that is not classified as
539    a modifier or "other".
540    .P
541    The Cs (Surrogate) property applies only to characters in the range U+D800 to
542    U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so
543    cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
544    (see the discussion of PCRE_NO_UTF8_CHECK in the
545    .\" HREF
546    \fBpcreapi\fP
547    .\"
548    page).
549    .P
550    The long synonyms for these properties that Perl supports (such as \ep{Letter})
551    are not supported by PCRE, nor is it permitted to prefix any of these
552    properties with "Is".
553    .P
554    No character that is in the Unicode table has the Cn (unassigned) property.
555    Instead, this property is assumed for any code point that is not in the
556    Unicode table.
557  .P  .P
558  Specifying caseless matching does not affect these escape sequences. For  Specifying caseless matching does not affect these escape sequences. For
559  example, \ep{Lu} always matches only upper case letters.  example, \ep{Lu} always matches only upper case letters.
# Line 366  atomic group Line 571  atomic group
571  (see below).  (see below).
572  .\"  .\"
573  Characters with the "mark" property are typically accents that affect the  Characters with the "mark" property are typically accents that affect the
574  preceding character.  preceding character. None of them have codepoints less than 256, so in
575    non-UTF-8 mode \eX matches any one character.
576  .P  .P
577  Matching characters by Unicode property is not fast, because PCRE has to search  Matching characters by Unicode property is not fast, because PCRE has to search
578  a structure that contains data for over fifteen thousand characters. That is  a structure that contains data for over fifteen thousand characters. That is
# Line 374  why the traditional escape sequences suc Line 580  why the traditional escape sequences suc
580  properties in PCRE.  properties in PCRE.
581  .  .
582  .  .
583    .\" HTML <a name="resetmatchstart"></a>
584    .SS "Resetting the match start"
585    .rs
586    .sp
587    The escape sequence \eK, which is a Perl 5.10 feature, causes any previously
588    matched characters not to be included in the final matched sequence. For
589    example, the pattern:
590    .sp
591      foo\eKbar
592    .sp
593    matches "foobar", but reports that it has matched "bar". This feature is
594    similar to a lookbehind assertion
595    .\" HTML <a href="#lookbehind">
596    .\" </a>
597    (described below).
598    .\"
599    However, in this case, the part of the subject before the real match does not
600    have to be of fixed length, as lookbehind assertions do. The use of \eK does
601    not interfere with the setting of
602    .\" HTML <a href="#subpattern">
603    .\" </a>
604    captured substrings.
605    .\"
606    For example, when the pattern
607    .sp
608      (foo)\eKbar
609    .sp
610    matches "foobar", the first substring is still set to "foo".
611    .
612    .
613  .\" HTML <a name="smallassertions"></a>  .\" HTML <a name="smallassertions"></a>
614  .SS "Simple assertions"  .SS "Simple assertions"
615  .rs  .rs
616  .sp  .sp
617  The fourth use of backslash is for certain simple assertions. An assertion  The final use of backslash is for certain simple assertions. An assertion
618  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,
619  without consuming any characters from the subject string. The use of  without consuming any characters from the subject string. The use of
620  subpatterns for more complicated assertions is described  subpatterns for more complicated assertions is described
# Line 386  subpatterns for more complicated asserti Line 622  subpatterns for more complicated asserti
622  .\" </a>  .\" </a>
623  below.  below.
624  .\"  .\"
625  The backslashed  The backslashed assertions are:
 assertions are:  
626  .sp  .sp
627    \eb     matches at a word boundary    \eb     matches at a word boundary
628    \eB     matches when not at a word boundary    \eB     matches when not at a word boundary
629    \eA     matches at start of subject    \eA     matches at the start of the subject
630    \eZ     matches at end of subject or before newline at end    \eZ     matches at the end of the subject
631    \ez     matches at end of subject            also matches before a newline at the end of the subject
632    \eG     matches at first matching position in subject    \ez     matches only at the end of the subject
633      \eG     matches at the first matching position in the subject
634  .sp  .sp
635  These assertions may not appear in character classes (but note that \eb has a  These assertions may not appear in character classes (but note that \eb has a
636  different meaning, namely the backspace character, inside a character class).  different meaning, namely the backspace character, inside a character class).
# Line 412  PCRE_NOTBOL or PCRE_NOTEOL options, whic Line 648  PCRE_NOTBOL or PCRE_NOTEOL options, whic
648  circumflex and dollar metacharacters. However, if the \fIstartoffset\fP  circumflex and dollar metacharacters. However, if the \fIstartoffset\fP
649  argument of \fBpcre_exec()\fP is non-zero, indicating that matching is to start  argument of \fBpcre_exec()\fP is non-zero, indicating that matching is to start
650  at a point other than the beginning of the subject, \eA can never match. The  at a point other than the beginning of the subject, \eA can never match. The
651  difference between \eZ and \ez is that \eZ matches before a newline that is the  difference between \eZ and \ez is that \eZ matches before a newline at the end
652  last character of the string as well as at the end of the string, whereas \ez  of the string as well as at the very end, whereas \ez matches only at the end.
 matches only at the end.  
653  .P  .P
654  The \eG assertion is true only when the current matching position is at the  The \eG assertion is true only when the current matching position is at the
655  start point of the match, as specified by the \fIstartoffset\fP argument of  start point of the match, as specified by the \fIstartoffset\fP argument of
# Line 458  to be anchored.) Line 693  to be anchored.)
693  .P  .P
694  A dollar character is an assertion that is true only if the current matching  A dollar character is an assertion that is true only if the current matching
695  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
696  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
697  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
698  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
699  Dollar has no special meaning in a character class.  character class.
700  .P  .P
701  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
702  the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This  the string, by setting the PCRE_DOLLAR_ENDONLY option at compile time. This
703  does not affect the \eZ assertion.  does not affect the \eZ assertion.
704  .P  .P
705  The meanings of the circumflex and dollar characters are changed if the  The meanings of the circumflex and dollar characters are changed if the
706  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
707  after and immediately before an internal newline character, respectively, in  immediately after internal newlines as well as at the start of the subject
708  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
709  the pattern /^abc$/ matches the subject string "def\enabc" (where \en  matches before any newlines in the string, as well as at the very end, when
710  represents a newline character) in multiline mode, but not otherwise.  PCRE_MULTILINE is set. When newline is specified as the two-character
711  Consequently, patterns that are anchored in single line mode because all  sequence CRLF, isolated CR and LF characters do not indicate newlines.
712  branches start with ^ are not anchored in multiline mode, and a match for  .P
713  circumflex is possible when the \fIstartoffset\fP argument of \fBpcre_exec()\fP  For example, the pattern /^abc$/ matches the subject string "def\enabc" (where
714  is non-zero. The PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is  \en represents a newline) in multiline mode, but not otherwise. Consequently,
715  set.  patterns that are anchored in single line mode because all branches start with
716    ^ are not anchored in multiline mode, and a match for circumflex is possible
717    when the \fIstartoffset\fP argument of \fBpcre_exec()\fP is non-zero. The
718    PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is set.
719  .P  .P
720  Note that the sequences \eA, \eZ, and \ez can be used to match the start and  Note that the sequences \eA, \eZ, and \ez can be used to match the start and
721  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
722  \eA it is always anchored, whether PCRE_MULTILINE is set or not.  \eA it is always anchored, whether or not PCRE_MULTILINE is set.
723  .  .
724  .  .
725  .SH "FULL STOP (PERIOD, DOT)"  .SH "FULL STOP (PERIOD, DOT)"
726  .rs  .rs
727  .sp  .sp
728  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
729  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
730  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.
731  byte long, except (by default) newline. If the PCRE_DOTALL option is set,  .P
732  dots match newlines as well. The handling of dot is entirely independent of the  When a line ending is defined as a single character, dot never matches that
733  handling of circumflex and dollar, the only relationship being that they both  character; when the two-character sequence CRLF is used, dot does not match CR
734  involve newline characters. Dot has no special meaning in a character class.  if it is immediately followed by LF, but otherwise it matches all characters
735    (including isolated CRs and LFs). When any Unicode line endings are being
736    recognized, dot does not match CR or LF or any of the other line ending
737    characters.
738    .P
739    The behaviour of dot with regard to newlines can be changed. If the PCRE_DOTALL
740    option is set, a dot matches any one character, without exception. If the
741    two-character sequence CRLF is present in the subject string, it takes two dots
742    to match it.
743    .P
744    The handling of dot is entirely independent of the handling of circumflex and
745    dollar, the only relationship being that they both involve newlines. Dot has no
746    special meaning in a character class.
747  .  .
748  .  .
749  .SH "MATCHING A SINGLE BYTE"  .SH "MATCHING A SINGLE BYTE"
750  .rs  .rs
751  .sp  .sp
752  Outside a character class, the escape sequence \eC matches any one byte, both  Outside a character class, the escape sequence \eC matches any one byte, both
753  in and out of UTF-8 mode. Unlike a dot, it can match a newline. The feature is  in and out of UTF-8 mode. Unlike a dot, it always matches any line-ending
754  provided in Perl in order to match individual bytes in UTF-8 mode. Because it  characters. The feature is provided in Perl in order to match individual bytes
755  breaks up UTF-8 characters into individual bytes, what remains in the string  in UTF-8 mode. Because it breaks up UTF-8 characters into individual bytes,
756  may be a malformed UTF-8 string. For this reason, the \eC escape sequence is  what remains in the string may be a malformed UTF-8 string. For this reason,
757  best avoided.  the \eC escape sequence is best avoided.
758  .P  .P
759  PCRE does not allow \eC to appear in lookbehind assertions  PCRE does not allow \eC to appear in lookbehind assertions
760  .\" HTML <a href="#lookbehind">  .\" HTML <a href="#lookbehind">
# Line 555  If you want to use caseless matching for Line 805  If you want to use caseless matching for
805  ensure that PCRE is compiled with Unicode property support as well as with  ensure that PCRE is compiled with Unicode property support as well as with
806  UTF-8 support.  UTF-8 support.
807  .P  .P
808  The newline character is never treated in any special way in character classes,  Characters that might indicate line breaks are never treated in any special way
809  whatever the setting of the PCRE_DOTALL or PCRE_MULTILINE options is. A class  when matching character classes, whatever line-ending sequence is in use, and
810  such as [^a] will always match a newline.  whatever setting of the PCRE_DOTALL and PCRE_MULTILINE options is used. A class
811    such as [^a] always matches one of these characters.
812  .P  .P
813  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
814  character class. For example, [d-m] matches any letter between d and m,  character class. For example, [d-m] matches any letter between d and m,
# Line 581  example [\ex{100}-\ex{2ff}]. Line 832  example [\ex{100}-\ex{2ff}].
832  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
833  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
834  [][\e\e^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character  [][\e\e^_`wxyzabc], matched caselessly, and in non-UTF-8 mode, if character
835  tables for the "fr_FR" locale are in use, [\exc8-\excb] matches accented E  tables for a French locale are in use, [\exc8-\excb] matches accented E
836  characters in both cases. In UTF-8 mode, PCRE supports the concept of case for  characters in both cases. In UTF-8 mode, PCRE supports the concept of case for
837  characters with values greater than 128 only when it is compiled with Unicode  characters with values greater than 128 only when it is compiled with Unicode
838  property support.  property support.
# Line 656  the pattern Line 907  the pattern
907    gilbert|sullivan    gilbert|sullivan
908  .sp  .sp
909  matches either "gilbert" or "sullivan". Any number of alternatives may appear,  matches either "gilbert" or "sullivan". Any number of alternatives may appear,
910  and an empty alternative is permitted (matching the empty string).  and an empty alternative is permitted (matching the empty string). The matching
911  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
912  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
 subpattern  
913  .\" HTML <a href="#subpattern">  .\" HTML <a href="#subpattern">
914  .\" </a>  .\" </a>
915  (defined below),  (defined below),
# Line 693  If the change is placed right at the sta Line 943  If the change is placed right at the sta
943  the global options (and it will therefore show up in data extracted by the  the global options (and it will therefore show up in data extracted by the
944  \fBpcre_fullinfo()\fP function).  \fBpcre_fullinfo()\fP function).
945  .P  .P
946  An option change within a subpattern affects only that part of the current  An option change within a subpattern (see below for a description of
947  pattern that follows it, so  subpatterns) affects only that part of the current pattern that follows it, so
948  .sp  .sp
949    (a(?i)b)c    (a(?i)b)c
950  .sp  .sp
# Line 710  branch is abandoned before the option se Line 960  branch is abandoned before the option se
960  option settings happen at compile time. There would be some very weird  option settings happen at compile time. There would be some very weird
961  behaviour otherwise.  behaviour otherwise.
962  .P  .P
963  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
964  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
965  respectively. The (?X) flag setting is special in that it must always occur  J, U and X respectively.
 earlier in the pattern than any of the additional features it turns on, even  
 when it is at top level. It is best to put it at the start.  
966  .  .
967  .  .
968  .\" HTML <a name="subpattern"></a>  .\" HTML <a name="subpattern"></a>
# Line 729  Turning part of a pattern into a subpatt Line 977  Turning part of a pattern into a subpatt
977    cat(aract|erpillar|)    cat(aract|erpillar|)
978  .sp  .sp
979  matches one of the words "cat", "cataract", or "caterpillar". Without the  matches one of the words "cat", "cataract", or "caterpillar". Without the
980  parentheses, it would match "cataract", "erpillar" or the empty string.  parentheses, it would match "cataract", "erpillar" or an empty string.
981  .sp  .sp
982  2. It sets up the subpattern as a capturing subpattern. This means that, when  2. It sets up the subpattern as a capturing subpattern. This means that, when
983  the whole pattern matches, that portion of the subject string that matched the  the whole pattern matches, that portion of the subject string that matched the
# Line 754  the string "the white queen" is matched Line 1002  the string "the white queen" is matched
1002    the ((?:red|white) (king|queen))    the ((?:red|white) (king|queen))
1003  .sp  .sp
1004  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
1005  2. The maximum number of capturing subpatterns is 65535, and the maximum depth  2. The maximum number of capturing subpatterns is 65535.
 of nesting of all subpatterns, both capturing and non-capturing, is 200.  
1006  .P  .P
1007  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
1008  a non-capturing subpattern, the option letters may appear between the "?" and  a non-capturing subpattern, the option letters may appear between the "?" and
# Line 770  is reached, an option setting in one bra Line 1017  is reached, an option setting in one bra
1017  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
1018  .  .
1019  .  .
1020    .SH "DUPLICATE SUBPATTERN NUMBERS"
1021    .rs
1022    .sp
1023    Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
1024    the same numbers for its capturing parentheses. Such a subpattern starts with
1025    (?| and is itself a non-capturing subpattern. For example, consider this
1026    pattern:
1027    .sp
1028      (?|(Sat)ur|(Sun))day
1029    .sp
1030    Because the two alternatives are inside a (?| group, both sets of capturing
1031    parentheses are numbered one. Thus, when the pattern matches, you can look
1032    at captured substring number one, whichever alternative matched. This construct
1033    is useful when you want to capture part, but not all, of one of a number of
1034    alternatives. Inside a (?| group, parentheses are numbered as usual, but the
1035    number is reset at the start of each branch. The numbers of any capturing
1036    buffers that follow the subpattern start after the highest number used in any
1037    branch. The following example is taken from the Perl documentation.
1038    The numbers underneath show in which buffer the captured content will be
1039    stored.
1040    .sp
1041      # before  ---------------branch-reset----------- after
1042      / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1043      # 1            2         2  3        2     3     4
1044    .sp
1045    A backreference or a recursive call to a numbered subpattern always refers to
1046    the first one in the pattern with the given number.
1047    .P
1048    An alternative approach to using this "branch reset" feature is to use
1049    duplicate named subpatterns, as described in the next section.
1050    .
1051    .
1052  .SH "NAMED SUBPATTERNS"  .SH "NAMED SUBPATTERNS"
1053  .rs  .rs
1054  .sp  .sp
1055  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
1056  to keep track of the numbers in complicated regular expressions. Furthermore,  to keep track of the numbers in complicated regular expressions. Furthermore,
1057  if an expression is modified, the numbers may change. To help with this  if an expression is modified, the numbers may change. To help with this
1058  difficulty, PCRE supports the naming of subpatterns, something that Perl does  difficulty, PCRE supports the naming of subpatterns. This feature was not
1059  not provide. The Python syntax (?P<name>...) is used. Names consist of  added to Perl until release 5.10. Python had the feature earlier, and PCRE
1060  alphanumeric characters and underscores, and must be unique within a pattern.  introduced it at release 4.0, using the Python syntax. PCRE now supports both
1061  .P  the Perl and the Python syntax.
1062  Named capturing parentheses are still allocated numbers as well as names. The  .P
1063  PCRE API provides function calls for extracting the name-to-number translation  In PCRE, a subpattern can be named in one of three ways: (?<name>...) or
1064  table from a compiled pattern. There is also a convenience function for  (?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing
1065  extracting a captured substring by name. For further details see the  parentheses from other parts of the pattern, such as
1066    .\" HTML <a href="#backreferences">
1067    .\" </a>
1068    backreferences,
1069    .\"
1070    .\" HTML <a href="#recursion">
1071    .\" </a>
1072    recursion,
1073    .\"
1074    and
1075    .\" HTML <a href="#conditions">
1076    .\" </a>
1077    conditions,
1078    .\"
1079    can be made by name as well as by number.
1080    .P
1081    Names consist of up to 32 alphanumeric characters and underscores. Named
1082    capturing parentheses are still allocated numbers as well as names, exactly as
1083    if the names were not present. The PCRE API provides function calls for
1084    extracting the name-to-number translation table from a compiled pattern. There
1085    is also a convenience function for extracting a captured substring by name.
1086    .P
1087    By default, a name must be unique within a pattern, but it is possible to relax
1088    this constraint by setting the PCRE_DUPNAMES option at compile time. This can
1089    be useful for patterns where only one instance of the named parentheses can
1090    match. Suppose you want to match the name of a weekday, either as a 3-letter
1091    abbreviation or as the full name, and in both cases you want to extract the
1092    abbreviation. This pattern (ignoring the line breaks) does the job:
1093    .sp
1094      (?<DN>Mon|Fri|Sun)(?:day)?|
1095      (?<DN>Tue)(?:sday)?|
1096      (?<DN>Wed)(?:nesday)?|
1097      (?<DN>Thu)(?:rsday)?|
1098      (?<DN>Sat)(?:urday)?
1099    .sp
1100    There are five capturing substrings, but only one is ever set after a match.
1101    (An alternative way of solving this problem is to use a "branch reset"
1102    subpattern, as described in the previous section.)
1103    .P
1104    The convenience function for extracting the data by name returns the substring
1105    for the first (and in this example, the only) subpattern of that name that
1106    matched. This saves searching to find which numbered subpattern it was. If you
1107    make a reference to a non-unique named subpattern from elsewhere in the
1108    pattern, the one that corresponds to the lowest number is used. For further
1109    details of the interfaces for handling named subpatterns, see the
1110  .\" HREF  .\" HREF
1111  \fBpcreapi\fP  \fBpcreapi\fP
1112  .\"  .\"
# Line 797  Repetition is specified by quantifiers, Line 1120  Repetition is specified by quantifiers,
1120  items:  items:
1121  .sp  .sp
1122    a literal data character    a literal data character
1123    the . metacharacter    the dot metacharacter
1124    the \eC escape sequence    the \eC escape sequence
1125    the \eX escape sequence (in UTF-8 mode with Unicode properties)    the \eX escape sequence (in UTF-8 mode with Unicode properties)
1126      the \eR escape sequence
1127    an escape such as \ed that matches a single character    an escape such as \ed that matches a single character
1128    a character class    a character class
1129    a back reference (see next section)    a back reference (see next section)
# Line 837  which may be several bytes long (and the Line 1161  which may be several bytes long (and the
1161  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
1162  previous item and the quantifier were not present.  previous item and the quantifier were not present.
1163  .P  .P
1164  For convenience (and historical compatibility) the three most common  For convenience, the three most common quantifiers have single-character
1165  quantifiers have single-character abbreviations:  abbreviations:
1166  .sp  .sp
1167    *    is equivalent to {0,}    *    is equivalent to {0,}
1168    +    is equivalent to {1,}    +    is equivalent to {1,}
# Line 886  own right. Because it has two uses, it c Line 1210  own right. Because it has two uses, it c
1210  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
1211  way the rest of the pattern matches.  way the rest of the pattern matches.
1212  .P  .P
1213  If the PCRE_UNGREEDY option is set (an option which is not available in Perl),  If the PCRE_UNGREEDY option is set (an option that is not available in Perl),
1214  the quantifiers are not greedy by default, but individual ones can be made  the quantifiers are not greedy by default, but individual ones can be made
1215  greedy by following them with a question mark. In other words, it inverts the  greedy by following them with a question mark. In other words, it inverts the
1216  default behaviour.  default behaviour.
# Line 896  is greater than 1 or with a limited maxi Line 1220  is greater than 1 or with a limited maxi
1220  compiled pattern, in proportion to the size of the minimum or maximum.  compiled pattern, in proportion to the size of the minimum or maximum.
1221  .P  .P
1222  If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent  If a pattern starts with .* or .{0,} and the PCRE_DOTALL option (equivalent
1223  to Perl's /s) is set, thus allowing the . to match newlines, the pattern is  to Perl's /s) is set, thus allowing the dot to match newlines, the pattern is
1224  implicitly anchored, because whatever follows will be tried against every  implicitly anchored, because whatever follows will be tried against every
1225  character position in the subject string, so there is no point in retrying the  character position in the subject string, so there is no point in retrying the
1226  overall match at any position after the first. PCRE normally treats such a  overall match at any position after the first. PCRE normally treats such a
# Line 908  alternatively using ^ to indicate anchor Line 1232  alternatively using ^ to indicate anchor
1232  .P  .P
1233  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1234  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a backreference
1235  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 where a later one
1236  succeed. Consider, for example:  succeeds. Consider, for example:
1237  .sp  .sp
1238    (.*)abc\e1    (.*)abc\e1
1239  .sp  .sp
# Line 935  matches "aba" the value of the second ca Line 1259  matches "aba" the value of the second ca
1259  .SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS"  .SH "ATOMIC GROUPING AND POSSESSIVE QUANTIFIERS"
1260  .rs  .rs
1261  .sp  .sp
1262  With both maximizing and minimizing repetition, failure of what follows  With both maximizing ("greedy") and minimizing ("ungreedy" or "lazy")
1263  normally causes the repeated item to be re-evaluated to see if a different  repetition, failure of what follows normally causes the repeated item to be
1264  number of repeats allows the rest of the pattern to match. Sometimes it is  re-evaluated to see if a different number of repeats allows the rest of the
1265  useful to prevent this, either to change the nature of the match, or to cause  pattern to match. Sometimes it is useful to prevent this, either to change the
1266  it fail earlier than it otherwise might, when the author of the pattern knows  nature of the match, or to cause it fail earlier than it otherwise might, when
1267  there is no point in carrying on.  the author of the pattern knows there is no point in carrying on.
1268  .P  .P
1269  Consider, for example, the pattern \ed+foo when applied to the subject line  Consider, for example, the pattern \ed+foo when applied to the subject line
1270  .sp  .sp
# Line 952  item, and then with 4, and so on, before Line 1276  item, and then with 4, and so on, before
1276  (a term taken from Jeffrey Friedl's book) provides the means for specifying  (a term taken from Jeffrey Friedl's book) provides the means for specifying
1277  that once a subpattern has matched, it is not to be re-evaluated in this way.  that once a subpattern has matched, it is not to be re-evaluated in this way.
1278  .P  .P
1279  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 gives up
1280  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
1281  special parenthesis, starting with (?> as in this example:  special parenthesis, starting with (?> as in this example:
1282  .sp  .sp
# Line 982  previous example can be rewritten as Line 1306  previous example can be rewritten as
1306  .sp  .sp
1307    \ed++foo    \ed++foo
1308  .sp  .sp
1309    Note that a possessive quantifier can be used with an entire group, for
1310    example:
1311    .sp
1312      (abc|xyz){2,3}+
1313    .sp
1314  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1315  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
1316  atomic group. However, there is no difference in the meaning or processing of a  atomic group. However, there is no difference in the meaning of a possessive
1317  possessive quantifier and the equivalent atomic group.  quantifier and the equivalent atomic group, though there may be a performance
1318  .P  difference; possessive quantifiers should be slightly faster.
1319  The possessive quantifier syntax is an extension to the Perl syntax. It  .P
1320  originates in Sun's Java package.  The possessive quantifier syntax is an extension to the Perl 5.8 syntax.
1321    Jeffrey Friedl originated the idea (and the name) in the first edition of his
1322    book. Mike McCloskey liked it, so implemented it when he built Sun's Java
1323    package, and PCRE copied it from there. It ultimately found its way into Perl
1324    at release 5.10.
1325    .P
1326    PCRE has an optimization that automatically "possessifies" certain simple
1327    pattern constructs. For example, the sequence A+B is treated as A++B because
1328    there is no point in backtracking into a sequence of A's when B must follow.
1329  .P  .P
1330  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
1331  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
# Line 1030  However, if the decimal number following Line 1367  However, if the decimal number following
1367  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
1368  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
1369  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
1370  numbers less than 10. See the subsection entitled "Non-printing characters"  numbers less than 10. A "forward back reference" of this type can make sense
1371    when a repetition is involved and the subpattern to the right has participated
1372    in an earlier iteration.
1373    .P
1374    It is not possible to have a numerical "forward back reference" to a subpattern
1375    whose number is 10 or more using this syntax because a sequence such as \e50 is
1376    interpreted as a character defined in octal. See the subsection entitled
1377    "Non-printing characters"
1378  .\" HTML <a href="#digitsafterbackslash">  .\" HTML <a href="#digitsafterbackslash">
1379  .\" </a>  .\" </a>
1380  above  above
1381  .\"  .\"
1382  for further details of the handling of digits following a backslash.  for further details of the handling of digits following a backslash. There is
1383    no such problem when named parentheses are used. A back reference to any
1384    subpattern is possible using named parentheses (see below).
1385    .P
1386    Another way of avoiding the ambiguity inherent in the use of digits following a
1387    backslash is to use the \eg escape sequence, which is a feature introduced in
1388    Perl 5.10. This escape must be followed by an unsigned number or a negative
1389    number, optionally enclosed in braces. These examples are all identical:
1390    .sp
1391      (ring), \e1
1392      (ring), \eg1
1393      (ring), \eg{1}
1394    .sp
1395    An unsigned number specifies an absolute reference without the ambiguity that
1396    is present in the older syntax. It is also useful when literal digits follow
1397    the reference. A negative number is a relative reference. Consider this
1398    example:
1399    .sp
1400      (abc(def)ghi)\eg{-1}
1401    .sp
1402    The sequence \eg{-1} is a reference to the most recently started capturing
1403    subpattern before \eg, that is, is it equivalent to \e2. Similarly, \eg{-2}
1404    would be equivalent to \e1. The use of relative references can be helpful in
1405    long patterns, and also in patterns that are created by joining together
1406    fragments that contain references within themselves.
1407  .P  .P
1408  A back reference matches whatever actually matched the capturing subpattern in  A back reference matches whatever actually matched the capturing subpattern in
1409  the current subject string, rather than anything matching the subpattern  the current subject string, rather than anything matching the subpattern
# Line 1057  back reference, the case of letters is r Line 1425  back reference, the case of letters is r
1425  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
1426  capturing subpattern is matched caselessly.  capturing subpattern is matched caselessly.
1427  .P  .P
1428  Back references to named subpatterns use the Python syntax (?P=name). We could  There are several different ways of writing back references to named
1429  rewrite the above example as follows:  subpatterns. The .NET syntax \ek{name} and the Perl syntax \ek<name> or
1430  .sp  \ek'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified
1431    (?<p1>(?i)rah)\es+(?P=p1)  back reference syntax, in which \eg can be used for both numeric and named
1432    references, is also supported. We could rewrite the above example in any of
1433    the following ways:
1434    .sp
1435      (?<p1>(?i)rah)\es+\ek<p1>
1436      (?'p1'(?i)rah)\es+\ek{p1}
1437      (?P<p1>(?i)rah)\es+(?P=p1)
1438      (?<p1>(?i)rah)\es+\eg{p1}
1439  .sp  .sp
1440    A subpattern that is referenced by name may appear in the pattern before or
1441    after the reference.
1442    .P
1443  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
1444  subpattern has not actually been used in a particular match, any back  subpattern has not actually been used in a particular match, any back
1445  references to it always fail. For example, the pattern  references to it always fail. For example, the pattern
# Line 1123  because it does not make sense for negat Line 1501  because it does not make sense for negat
1501  .SS "Lookahead assertions"  .SS "Lookahead assertions"
1502  .rs  .rs
1503  .sp  .sp
1504  Lookahead assertions start  Lookahead assertions start with (?= for positive assertions and (?! for
1505  with (?= for positive assertions and (?! for negative assertions. For example,  negative assertions. For example,
1506  .sp  .sp
1507    \ew+(?=;)    \ew+(?=;)
1508  .sp  .sp
# Line 1159  negative assertions. For example, Line 1537  negative assertions. For example,
1537  .sp  .sp
1538  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
1539  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
1540  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
1541  all have to have the same fixed length. Thus  do not all have to have the same fixed length. Thus
1542  .sp  .sp
1543    (?<=bullock|donkey)    (?<=bullock|donkey)
1544  .sp  .sp
# Line 1180  lengths, but it is acceptable if rewritt Line 1558  lengths, but it is acceptable if rewritt
1558  .sp  .sp
1559    (?<=abc|abde)    (?<=abc|abde)
1560  .sp  .sp
1561    In some cases, the Perl 5.10 escape sequence \eK
1562    .\" HTML <a href="#resetmatchstart">
1563    .\" </a>
1564    (see above)
1565    .\"
1566    can be used instead of a lookbehind assertion; this is not restricted to a
1567    fixed-length.
1568    .P
1569  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1570  temporarily move the current position back by the fixed width and then try to  temporarily move the current position back by the fixed length and then try to
1571  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
1572  match is deemed to fail.  assertion fails.
1573  .P  .P
1574  PCRE does not allow the \eC escape (which matches a single byte in UTF-8 mode)  PCRE does not allow the \eC escape (which matches a single byte in UTF-8 mode)
1575  to appear in lookbehind assertions, because it makes it impossible to calculate  to appear in lookbehind assertions, because it makes it impossible to calculate
1576  the length of the lookbehind. The \eX escape, which can match different numbers  the length of the lookbehind. The \eX and \eR escapes, which can match
1577  of bytes, is also not permitted.  different numbers of bytes, are also not permitted.
1578  .P  .P
1579  Atomic groups can be used in conjunction with lookbehind assertions to specify  Possessive quantifiers can be used in conjunction with lookbehind assertions to
1580  efficient matching at the end of the subject string. Consider a simple pattern  specify efficient matching at the end of the subject string. Consider a simple
1581  such as  pattern such as
1582  .sp  .sp
1583    abcd$    abcd$
1584  .sp  .sp
# Line 1208  then all but the last two characters, an Line 1594  then all but the last two characters, an
1594  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,
1595  if the pattern is written as  if the pattern is written as
1596  .sp  .sp
   ^(?>.*)(?<=abcd)  
 .sp  
 or, equivalently, using the possessive quantifier syntax,  
 .sp  
1597    ^.*+(?<=abcd)    ^.*+(?<=abcd)
1598  .sp  .sp
1599  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
1600  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
1601  characters. If it fails, the match fails immediately. For long strings, this  characters. If it fails, the match fails immediately. For long strings, this
1602  approach makes a significant difference to the processing time.  approach makes a significant difference to the processing time.
# Line 1254  is another pattern that matches "foo" pr Line 1636  is another pattern that matches "foo" pr
1636  characters that are not "999".  characters that are not "999".
1637  .  .
1638  .  .
1639    .\" HTML <a name="conditions"></a>
1640  .SH "CONDITIONAL SUBPATTERNS"  .SH "CONDITIONAL SUBPATTERNS"
1641  .rs  .rs
1642  .sp  .sp
# Line 1269  If the condition is satisfied, the yes-p Line 1652  If the condition is satisfied, the yes-p
1652  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
1653  subpattern, a compile-time error occurs.  subpattern, a compile-time error occurs.
1654  .P  .P
1655  There are three kinds of condition. If the text between the parentheses  There are four kinds of condition: references to subpatterns, references to
1656  consists of a sequence of digits, the condition is satisfied if the capturing  recursion, a pseudo-condition called DEFINE, and assertions.
1657  subpattern of that number has previously matched. The number must be greater  .
1658  than zero. Consider the following pattern, which contains non-significant white  .SS "Checking for a used subpattern by number"
1659  space to make it more readable (assume the PCRE_EXTENDED option) and to divide  .rs
1660  it into three parts for ease of discussion:  .sp
1661    If the text between the parentheses consists of a sequence of digits, the
1662    condition is true if the capturing subpattern of that number has previously
1663    matched. An alternative notation is to precede the digits with a plus or minus
1664    sign. In this case, the subpattern number is relative rather than absolute.
1665    The most recently opened parentheses can be referenced by (?(-1), the next most
1666    recent by (?(-2), and so on. In looping constructs it can also make sense to
1667    refer to subsequent groups with constructs such as (?(+2).
1668    .P
1669    Consider the following pattern, which contains non-significant white space to
1670    make it more readable (assume the PCRE_EXTENDED option) and to divide it into
1671    three parts for ease of discussion:
1672  .sp  .sp
1673    ( \e( )?    [^()]+    (?(1) \e) )    ( \e( )?    [^()]+    (?(1) \e) )
1674  .sp  .sp
# Line 1288  parenthesis is required. Otherwise, sinc Line 1682  parenthesis is required. Otherwise, sinc
1682  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1683  non-parentheses, optionally enclosed in parentheses.  non-parentheses, optionally enclosed in parentheses.
1684  .P  .P
1685  If the condition is the string (R), it is satisfied if a recursive call to the  If you were embedding this pattern in a larger one, you could use a relative
1686  pattern or subpattern has been made. At "top level", the condition is false.  reference:
1687  This is a PCRE extension. Recursive patterns are described in the next section.  .sp
1688      ...other stuff... ( \e( )?    [^()]+    (?(-1) \e) ) ...
1689    .sp
1690    This makes the fragment independent of the parentheses in the larger pattern.
1691    .
1692    .SS "Checking for a used subpattern by name"
1693    .rs
1694    .sp
1695    Perl uses the syntax (?(<name>)...) or (?('name')...) to test for a used
1696    subpattern by name. For compatibility with earlier versions of PCRE, which had
1697    this facility before Perl, the syntax (?(name)...) is also recognized. However,
1698    there is a possible ambiguity with this syntax, because subpattern names may
1699    consist entirely of digits. PCRE looks first for a named subpattern; if it
1700    cannot find one and the name consists entirely of digits, PCRE looks for a
1701    subpattern of that number, which must be greater than zero. Using subpattern
1702    names that consist entirely of digits is not recommended.
1703    .P
1704    Rewriting the above example to use a named subpattern gives this:
1705    .sp
1706      (?<OPEN> \e( )?    [^()]+    (?(<OPEN>) \e) )
1707    .sp
1708    .
1709    .SS "Checking for pattern recursion"
1710    .rs
1711    .sp
1712    If the condition is the string (R), and there is no subpattern with the name R,
1713    the condition is true if a recursive call to the whole pattern or any
1714    subpattern has been made. If digits or a name preceded by ampersand follow the
1715    letter R, for example:
1716    .sp
1717      (?(R3)...) or (?(R&name)...)
1718    .sp
1719    the condition is true if the most recent recursion is into the subpattern whose
1720    number or name is given. This condition does not check the entire recursion
1721    stack.
1722    .P
1723    At "top level", all these recursion test conditions are false. Recursive
1724    patterns are described below.
1725    .
1726    .SS "Defining subpatterns for use by reference only"
1727    .rs
1728    .sp
1729    If the condition is the string (DEFINE), and there is no subpattern with the
1730    name DEFINE, the condition is always false. In this case, there may be only one
1731    alternative in the subpattern. It is always skipped if control reaches this
1732    point in the pattern; the idea of DEFINE is that it can be used to define
1733    "subroutines" that can be referenced from elsewhere. (The use of "subroutines"
1734    is described below.) For example, a pattern to match an IPv4 address could be
1735    written like this (ignore whitespace and line breaks):
1736    .sp
1737      (?(DEFINE) (?<byte> 2[0-4]\ed | 25[0-5] | 1\ed\ed | [1-9]?\ed) )
1738      \eb (?&byte) (\e.(?&byte)){3} \eb
1739    .sp
1740    The first part of the pattern is a DEFINE group inside which a another group
1741    named "byte" is defined. This matches an individual component of an IPv4
1742    address (a number less than 256). When matching takes place, this part of the
1743    pattern is skipped because DEFINE acts like a false condition.
1744  .P  .P
1745  If the condition is not a sequence of digits or (R), it must be an assertion.  The rest of the pattern uses references to the named group to match the four
1746    dot-separated components of an IPv4 address, insisting on a word boundary at
1747    each end.
1748    .
1749    .SS "Assertion conditions"
1750    .rs
1751    .sp
1752    If the condition is not in any of the above formats, it must be an assertion.
1753  This may be a positive or negative lookahead or lookbehind assertion. Consider  This may be a positive or negative lookahead or lookbehind assertion. Consider
1754  this pattern, again containing non-significant white space, and with the two  this pattern, again containing non-significant white space, and with the two
1755  alternatives on the second line:  alternatives on the second line:
# Line 1317  closing parenthesis. Nested parentheses Line 1774  closing parenthesis. Nested parentheses
1774  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.
1775  .P  .P
1776  If the PCRE_EXTENDED option is set, an unescaped # character outside a  If the PCRE_EXTENDED option is set, an unescaped # character outside a
1777  character class introduces a comment that continues up to the next newline  character class introduces a comment that continues to immediately after the
1778  character in the pattern.  next newline in the pattern.
1779  .  .
1780  .  .
1781    .\" HTML <a name="recursion"></a>
1782  .SH "RECURSIVE PATTERNS"  .SH "RECURSIVE PATTERNS"
1783  .rs  .rs
1784  .sp  .sp
1785  Consider the problem of matching a string in parentheses, allowing for  Consider the problem of matching a string in parentheses, allowing for
1786  unlimited nested parentheses. Without the use of recursion, the best that can  unlimited nested parentheses. Without the use of recursion, the best that can
1787  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
1788  is not possible to handle an arbitrary nesting depth. Perl provides a facility  is not possible to handle an arbitrary nesting depth.
1789  that allows regular expressions to recurse (amongst other things). It does this  .P
1790  by interpolating Perl code in the expression at run time, and the code can  For some time, Perl has provided a facility that allows regular expressions to
1791  refer to the expression itself. A Perl pattern to solve the parentheses problem  recurse (amongst other things). It does this by interpolating Perl code in the
1792  can be created like this:  expression at run time, and the code can refer to the expression itself. A Perl
1793    pattern using code interpolation to solve the parentheses problem can be
1794    created like this:
1795  .sp  .sp
1796    $re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x;    $re = qr{\e( (?: (?>[^()]+) | (?p{$re}) )* \e)}x;
1797  .sp  .sp
1798  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
1799  recursively to the pattern in which it appears. Obviously, PCRE cannot support  recursively to the pattern in which it appears.
1800  the interpolation of Perl code. Instead, it supports some special syntax for  .P
1801  recursion of the entire pattern, and also for individual subpattern recursion.  Obviously, PCRE cannot support the interpolation of Perl code. Instead, it
1802  .P  supports special syntax for recursion of the entire pattern, and also for
1803  The special item that consists of (? followed by a number greater than zero and  individual subpattern recursion. After its introduction in PCRE and Python,
1804  a closing parenthesis is a recursive call of the subpattern of the given  this kind of recursion was introduced into Perl at release 5.10.
1805  number, provided that it occurs inside that subpattern. (If not, it is a  .P
1806  "subroutine" call, which is described in the next section.) The special item  A special item that consists of (? followed by a number greater than zero and a
1807  (?R) is a recursive call of the entire regular expression.  closing parenthesis is a recursive call of the subpattern of the given number,
1808    provided that it occurs inside that subpattern. (If not, it is a "subroutine"
1809    call, which is described in the next section.) The special item (?R) or (?0) is
1810    a recursive call of the entire regular expression.
1811    .P
1812    In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
1813    treated as an atomic group. That is, once it has matched some of the subject
1814    string, it is never re-entered, even if it contains untried alternatives and
1815    there is a subsequent matching failure.
1816  .P  .P
1817  For example, this PCRE pattern solves the nested parentheses problem (assume  This PCRE pattern solves the nested parentheses problem (assume the
1818  the PCRE_EXTENDED option is set so that white space is ignored):  PCRE_EXTENDED option is set so that white space is ignored):
1819  .sp  .sp
1820    \e( ( (?>[^()]+) | (?R) )* \e)    \e( ( (?>[^()]+) | (?R) )* \e)
1821  .sp  .sp
1822  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
1823  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
1824  match of the pattern itself (that is a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
1825  Finally there is a closing parenthesis.  Finally there is a closing parenthesis.
1826  .P  .P
1827  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
# Line 1362  pattern, so instead you could use this: Line 1830  pattern, so instead you could use this:
1830    ( \e( ( (?>[^()]+) | (?1) )* \e) )    ( \e( ( (?>[^()]+) | (?1) )* \e) )
1831  .sp  .sp
1832  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
1833  them instead of the whole pattern. In a larger pattern, keeping track of  them instead of the whole pattern.
1834  parenthesis numbers can be tricky. It may be more convenient to use named  .P
1835  parentheses instead. For this, PCRE uses (?P>name), which is an extension to  In a larger pattern, keeping track of parenthesis numbers can be tricky. This
1836  the Python syntax that PCRE uses for named parentheses (Perl does not provide  is made easier by the use of relative references. (A Perl 5.10 feature.)
1837  named parentheses). We could rewrite the above example as follows:  Instead of (?1) in the pattern above you can write (?-2) to refer to the second
1838  .sp  most recently opened parentheses preceding the recursion. In other words, a
1839    (?P<pn> \e( ( (?>[^()]+) | (?P>pn) )* \e) )  negative number counts capturing parentheses leftwards from the point at which
1840  .sp  it is encountered.
1841  This particular example pattern contains nested unlimited repeats, and so the  .P
1842  use of atomic grouping for matching strings of non-parentheses is important  It is also possible to refer to subsequently opened parentheses, by writing
1843  when applying the pattern to strings that do not match. For example, when this  references such as (?+2). However, these cannot be recursive because the
1844  pattern is applied to  reference is not inside the parentheses that are referenced. They are always
1845    "subroutine" calls, as described in the next section.
1846    .P
1847    An alternative approach is to use named parentheses instead. The Perl syntax
1848    for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We
1849    could rewrite the above example as follows:
1850    .sp
1851      (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )
1852    .sp
1853    If there is more than one subpattern with the same name, the earliest one is
1854    used.
1855    .P
1856    This particular example pattern that we have been looking at contains nested
1857    unlimited repeats, and so the use of atomic grouping for matching strings of
1858    non-parentheses is important when applying the pattern to strings that do not
1859    match. For example, when this pattern is applied to
1860  .sp  .sp
1861    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1862  .sp  .sp
# Line 1385  before failure can be reported. Line 1868  before failure can be reported.
1868  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
1869  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.
1870  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
1871  the next section and the  below and the
1872  .\" HREF  .\" HREF
1873  \fBpcrecallout\fP  \fBpcrecallout\fP
1874  .\"  .\"
# Line 1424  is the actual recursive call. Line 1907  is the actual recursive call.
1907  .sp  .sp
1908  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
1909  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
1910  subroutine in a programming language. An earlier example pointed out that the  subroutine in a programming language. The "called" subpattern may be defined
1911  pattern  before or after the reference. A numbered reference can be absolute or
1912    relative, as in these examples:
1913    .sp
1914      (...(absolute)...)...(?2)...
1915      (...(relative)...)...(?-1)...
1916      (...(?+1)...(relative)...
1917    .sp
1918    An earlier example pointed out that the pattern
1919  .sp  .sp
1920    (sens|respons)e and \e1ibility    (sens|respons)e and \e1ibility
1921  .sp  .sp
# Line 1435  matches "sense and sensibility" and "res Line 1925  matches "sense and sensibility" and "res
1925    (sens|respons)e and (?1)ibility    (sens|respons)e and (?1)ibility
1926  .sp  .sp
1927  is used, it does match "sense and responsibility" as well as the other two  is used, it does match "sense and responsibility" as well as the other two
1928  strings. Such references must, however, follow the subpattern to which they  strings. Another example is given in the discussion of DEFINE above.
1929  refer.  .P
1930    Like recursive subpatterns, a "subroutine" call is always treated as an atomic
1931    group. That is, once it has matched some of the subject string, it is never
1932    re-entered, even if it contains untried alternatives and there is a subsequent
1933    matching failure.
1934    .P
1935    When a subpattern is used as a subroutine, processing options such as
1936    case-independence are fixed when the subpattern is defined. They cannot be
1937    changed for different calls. For example, consider this pattern:
1938    .sp
1939      (abc)(?i:(?-1))
1940    .sp
1941    It matches "abcabc". It does not match "abcABC" because the change of
1942    processing option does not affect the called subpattern.
1943  .  .
1944  .  .
1945  .SH CALLOUTS  .SH CALLOUTS
# Line 1457  function is to be called. If you want to Line 1960  function is to be called. If you want to
1960  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.
1961  For example, this pattern has two callout points:  For example, this pattern has two callout points:
1962  .sp  .sp
1963    (?C1)\dabc(?C2)def    (?C1)abc(?C2)def
1964  .sp  .sp
1965  If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are  If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are
1966  automatically installed before each item in the pattern. They are all numbered  automatically installed before each item in the pattern. They are all numbered
# Line 1473  description of the interface to the call Line 1976  description of the interface to the call
1976  \fBpcrecallout\fP  \fBpcrecallout\fP
1977  .\"  .\"
1978  documentation.  documentation.
1979    .
1980    .
1981    .SH "BACTRACKING CONTROL"
1982    .rs
1983    .sp
1984    Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
1985    are described in the Perl documentation as "experimental and subject to change
1986    or removal in a future version of Perl". It goes on to say: "Their usage in
1987    production code should be noted to avoid problems during upgrades." The same
1988    remarks apply to the PCRE features described in this section.
1989    .P
1990    Since these verbs are specifically related to backtracking, they can be used
1991    only when the pattern is to be matched using \fBpcre_exec()\fP, which uses a
1992    backtracking algorithm. They cause an error if encountered by
1993    \fBpcre_dfa_exec()\fP.
1994  .P  .P
1995  .in 0  The new verbs make use of what was previously invalid syntax: an opening
1996  Last updated: 28 February 2005  parenthesis followed by an asterisk. In Perl, they are generally of the form
1997  .br  (*VERB:ARG) but PCRE does not support the use of arguments, so its general
1998  Copyright (c) 1997-2005 University of Cambridge.  form is just (*VERB). Any number of these verbs may occur in a pattern. There
1999    are two kinds:
2000    .
2001    .SS "Verbs that act immediately"
2002    .rs
2003    .sp
2004    The following verbs act as soon as they are encountered:
2005    .sp
2006       (*ACCEPT)
2007    .sp
2008    This verb causes the match to end successfully, skipping the remainder of the
2009    pattern. When inside a recursion, only the innermost pattern is ended
2010    immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside
2011    capturing parentheses. In Perl, the data so far is captured: in PCRE no data is
2012    captured. For example:
2013    .sp
2014      A(A|B(*ACCEPT)|C)D
2015    .sp
2016    This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is
2017    captured.
2018    .sp
2019      (*FAIL) or (*F)
2020    .sp
2021    This verb causes the match to fail, forcing backtracking to occur. It is
2022    equivalent to (?!) but easier to read. The Perl documentation notes that it is
2023    probably useful only when combined with (?{}) or (??{}). Those are, of course,
2024    Perl features that are not present in PCRE. The nearest equivalent is the
2025    callout feature, as for example in this pattern:
2026    .sp
2027      a+(?C)(*FAIL)
2028    .sp
2029    A match with the string "aaaa" always fails, but the callout is taken before
2030    each backtrack happens (in this example, 10 times).
2031    .
2032    .SS "Verbs that act after backtracking"
2033    .rs
2034    .sp
2035    The following verbs do nothing when they are encountered. Matching continues
2036    with what follows, but if there is no subsequent match, a failure is forced.
2037    The verbs differ in exactly what kind of failure occurs.
2038    .sp
2039      (*COMMIT)
2040    .sp
2041    This verb causes the whole match to fail outright if the rest of the pattern
2042    does not match. Even if the pattern is unanchored, no further attempts to find
2043    a match by advancing the start point take place. Once (*COMMIT) has been
2044    passed, \fBpcre_exec()\fP is committed to finding a match at the current
2045    starting point, or not at all. For example:
2046    .sp
2047      a+(*COMMIT)b
2048    .sp
2049    This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2050    dynamic anchor, or "I've started, so I must finish."
2051    .sp
2052      (*PRUNE)
2053    .sp
2054    This verb causes the match to fail at the current position if the rest of the
2055    pattern does not match. If the pattern is unanchored, the normal "bumpalong"
2056    advance to the next starting character then happens. Backtracking can occur as
2057    usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but
2058    if there is no match to the right, backtracking cannot cross (*PRUNE).
2059    In simple cases, the use of (*PRUNE) is just an alternative to an atomic
2060    group or possessive quantifier, but there are some uses of (*PRUNE) that cannot
2061    be expressed in any other way.
2062    .sp
2063      (*SKIP)
2064    .sp
2065    This verb is like (*PRUNE), except that if the pattern is unanchored, the
2066    "bumpalong" advance is not to the next character, but to the position in the
2067    subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text
2068    was matched leading up to it cannot be part of a successful match. Consider:
2069    .sp
2070      a+(*SKIP)b
2071    .sp
2072    If the subject is "aaaac...", after the first match attempt fails (starting at
2073    the first character in the string), the starting point skips on to start the
2074    next attempt at "c". Note that a possessive quantifer does not have the same
2075    effect in this example; although it would suppress backtracking during the
2076    first match attempt, the second attempt would start at the second character
2077    instead of skipping on to "c".
2078    .sp
2079      (*THEN)
2080    .sp
2081    This verb causes a skip to the next alternation if the rest of the pattern does
2082    not match. That is, it cancels pending backtracking, but only within the
2083    current alternation. Its name comes from the observation that it can be used
2084    for a pattern-based if-then-else block:
2085    .sp
2086      ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2087    .sp
2088    If the COND1 pattern matches, FOO is tried (and possibly further items after
2089    the end of the group if FOO succeeds); on failure the matcher skips to the
2090    second alternative and tries COND2, without backtracking into COND1. If (*THEN)
2091    is used outside of any alternation, it acts exactly like (*PRUNE).
2092    .
2093    .
2094    .SH "SEE ALSO"
2095    .rs
2096    .sp
2097    \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3).
2098    .
2099    .
2100    .SH AUTHOR
2101    .rs
2102    .sp
2103    .nf
2104    Philip Hazel
2105    University Computing Service
2106    Cambridge CB2 3QH, England.
2107    .fi
2108    .
2109    .
2110    .SH REVISION
2111    .rs
2112    .sp
2113    .nf
2114    Last updated: 09 August 2007
2115    Copyright (c) 1997-2007 University of Cambridge.
2116    .fi

Legend:
Removed from v.79  
changed lines
  Added in v.211

  ViewVC Help
Powered by ViewVC 1.1.5