/[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 178 by ph10, Wed Jun 13 08:44:34 2007 UTC revision 333 by ph10, Thu Apr 10 19:55:57 2008 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. PCRE tries to match Perl syntax and semantics as closely as it can. PCRE
13    also supports some alternative regular expression syntax (which does not
14    conflict with the Perl syntax) in order to provide some compatibility with
15    regular expressions in Python, .NET, and Oniguruma.
16    .P
17    Perl's regular expressions are described in its own documentation, and
18    regular expressions in general are covered in a number of books, some of which
19    have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
20    published by O'Reilly, covers regular expressions in great detail. This
21    description of PCRE's regular expressions is intended as reference material.
22  .P  .P
23  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,
24  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 40  discussed in the Line 49  discussed in the
49  page.  page.
50  .  .
51  .  .
52    .SH "NEWLINE CONVENTIONS"
53    .rs
54    .sp
55    PCRE supports five different conventions for indicating line breaks in
56    strings: a single CR (carriage return) character, a single LF (linefeed)
57    character, the two-character sequence CRLF, any of the three preceding, or any
58    Unicode newline sequence. The
59    .\" HREF
60    \fBpcreapi\fP
61    .\"
62    page has
63    .\" HTML <a href="pcreapi.html#newlines">
64    .\" </a>
65    further discussion
66    .\"
67    about newlines, and shows how to set the newline convention in the
68    \fIoptions\fP arguments for the compiling and matching functions.
69    .P
70    It is also possible to specify a newline convention by starting a pattern
71    string with one of the following five sequences:
72    .sp
73      (*CR)        carriage return
74      (*LF)        linefeed
75      (*CRLF)      carriage return, followed by linefeed
76      (*ANYCRLF)   any of the three above
77      (*ANY)       all Unicode newline sequences
78    .sp
79    These override the default and the options given to \fBpcre_compile()\fP. For
80    example, on a Unix system where LF is the default newline sequence, the pattern
81    .sp
82      (*CR)a.b
83    .sp
84    changes the convention to CR. That pattern matches "a\enb" because LF is no
85    longer a newline. Note that these special settings, which are not
86    Perl-compatible, are recognized only at the very start of a pattern, and that
87    they must be in upper case. If more than one of them is present, the last one
88    is used.
89    .P
90    The newline convention does not affect what the \eR escape sequence matches. By
91    default, this is any Unicode newline sequence, for Perl compatibility. However,
92    this can be changed; see the description of \eR in the section entitled
93    .\" HTML <a href="#newlineseq">
94    .\" </a>
95    "Newline sequences"
96    .\"
97    below. A change of \eR setting can be combined with a change of newline
98    convention.
99    .
100    .
101  .SH "CHARACTERS AND METACHARACTERS"  .SH "CHARACTERS AND METACHARACTERS"
102  .rs  .rs
103  .sp  .sp
# Line 149  represents: Line 207  represents:
207    \ecx       "control-x", where x is any character    \ecx       "control-x", where x is any character
208    \ee        escape (hex 1B)    \ee        escape (hex 1B)
209    \ef        formfeed (hex 0C)    \ef        formfeed (hex 0C)
210    \en        newline (hex 0A)    \en        linefeed (hex 0A)
211    \er        carriage return (hex 0D)    \er        carriage return (hex 0D)
212    \et        tab (hex 09)    \et        tab (hex 09)
213    \eddd      character with octal code ddd, or backreference    \eddd      character with octal code ddd, or backreference
# Line 164  Thus \ecz becomes hex 1A, but \ec{ becom Line 222  Thus \ecz becomes hex 1A, but \ec{ becom
222  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
223  upper or lower case). Any number of hexadecimal digits may appear between \ex{  upper or lower case). Any number of hexadecimal digits may appear between \ex{
224  and }, but the value of the character code must be less than 256 in non-UTF-8  and }, but the value of the character code must be less than 256 in non-UTF-8
225  mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value  mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in
226  is 7FFFFFFF). If characters other than hexadecimal digits appear between \ex{  hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code
227  and }, or if there is no terminating }, this form of escape is not recognized.  point, which is 10FFFF.
228  Instead, the initial \ex will be interpreted as a basic hexadecimal escape,  .P
229  with no following digits, giving a character whose value is zero.  If characters other than hexadecimal digits appear between \ex{ and }, or if
230    there is no terminating }, this form of escape is not recognized. Instead, the
231    initial \ex will be interpreted as a basic hexadecimal escape, with no
232    following digits, giving a character whose value is zero.
233  .P  .P
234  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
235  syntaxes for \ex. There is no difference in the way they are handled. For  syntaxes for \ex. There is no difference in the way they are handled. For
# Line 240  meanings Line 301  meanings
301  .SS "Absolute and relative back references"  .SS "Absolute and relative back references"
302  .rs  .rs
303  .sp  .sp
304  The sequence \eg followed by a positive or negative number, optionally enclosed  The sequence \eg followed by an unsigned or a negative number, optionally
305  in braces, is an absolute or relative back reference. A named back reference  enclosed in braces, is an absolute or relative back reference. A named back
306  can be coded as \eg{name}. Back references are discussed  reference can be coded as \eg{name}. Back references are discussed
307  .\" HTML <a href="#backreferences">  .\" HTML <a href="#backreferences">
308  .\" </a>  .\" </a>
309  later,  later,
# Line 254  parenthesized subpatterns. Line 315  parenthesized subpatterns.
315  .\"  .\"
316  .  .
317  .  .
318    .SS "Absolute and relative subroutine calls"
319    .rs
320    .sp
321    For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
322    a number enclosed either in angle brackets or single quotes, is an alternative
323    syntax for referencing a subpattern as a "subroutine". Details are discussed
324    .\" HTML <a href="#onigurumasubroutines">
325    .\" </a>
326    later.
327    .\"
328    Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
329    synonymous. The former is a back reference; the latter is a subroutine call.
330    .
331    .
332  .SS "Generic character types"  .SS "Generic character types"
333  .rs  .rs
334  .sp  .sp
335  Another use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types. The
336  following are always recognized:  following are always recognized:
337  .sp  .sp
338    \ed     any decimal digit    \ed     any decimal digit
339    \eD     any character that is not a decimal digit    \eD     any character that is not a decimal digit
340    \eh     any horizontal whitespace character    \eh     any horizontal whitespace character
341    \eH     any character that is not a horizontal whitespace character    \eH     any character that is not a horizontal whitespace character
342    \es     any whitespace character    \es     any whitespace character
343    \eS     any character that is not a whitespace character    \eS     any character that is not a whitespace character
344    \ev     any vertical whitespace character    \ev     any vertical whitespace character
345    \eV     any character that is not a vertical whitespace character    \eV     any character that is not a vertical whitespace character
346    \ew     any "word" character    \ew     any "word" character
347    \eW     any "non-word" character    \eW     any "non-word" character
348  .sp  .sp
# Line 287  does. Line 362  does.
362  .P  .P
363  In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or  In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or
364  \ew, and always match \eD, \eS, and \eW. This is true even when Unicode  \ew, and always match \eD, \eS, and \eW. This is true even when Unicode
365  character property support is available. These sequences retain their original  character property support is available. These sequences retain their original
366  meanings from before UTF-8 support was available, mainly for efficiency  meanings from before UTF-8 support was available, mainly for efficiency
367  reasons.  reasons.
368  .P  .P
369  The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the  The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the
370  other sequences, these do match certain high-valued codepoints in UTF-8 mode.  other sequences, these do match certain high-valued codepoints in UTF-8 mode.
371  The horizontal space characters are:  The horizontal space characters are:
372  .sp  .sp
# Line 343  accented letters, and these are matched Line 418  accented letters, and these are matched
418  is discouraged.  is discouraged.
419  .  .
420  .  .
421    .\" HTML <a name="newlineseq"></a>
422  .SS "Newline sequences"  .SS "Newline sequences"
423  .rs  .rs
424  .sp  .sp
425  Outside a character class, the escape sequence \eR matches any Unicode newline  Outside a character class, by default, the escape sequence \eR matches any
426  sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \eR is equivalent to  Unicode newline sequence. This is a Perl 5.10 feature. In non-UTF-8 mode \eR is
427  the following:  equivalent to the following:
428  .sp  .sp
429    (?>\er\en|\en|\ex0b|\ef|\er|\ex85)    (?>\er\en|\en|\ex0b|\ef|\er|\ex85)
430  .sp  .sp
# Line 368  are added: LS (line separator, U+2028) a Line 444  are added: LS (line separator, U+2028) a
444  Unicode character property support is not needed for these characters to be  Unicode character property support is not needed for these characters to be
445  recognized.  recognized.
446  .P  .P
447    It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
448    complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
449    either at compile time or when the pattern is matched. (BSR is an abbrevation
450    for "backslash R".) This can be made the default when PCRE is built; if this is
451    the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
452    It is also possible to specify these settings by starting a pattern string with
453    one of the following sequences:
454    .sp
455      (*BSR_ANYCRLF)   CR, LF, or CRLF only
456      (*BSR_UNICODE)   any Unicode newline sequence
457    .sp
458    These override the default and the options given to \fBpcre_compile()\fP, but
459    they can be overridden by options given to \fBpcre_exec()\fP. Note that these
460    special settings, which are not Perl-compatible, are recognized only at the
461    very start of a pattern, and that they must be in upper case. If more than one
462    of them is present, the last one is used. They can be combined with a change of
463    newline convention, for example, a pattern can start with:
464    .sp
465      (*ANY)(*BSR_ANYCRLF)
466    .sp
467  Inside a character class, \eR matches the letter "R".  Inside a character class, \eR matches the letter "R".
468  .  .
469  .  .
# Line 376  Inside a character class, \eR matches th Line 472  Inside a character class, \eR matches th
472  .rs  .rs
473  .sp  .sp
474  When PCRE is built with Unicode character property support, three additional  When PCRE is built with Unicode character property support, three additional
475  escape sequences to match character properties are available when UTF-8 mode  escape sequences that match characters with specific properties are available.
476  is selected. They are:  When not in UTF-8 mode, these sequences are of course limited to testing
477    characters whose codepoints are less than 256, but they do work in this mode.
478    The extra escape sequences are:
479  .sp  .sp
480    \ep{\fIxx\fP}   a character with the \fIxx\fP property    \ep{\fIxx\fP}   a character with the \fIxx\fP property
481    \eP{\fIxx\fP}   a character without the \fIxx\fP property    \eP{\fIxx\fP}   a character without the \fIxx\fP property
# Line 529  The special property L& is also supporte Line 627  The special property L& is also supporte
627  the Lu, Ll, or Lt property, in other words, a letter that is not classified as  the Lu, Ll, or Lt property, in other words, a letter that is not classified as
628  a modifier or "other".  a modifier or "other".
629  .P  .P
630    The Cs (Surrogate) property applies only to characters in the range U+D800 to
631    U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so
632    cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
633    (see the discussion of PCRE_NO_UTF8_CHECK in the
634    .\" HREF
635    \fBpcreapi\fP
636    .\"
637    page).
638    .P
639  The long synonyms for these properties that Perl supports (such as \ep{Letter})  The long synonyms for these properties that Perl supports (such as \ep{Letter})
640  are not supported by PCRE, nor is it permitted to prefix any of these  are not supported by PCRE, nor is it permitted to prefix any of these
641  properties with "Is".  properties with "Is".
# Line 553  atomic group Line 660  atomic group
660  (see below).  (see below).
661  .\"  .\"
662  Characters with the "mark" property are typically accents that affect the  Characters with the "mark" property are typically accents that affect the
663  preceding character.  preceding character. None of them have codepoints less than 256, so in
664    non-UTF-8 mode \eX matches any one character.
665  .P  .P
666  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
667  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 903  alternative in the subpattern. Line 1011  alternative in the subpattern.
1011  .rs  .rs
1012  .sp  .sp
1013  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1014  PCRE_EXTENDED options can be changed from within the pattern by a sequence of  PCRE_EXTENDED options (which are Perl-compatible) can be changed from within
1015  Perl option letters enclosed between "(?" and ")". The option letters are  the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1016    The option letters are
1017  .sp  .sp
1018    i  for PCRE_CASELESS    i  for PCRE_CASELESS
1019    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
# Line 918  PCRE_MULTILINE while unsetting PCRE_DOTA Line 1027  PCRE_MULTILINE while unsetting PCRE_DOTA
1027  permitted. If a letter appears both before and after the hyphen, the option is  permitted. If a letter appears both before and after the hyphen, the option is
1028  unset.  unset.
1029  .P  .P
1030    The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
1031    changed in the same way as the Perl-compatible options by using the characters
1032    J, U and X respectively.
1033    .P
1034  When an option change occurs at top level (that is, not inside subpattern  When an option change occurs at top level (that is, not inside subpattern
1035  parentheses), the change applies to the remainder of the pattern that follows.  parentheses), the change applies to the remainder of the pattern that follows.
1036  If the change is placed right at the start of a pattern, PCRE extracts it into  If the change is placed right at the start of a pattern, PCRE extracts it into
# Line 941  branch is abandoned before the option se Line 1054  branch is abandoned before the option se
1054  option settings happen at compile time. There would be some very weird  option settings happen at compile time. There would be some very weird
1055  behaviour otherwise.  behaviour otherwise.
1056  .P  .P
1057  The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be  \fBNote:\fP There are other PCRE-specific options that can be set by the
1058  changed in the same way as the Perl-compatible options by using the characters  application when the compile or match functions are called. In some cases the
1059  J, U and X respectively.  pattern can contain special leading sequences to override what the application
1060    has set or what has been defaulted. Details are given in the section entitled
1061    .\" HTML <a href="#newlineseq">
1062    .\" </a>
1063    "Newline sequences"
1064    .\"
1065    above.
1066  .  .
1067  .  .
1068  .\" HTML <a name="subpattern"></a>  .\" HTML <a name="subpattern"></a>
# Line 1001  the above patterns match "SUNDAY" as wel Line 1120  the above patterns match "SUNDAY" as wel
1120  .SH "DUPLICATE SUBPATTERN NUMBERS"  .SH "DUPLICATE SUBPATTERN NUMBERS"
1121  .rs  .rs
1122  .sp  .sp
1123  Perl 5.10 introduced a feature whereby each alternative in a subpattern uses  Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
1124  the same numbers for its capturing parentheses. Such a subpattern starts with  the same numbers for its capturing parentheses. Such a subpattern starts with
1125  (?| and is itself a non-capturing subpattern. For example, consider this  (?| and is itself a non-capturing subpattern. For example, consider this
1126  pattern:  pattern:
1127  .sp  .sp
1128    (?|(Sat)ur|(Sun))day    (?|(Sat)ur|(Sun))day
1129  .sp  .sp
1130  Because the two alternatives are inside a (?| group, both sets of capturing  Because the two alternatives are inside a (?| group, both sets of capturing
1131  parentheses are numbered one. Thus, when the pattern matches, you can look  parentheses are numbered one. Thus, when the pattern matches, you can look
1132  at captured substring number one, whichever alternative matched. This construct  at captured substring number one, whichever alternative matched. This construct
1133  is useful when you want to capture part, but not all, of one of a number of  is useful when you want to capture part, but not all, of one of a number of
1134  alternatives. Inside a (?| group, parentheses are numbered as usual, but the  alternatives. Inside a (?| group, parentheses are numbered as usual, but the
1135  number is reset at the start of each branch. The numbers of any capturing  number is reset at the start of each branch. The numbers of any capturing
1136  buffers that follow the subpattern start after the highest number used in any  buffers that follow the subpattern start after the highest number used in any
1137  branch. The following example is taken from the Perl documentation.  branch. The following example is taken from the Perl documentation.
1138  The numbers underneath show in which buffer the captured content will be  The numbers underneath show in which buffer the captured content will be
1139  stored.  stored.
1140  .sp  .sp
1141    # before  ---------------branch-reset----------- after    # before  ---------------branch-reset----------- after
1142    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1143    # 1            2         2  3        2     3     4    # 1            2         2  3        2     3     4
1144  .sp  .sp
1145  A backreference or a recursive call to a numbered subpattern always refers to  A backreference or a recursive call to a numbered subpattern always refers to
1146  the first one in the pattern with the given number.  the first one in the pattern with the given number.
1147  .P  .P
# Line 1079  abbreviation. This pattern (ignoring the Line 1198  abbreviation. This pattern (ignoring the
1198    (?<DN>Sat)(?:urday)?    (?<DN>Sat)(?:urday)?
1199  .sp  .sp
1200  There are five capturing substrings, but only one is ever set after a match.  There are five capturing substrings, but only one is ever set after a match.
1201  (An alternative way of solving this problem is to use a "branch reset"  (An alternative way of solving this problem is to use a "branch reset"
1202  subpattern, as described in the previous section.)  subpattern, as described in the previous section.)
1203  .P  .P
1204  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
# Line 1287  previous example can be rewritten as Line 1406  previous example can be rewritten as
1406  .sp  .sp
1407    \ed++foo    \ed++foo
1408  .sp  .sp
1409    Note that a possessive quantifier can be used with an entire group, for
1410    example:
1411    .sp
1412      (abc|xyz){2,3}+
1413    .sp
1414  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1415  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
1416  atomic group. However, there is no difference in the meaning of a possessive  atomic group. However, there is no difference in the meaning of a possessive
# Line 1361  subpattern is possible using named paren Line 1485  subpattern is possible using named paren
1485  .P  .P
1486  Another way of avoiding the ambiguity inherent in the use of digits following a  Another way of avoiding the ambiguity inherent in the use of digits following a
1487  backslash is to use the \eg escape sequence, which is a feature introduced in  backslash is to use the \eg escape sequence, which is a feature introduced in
1488  Perl 5.10. This escape must be followed by a positive or a negative number,  Perl 5.10. This escape must be followed by an unsigned number or a negative
1489  optionally enclosed in braces. These examples are all identical:  number, optionally enclosed in braces. These examples are all identical:
1490  .sp  .sp
1491    (ring), \e1    (ring), \e1
1492    (ring), \eg1    (ring), \eg1
1493    (ring), \eg{1}    (ring), \eg{1}
1494  .sp  .sp
1495  A positive number specifies an absolute reference without the ambiguity that is  An unsigned number specifies an absolute reference without the ambiguity that
1496  present in the older syntax. It is also useful when literal digits follow the  is present in the older syntax. It is also useful when literal digits follow
1497  reference. A negative number is a relative reference. Consider this example:  the reference. A negative number is a relative reference. Consider this
1498    example:
1499  .sp  .sp
1500    (abc(def)ghi)\eg{-1}    (abc(def)ghi)\eg{-1}
1501  .sp  .sp
# Line 1917  It matches "abcabc". It does not match " Line 2042  It matches "abcabc". It does not match "
2042  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
2043  .  .
2044  .  .
2045    .\" HTML <a name="onigurumasubroutines"></a>
2046    .SH "ONIGURUMA SUBROUTINE SYNTAX"
2047    .rs
2048    .sp
2049    For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
2050    a number enclosed either in angle brackets or single quotes, is an alternative
2051    syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2052    are two of the examples used above, rewritten using this syntax:
2053    .sp
2054      (?<pn> \e( ( (?>[^()]+) | \eg<pn> )* \e) )
2055      (sens|respons)e and \eg'1'ibility
2056    .sp
2057    PCRE supports an extension to Oniguruma: if a number is preceded by a
2058    plus or a minus sign it is taken as a relative reference. For example:
2059    .sp
2060      (abc)(?i:\eg<-1>)
2061    .sp
2062    Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
2063    synonymous. The former is a back reference; the latter is a subroutine call.
2064    .
2065    .
2066  .SH CALLOUTS  .SH CALLOUTS
2067  .rs  .rs
2068  .sp  .sp
# Line 1953  description of the interface to the call Line 2099  description of the interface to the call
2099  documentation.  documentation.
2100  .  .
2101  .  .
2102    .SH "BACKTRACKING CONTROL"
2103    .rs
2104    .sp
2105    Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2106    are described in the Perl documentation as "experimental and subject to change
2107    or removal in a future version of Perl". It goes on to say: "Their usage in
2108    production code should be noted to avoid problems during upgrades." The same
2109    remarks apply to the PCRE features described in this section.
2110    .P
2111    Since these verbs are specifically related to backtracking, they can be used
2112    only when the pattern is to be matched using \fBpcre_exec()\fP, which uses a
2113    backtracking algorithm. They cause an error if encountered by
2114    \fBpcre_dfa_exec()\fP.
2115    .P
2116    The new verbs make use of what was previously invalid syntax: an opening
2117    parenthesis followed by an asterisk. In Perl, they are generally of the form
2118    (*VERB:ARG) but PCRE does not support the use of arguments, so its general
2119    form is just (*VERB). Any number of these verbs may occur in a pattern. There
2120    are two kinds:
2121    .
2122    .SS "Verbs that act immediately"
2123    .rs
2124    .sp
2125    The following verbs act as soon as they are encountered:
2126    .sp
2127       (*ACCEPT)
2128    .sp
2129    This verb causes the match to end successfully, skipping the remainder of the
2130    pattern. When inside a recursion, only the innermost pattern is ended
2131    immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside
2132    capturing parentheses. In Perl, the data so far is captured: in PCRE no data is
2133    captured. For example:
2134    .sp
2135      A(A|B(*ACCEPT)|C)D
2136    .sp
2137    This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is
2138    captured.
2139    .sp
2140      (*FAIL) or (*F)
2141    .sp
2142    This verb causes the match to fail, forcing backtracking to occur. It is
2143    equivalent to (?!) but easier to read. The Perl documentation notes that it is
2144    probably useful only when combined with (?{}) or (??{}). Those are, of course,
2145    Perl features that are not present in PCRE. The nearest equivalent is the
2146    callout feature, as for example in this pattern:
2147    .sp
2148      a+(?C)(*FAIL)
2149    .sp
2150    A match with the string "aaaa" always fails, but the callout is taken before
2151    each backtrack happens (in this example, 10 times).
2152    .
2153    .SS "Verbs that act after backtracking"
2154    .rs
2155    .sp
2156    The following verbs do nothing when they are encountered. Matching continues
2157    with what follows, but if there is no subsequent match, a failure is forced.
2158    The verbs differ in exactly what kind of failure occurs.
2159    .sp
2160      (*COMMIT)
2161    .sp
2162    This verb causes the whole match to fail outright if the rest of the pattern
2163    does not match. Even if the pattern is unanchored, no further attempts to find
2164    a match by advancing the start point take place. Once (*COMMIT) has been
2165    passed, \fBpcre_exec()\fP is committed to finding a match at the current
2166    starting point, or not at all. For example:
2167    .sp
2168      a+(*COMMIT)b
2169    .sp
2170    This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2171    dynamic anchor, or "I've started, so I must finish."
2172    .sp
2173      (*PRUNE)
2174    .sp
2175    This verb causes the match to fail at the current position if the rest of the
2176    pattern does not match. If the pattern is unanchored, the normal "bumpalong"
2177    advance to the next starting character then happens. Backtracking can occur as
2178    usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but
2179    if there is no match to the right, backtracking cannot cross (*PRUNE).
2180    In simple cases, the use of (*PRUNE) is just an alternative to an atomic
2181    group or possessive quantifier, but there are some uses of (*PRUNE) that cannot
2182    be expressed in any other way.
2183    .sp
2184      (*SKIP)
2185    .sp
2186    This verb is like (*PRUNE), except that if the pattern is unanchored, the
2187    "bumpalong" advance is not to the next character, but to the position in the
2188    subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text
2189    was matched leading up to it cannot be part of a successful match. Consider:
2190    .sp
2191      a+(*SKIP)b
2192    .sp
2193    If the subject is "aaaac...", after the first match attempt fails (starting at
2194    the first character in the string), the starting point skips on to start the
2195    next attempt at "c". Note that a possessive quantifer does not have the same
2196    effect in this example; although it would suppress backtracking during the
2197    first match attempt, the second attempt would start at the second character
2198    instead of skipping on to "c".
2199    .sp
2200      (*THEN)
2201    .sp
2202    This verb causes a skip to the next alternation if the rest of the pattern does
2203    not match. That is, it cancels pending backtracking, but only within the
2204    current alternation. Its name comes from the observation that it can be used
2205    for a pattern-based if-then-else block:
2206    .sp
2207      ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2208    .sp
2209    If the COND1 pattern matches, FOO is tried (and possibly further items after
2210    the end of the group if FOO succeeds); on failure the matcher skips to the
2211    second alternative and tries COND2, without backtracking into COND1. If (*THEN)
2212    is used outside of any alternation, it acts exactly like (*PRUNE).
2213    .
2214    .
2215  .SH "SEE ALSO"  .SH "SEE ALSO"
2216  .rs  .rs
2217  .sp  .sp
# Line 1973  Cambridge CB2 3QH, England. Line 2232  Cambridge CB2 3QH, England.
2232  .rs  .rs
2233  .sp  .sp
2234  .nf  .nf
2235  Last updated: 13 June 2007  Last updated: 10 April 2008
2236  Copyright (c) 1997-2007 University of Cambridge.  Copyright (c) 1997-2008 University of Cambridge.
2237  .fi  .fi

Legend:
Removed from v.178  
changed lines
  Added in v.333

  ViewVC Help
Powered by ViewVC 1.1.5