/[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 93 by nigel, Sat Feb 24 21:41:42 2007 UTC revision 394 by ph10, Wed Mar 18 16:38:23 2009 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 30  The remainder of this document discusses Line 39  The remainder of this document discusses
39  PCRE when its main matching function, \fBpcre_exec()\fP, is used.  PCRE when its main matching function, \fBpcre_exec()\fP, is used.
40  From release 6.0, PCRE offers a second matching function,  From release 6.0, PCRE offers a second matching function,
41  \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
42  Perl-compatible. The advantages and disadvantages of the alternative function,  Perl-compatible. Some of the features discussed below are not available when
43  and how it differs from the normal function, are discussed in the  \fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the
44    alternative function, and how it differs from the normal function, are
45    discussed in the
46  .\" HREF  .\" HREF
47  \fBpcrematching\fP  \fBpcrematching\fP
48  .\"  .\"
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 147  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 162  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 238  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. Back references are  enclosed in braces, is an absolute or relative back reference. A named back
306  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 252  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
# Line 260  following are always recognized: Line 337  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
341      \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
345      \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 275  there is no character to match. Line 356  there is no character to match.
356  .P  .P
357  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).
358  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
359  are HT (9), LF (10), FF (12), CR (13), and space (32). (If "use locale;" is  are HT (9), LF (10), FF (12), CR (13), and space (32). If "use locale;" is
360  included in a Perl script, \es may match the VT character. In PCRE, it never  included in a Perl script, \es may match the VT character. In PCRE, it never
361  does.)  does.
362    .P
363    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
365    character property support is available. These sequences retain their original
366    meanings from before UTF-8 support was available, mainly for efficiency
367    reasons. Note that this also affects \eb, because it is defined in terms of \ew
368    and \eW.
369    .P
370    The sequences \eh, \eH, \ev, and \eV are Perl 5.10 features. In contrast to the
371    other sequences, these do match certain high-valued codepoints in UTF-8 mode.
372    The horizontal space characters are:
373    .sp
374      U+0009     Horizontal tab
375      U+0020     Space
376      U+00A0     Non-break space
377      U+1680     Ogham space mark
378      U+180E     Mongolian vowel separator
379      U+2000     En quad
380      U+2001     Em quad
381      U+2002     En space
382      U+2003     Em space
383      U+2004     Three-per-em space
384      U+2005     Four-per-em space
385      U+2006     Six-per-em space
386      U+2007     Figure space
387      U+2008     Punctuation space
388      U+2009     Thin space
389      U+200A     Hair space
390      U+202F     Narrow no-break space
391      U+205F     Medium mathematical space
392      U+3000     Ideographic space
393    .sp
394    The vertical space characters are:
395    .sp
396      U+000A     Linefeed
397      U+000B     Vertical tab
398      U+000C     Formfeed
399      U+000D     Carriage return
400      U+0085     Next line
401      U+2028     Line separator
402      U+2029     Paragraph separator
403  .P  .P
404  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
405  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 291  in the Line 413  in the
413  .\" HREF  .\" HREF
414  \fBpcreapi\fP  \fBpcreapi\fP
415  .\"  .\"
416  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,
417  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
418  .P  accented letters, and these are matched by \ew. The use of locales with Unicode
419  In UTF-8 mode, characters with values greater than 128 never match \ed, \es, or  is discouraged.
 \ew, and always match \eD, \eS, and \eW. This is true even when Unicode  
 character property support is available. The use of locales with Unicode is  
 discouraged.  
420  .  .
421  .  .
422    .\" HTML <a name="newlineseq"></a>
423  .SS "Newline sequences"  .SS "Newline sequences"
424  .rs  .rs
425  .sp  .sp
426  Outside a character class, the escape sequence \eR matches any Unicode newline  Outside a character class, by default, the escape sequence \eR matches any
427  sequence. This is an extension to Perl. 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
428  the following:  equivalent to the following:
429  .sp  .sp
430    (?>\er\en|\en|\ex0b|\ef|\er|\ex85)    (?>\er\en|\en|\ex0b|\ef|\er|\ex85)
431  .sp  .sp
# Line 325  are added: LS (line separator, U+2028) a Line 445  are added: LS (line separator, U+2028) a
445  Unicode character property support is not needed for these characters to be  Unicode character property support is not needed for these characters to be
446  recognized.  recognized.
447  .P  .P
448    It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
449    complete set of Unicode line endings) by setting the option PCRE_BSR_ANYCRLF
450    either at compile time or when the pattern is matched. (BSR is an abbrevation
451    for "backslash R".) This can be made the default when PCRE is built; if this is
452    the case, the other behaviour can be requested via the PCRE_BSR_UNICODE option.
453    It is also possible to specify these settings by starting a pattern string with
454    one of the following sequences:
455    .sp
456      (*BSR_ANYCRLF)   CR, LF, or CRLF only
457      (*BSR_UNICODE)   any Unicode newline sequence
458    .sp
459    These override the default and the options given to \fBpcre_compile()\fP, but
460    they can be overridden by options given to \fBpcre_exec()\fP. Note that these
461    special settings, which are not Perl-compatible, are recognized only at the
462    very start of a pattern, and that they must be in upper case. If more than one
463    of them is present, the last one is used. They can be combined with a change of
464    newline convention, for example, a pattern can start with:
465    .sp
466      (*ANY)(*BSR_ANYCRLF)
467    .sp
468  Inside a character class, \eR matches the letter "R".  Inside a character class, \eR matches the letter "R".
469  .  .
470  .  .
# Line 333  Inside a character class, \eR matches th Line 473  Inside a character class, \eR matches th
473  .rs  .rs
474  .sp  .sp
475  When PCRE is built with Unicode character property support, three additional  When PCRE is built with Unicode character property support, three additional
476  escape sequences to match character properties are available when UTF-8 mode  escape sequences that match characters with specific properties are available.
477  is selected. They are:  When not in UTF-8 mode, these sequences are of course limited to testing
478    characters whose codepoints are less than 256, but they do work in this mode.
479    The extra escape sequences are:
480  .sp  .sp
481    \ep{\fIxx\fP}   a character with the \fIxx\fP property    \ep{\fIxx\fP}   a character with the \fIxx\fP property
482    \eP{\fIxx\fP}   a character without the \fIxx\fP property    \eP{\fIxx\fP}   a character without the \fIxx\fP property
# Line 486  The special property L& is also supporte Line 628  The special property L& is also supporte
628  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
629  a modifier or "other".  a modifier or "other".
630  .P  .P
631    The Cs (Surrogate) property applies only to characters in the range U+D800 to
632    U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so
633    cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
634    (see the discussion of PCRE_NO_UTF8_CHECK in the
635    .\" HREF
636    \fBpcreapi\fP
637    .\"
638    page).
639    .P
640  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})
641  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
642  properties with "Is".  properties with "Is".
# Line 510  atomic group Line 661  atomic group
661  (see below).  (see below).
662  .\"  .\"
663  Characters with the "mark" property are typically accents that affect the  Characters with the "mark" property are typically accents that affect the
664  preceding character.  preceding character. None of them have codepoints less than 256, so in
665    non-UTF-8 mode \eX matches any one character.
666  .P  .P
667  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
668  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 518  why the traditional escape sequences suc Line 670  why the traditional escape sequences suc
670  properties in PCRE.  properties in PCRE.
671  .  .
672  .  .
673    .\" HTML <a name="resetmatchstart"></a>
674    .SS "Resetting the match start"
675    .rs
676    .sp
677    The escape sequence \eK, which is a Perl 5.10 feature, causes any previously
678    matched characters not to be included in the final matched sequence. For
679    example, the pattern:
680    .sp
681      foo\eKbar
682    .sp
683    matches "foobar", but reports that it has matched "bar". This feature is
684    similar to a lookbehind assertion
685    .\" HTML <a href="#lookbehind">
686    .\" </a>
687    (described below).
688    .\"
689    However, in this case, the part of the subject before the real match does not
690    have to be of fixed length, as lookbehind assertions do. The use of \eK does
691    not interfere with the setting of
692    .\" HTML <a href="#subpattern">
693    .\" </a>
694    captured substrings.
695    .\"
696    For example, when the pattern
697    .sp
698      (foo)\eKbar
699    .sp
700    matches "foobar", the first substring is still set to "foo".
701    .
702    .
703  .\" HTML <a name="smallassertions"></a>  .\" HTML <a name="smallassertions"></a>
704  .SS "Simple assertions"  .SS "Simple assertions"
705  .rs  .rs
# Line 740  example [\ex{100}-\ex{2ff}]. Line 922  example [\ex{100}-\ex{2ff}].
922  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
923  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
924  [][\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
925  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
926  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
927  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
928  property support.  property support.
# Line 830  alternative in the subpattern. Line 1012  alternative in the subpattern.
1012  .rs  .rs
1013  .sp  .sp
1014  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and  The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and
1015  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
1016  Perl option letters enclosed between "(?" and ")". The option letters are  the pattern by a sequence of Perl option letters enclosed between "(?" and ")".
1017    The option letters are
1018  .sp  .sp
1019    i  for PCRE_CASELESS    i  for PCRE_CASELESS
1020    m  for PCRE_MULTILINE    m  for PCRE_MULTILINE
# Line 845  PCRE_MULTILINE while unsetting PCRE_DOTA Line 1028  PCRE_MULTILINE while unsetting PCRE_DOTA
1028  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
1029  unset.  unset.
1030  .P  .P
1031    The PCRE-specific options PCRE_DUPNAMES, PCRE_UNGREEDY, and PCRE_EXTRA can be
1032    changed in the same way as the Perl-compatible options by using the characters
1033    J, U and X respectively.
1034    .P
1035  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
1036  parentheses), the change applies to the remainder of the pattern that follows.  parentheses), the change applies to the remainder of the pattern that follows.
1037  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 868  branch is abandoned before the option se Line 1055  branch is abandoned before the option se
1055  option settings happen at compile time. There would be some very weird  option settings happen at compile time. There would be some very weird
1056  behaviour otherwise.  behaviour otherwise.
1057  .P  .P
1058  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
1059  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
1060  J, U and X respectively.  pattern can contain special leading sequences to override what the application
1061    has set or what has been defaulted. Details are given in the section entitled
1062    .\" HTML <a href="#newlineseq">
1063    .\" </a>
1064    "Newline sequences"
1065    .\"
1066    above.
1067  .  .
1068  .  .
1069  .\" HTML <a name="subpattern"></a>  .\" HTML <a name="subpattern"></a>
# Line 925  is reached, an option setting in one bra Line 1118  is reached, an option setting in one bra
1118  the above patterns match "SUNDAY" as well as "Saturday".  the above patterns match "SUNDAY" as well as "Saturday".
1119  .  .
1120  .  .
1121    .SH "DUPLICATE SUBPATTERN NUMBERS"
1122    .rs
1123    .sp
1124    Perl 5.10 introduced a feature whereby each alternative in a subpattern uses
1125    the same numbers for its capturing parentheses. Such a subpattern starts with
1126    (?| and is itself a non-capturing subpattern. For example, consider this
1127    pattern:
1128    .sp
1129      (?|(Sat)ur|(Sun))day
1130    .sp
1131    Because the two alternatives are inside a (?| group, both sets of capturing
1132    parentheses are numbered one. Thus, when the pattern matches, you can look
1133    at captured substring number one, whichever alternative matched. This construct
1134    is useful when you want to capture part, but not all, of one of a number of
1135    alternatives. Inside a (?| group, parentheses are numbered as usual, but the
1136    number is reset at the start of each branch. The numbers of any capturing
1137    buffers that follow the subpattern start after the highest number used in any
1138    branch. The following example is taken from the Perl documentation.
1139    The numbers underneath show in which buffer the captured content will be
1140    stored.
1141    .sp
1142      # before  ---------------branch-reset----------- after
1143      / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1144      # 1            2         2  3        2     3     4
1145    .sp
1146    A backreference or a recursive call to a numbered subpattern always refers to
1147    the first one in the pattern with the given number.
1148    .P
1149    An alternative approach to using this "branch reset" feature is to use
1150    duplicate named subpatterns, as described in the next section.
1151    .
1152    .
1153  .SH "NAMED SUBPATTERNS"  .SH "NAMED SUBPATTERNS"
1154  .rs  .rs
1155  .sp  .sp
# Line 974  abbreviation. This pattern (ignoring the Line 1199  abbreviation. This pattern (ignoring the
1199    (?<DN>Sat)(?:urday)?    (?<DN>Sat)(?:urday)?
1200  .sp  .sp
1201  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.
1202    (An alternative way of solving this problem is to use a "branch reset"
1203    subpattern, as described in the previous section.)
1204    .P
1205  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
1206  for the first (and in this example, the only) subpattern of that name that  for the first (and in this example, the only) subpattern of that name that
1207  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was. If you
# Line 984  details of the interfaces for handling n Line 1212  details of the interfaces for handling n
1212  \fBpcreapi\fP  \fBpcreapi\fP
1213  .\"  .\"
1214  documentation.  documentation.
1215    .P
1216    \fBWarning:\fP You cannot use different names to distinguish between two
1217    subpatterns with the same number (see the previous section) because PCRE uses
1218    only the numbers when matching.
1219  .  .
1220  .  .
1221  .SH REPETITION  .SH REPETITION
# Line 1032  support is available, \eX{3} matches thr Line 1264  support is available, \eX{3} matches thr
1264  which may be several bytes long (and they may be of different lengths).  which may be several bytes long (and they may be of different lengths).
1265  .P  .P
1266  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
1267  previous item and the quantifier were not present.  previous item and the quantifier were not present. This may be useful for
1268    subpatterns that are referenced as
1269    .\" HTML <a href="#subpatternsassubroutines">
1270    .\" </a>
1271    subroutines
1272    .\"
1273    from elsewhere in the pattern. Items other than subpatterns that have a {0}
1274    quantifier are omitted from the compiled pattern.
1275  .P  .P
1276  For convenience, the three most common quantifiers have single-character  For convenience, the three most common quantifiers have single-character
1277  abbreviations:  abbreviations:
# Line 1179  previous example can be rewritten as Line 1418  previous example can be rewritten as
1418  .sp  .sp
1419    \ed++foo    \ed++foo
1420  .sp  .sp
1421    Note that a possessive quantifier can be used with an entire group, for
1422    example:
1423    .sp
1424      (abc|xyz){2,3}+
1425    .sp
1426  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1427  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
1428  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 1253  subpattern is possible using named paren Line 1497  subpattern is possible using named paren
1497  .P  .P
1498  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
1499  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
1500  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
1501  optionally enclosed in braces. These examples are all identical:  number, optionally enclosed in braces. These examples are all identical:
1502  .sp  .sp
1503    (ring), \e1    (ring), \e1
1504    (ring), \eg1    (ring), \eg1
1505    (ring), \eg{1}    (ring), \eg{1}
1506  .sp  .sp
1507  A positive number specifies an absolute reference without the ambiguity that is  An unsigned number specifies an absolute reference without the ambiguity that
1508  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
1509  reference. A negative number is a relative reference. Consider this example:  the reference. A negative number is a relative reference. Consider this
1510    example:
1511  .sp  .sp
1512    (abc(def)ghi)\eg{-1}    (abc(def)ghi)\eg{-1}
1513  .sp  .sp
# Line 1292  back reference, the case of letters is r Line 1537  back reference, the case of letters is r
1537  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
1538  capturing subpattern is matched caselessly.  capturing subpattern is matched caselessly.
1539  .P  .P
1540  Back references to named subpatterns use the Perl syntax \ek<name> or \ek'name'  There are several different ways of writing back references to named
1541  or the Python syntax (?P=name). We could rewrite the above example in either of  subpatterns. The .NET syntax \ek{name} and the Perl syntax \ek<name> or
1542    \ek'name' are supported, as is the Python syntax (?P=name). Perl 5.10's unified
1543    back reference syntax, in which \eg can be used for both numeric and named
1544    references, is also supported. We could rewrite the above example in any of
1545  the following ways:  the following ways:
1546  .sp  .sp
1547    (?<p1>(?i)rah)\es+\ek<p1>    (?<p1>(?i)rah)\es+\ek<p1>
1548      (?'p1'(?i)rah)\es+\ek{p1}
1549    (?P<p1>(?i)rah)\es+(?P=p1)    (?P<p1>(?i)rah)\es+(?P=p1)
1550      (?<p1>(?i)rah)\es+\eg{p1}
1551  .sp  .sp
1552  A subpattern that is referenced by name may appear in the pattern before or  A subpattern that is referenced by name may appear in the pattern before or
1553  after the reference.  after the reference.
# Line 1420  lengths, but it is acceptable if rewritt Line 1670  lengths, but it is acceptable if rewritt
1670  .sp  .sp
1671    (?<=abc|abde)    (?<=abc|abde)
1672  .sp  .sp
1673    In some cases, the Perl 5.10 escape sequence \eK
1674    .\" HTML <a href="#resetmatchstart">
1675    .\" </a>
1676    (see above)
1677    .\"
1678    can be used instead of a lookbehind assertion; this is not restricted to a
1679    fixed-length.
1680    .P
1681  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1682  temporarily move the current position back by the fixed length and then try to  temporarily move the current position back by the fixed length and then try to
1683  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
# Line 1514  recursion, a pseudo-condition called DEF Line 1772  recursion, a pseudo-condition called DEF
1772  .sp  .sp
1773  If the text between the parentheses consists of a sequence of digits, the  If the text between the parentheses consists of a sequence of digits, the
1774  condition is true if the capturing subpattern of that number has previously  condition is true if the capturing subpattern of that number has previously
1775  matched.  matched. An alternative notation is to precede the digits with a plus or minus
1776    sign. In this case, the subpattern number is relative rather than absolute.
1777    The most recently opened parentheses can be referenced by (?(-1), the next most
1778    recent by (?(-2), and so on. In looping constructs it can also make sense to
1779    refer to subsequent groups with constructs such as (?(+2).
1780  .P  .P
1781  Consider the following pattern, which contains non-significant white space to  Consider the following pattern, which contains non-significant white space to
1782  make it more readable (assume the PCRE_EXTENDED option) and to divide it into  make it more readable (assume the PCRE_EXTENDED option) and to divide it into
# Line 1531  the condition is true, and so the yes-pa Line 1793  the condition is true, and so the yes-pa
1793  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
1794  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1795  non-parentheses, optionally enclosed in parentheses.  non-parentheses, optionally enclosed in parentheses.
1796    .P
1797    If you were embedding this pattern in a larger one, you could use a relative
1798    reference:
1799    .sp
1800      ...other stuff... ( \e( )?    [^()]+    (?(-1) \e) ) ...
1801    .sp
1802    This makes the fragment independent of the parentheses in the larger pattern.
1803  .  .
1804  .SS "Checking for a used subpattern by name"  .SS "Checking for a used subpattern by name"
1805  .rs  .rs
# Line 1673  pattern, so instead you could use this: Line 1942  pattern, so instead you could use this:
1942    ( \e( ( (?>[^()]+) | (?1) )* \e) )    ( \e( ( (?>[^()]+) | (?1) )* \e) )
1943  .sp  .sp
1944  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
1945  them instead of the whole pattern. In a larger pattern, keeping track of  them instead of the whole pattern.
1946  parenthesis numbers can be tricky. It may be more convenient to use named  .P
1947  parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier  In a larger pattern, keeping track of parenthesis numbers can be tricky. This
1948  syntax (?P>name) is also supported. We could rewrite the above example as  is made easier by the use of relative references. (A Perl 5.10 feature.)
1949  follows:  Instead of (?1) in the pattern above you can write (?-2) to refer to the second
1950    most recently opened parentheses preceding the recursion. In other words, a
1951    negative number counts capturing parentheses leftwards from the point at which
1952    it is encountered.
1953    .P
1954    It is also possible to refer to subsequently opened parentheses, by writing
1955    references such as (?+2). However, these cannot be recursive because the
1956    reference is not inside the parentheses that are referenced. They are always
1957    "subroutine" calls, as described in the next section.
1958    .P
1959    An alternative approach is to use named parentheses instead. The Perl syntax
1960    for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We
1961    could rewrite the above example as follows:
1962  .sp  .sp
1963    (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )    (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )
1964  .sp  .sp
1965  If there is more than one subpattern with the same name, the earliest one is  If there is more than one subpattern with the same name, the earliest one is
1966  used. This particular example pattern contains nested unlimited repeats, and so  used.
1967  the use of atomic grouping for matching strings of non-parentheses is important  .P
1968  when applying the pattern to strings that do not match. For example, when this  This particular example pattern that we have been looking at contains nested
1969  pattern is applied to  unlimited repeats, and so the use of atomic grouping for matching strings of
1970    non-parentheses is important when applying the pattern to strings that do not
1971    match. For example, when this pattern is applied to
1972  .sp  .sp
1973    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1974  .sp  .sp
# Line 1737  is the actual recursive call. Line 2020  is the actual recursive call.
2020  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
2021  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
2022  subroutine in a programming language. The "called" subpattern may be defined  subroutine in a programming language. The "called" subpattern may be defined
2023  before or after the reference. An earlier example pointed out that the pattern  before or after the reference. A numbered reference can be absolute or
2024    relative, as in these examples:
2025    .sp
2026      (...(absolute)...)...(?2)...
2027      (...(relative)...)...(?-1)...
2028      (...(?+1)...(relative)...
2029    .sp
2030    An earlier example pointed out that the pattern
2031  .sp  .sp
2032    (sens|respons)e and \e1ibility    (sens|respons)e and \e1ibility
2033  .sp  .sp
# Line 1758  When a subpattern is used as a subroutin Line 2048  When a subpattern is used as a subroutin
2048  case-independence are fixed when the subpattern is defined. They cannot be  case-independence are fixed when the subpattern is defined. They cannot be
2049  changed for different calls. For example, consider this pattern:  changed for different calls. For example, consider this pattern:
2050  .sp  .sp
2051    (abc)(?i:(?1))    (abc)(?i:(?-1))
2052  .sp  .sp
2053  It matches "abcabc". It does not match "abcABC" because the change of  It matches "abcabc". It does not match "abcABC" because the change of
2054  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
2055  .  .
2056  .  .
2057    .\" HTML <a name="onigurumasubroutines"></a>
2058    .SH "ONIGURUMA SUBROUTINE SYNTAX"
2059    .rs
2060    .sp
2061    For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
2062    a number enclosed either in angle brackets or single quotes, is an alternative
2063    syntax for referencing a subpattern as a subroutine, possibly recursively. Here
2064    are two of the examples used above, rewritten using this syntax:
2065    .sp
2066      (?<pn> \e( ( (?>[^()]+) | \eg<pn> )* \e) )
2067      (sens|respons)e and \eg'1'ibility
2068    .sp
2069    PCRE supports an extension to Oniguruma: if a number is preceded by a
2070    plus or a minus sign it is taken as a relative reference. For example:
2071    .sp
2072      (abc)(?i:\eg<-1>)
2073    .sp
2074    Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
2075    synonymous. The former is a back reference; the latter is a subroutine call.
2076    .
2077    .
2078  .SH CALLOUTS  .SH CALLOUTS
2079  .rs  .rs
2080  .sp  .sp
# Line 1782  function is to be called. If you want to Line 2093  function is to be called. If you want to
2093  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.
2094  For example, this pattern has two callout points:  For example, this pattern has two callout points:
2095  .sp  .sp
2096    (?C1)\dabc(?C2)def    (?C1)abc(?C2)def
2097  .sp  .sp
2098  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
2099  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 1800  description of the interface to the call Line 2111  description of the interface to the call
2111  documentation.  documentation.
2112  .  .
2113  .  .
2114    .SH "BACKTRACKING CONTROL"
2115    .rs
2116    .sp
2117    Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2118    are described in the Perl documentation as "experimental and subject to change
2119    or removal in a future version of Perl". It goes on to say: "Their usage in
2120    production code should be noted to avoid problems during upgrades." The same
2121    remarks apply to the PCRE features described in this section.
2122    .P
2123    Since these verbs are specifically related to backtracking, most of them can be
2124    used only when the pattern is to be matched using \fBpcre_exec()\fP, which uses
2125    a backtracking algorithm. With the exception of (*FAIL), which behaves like a
2126    failing negative assertion, they cause an error if encountered by
2127    \fBpcre_dfa_exec()\fP.
2128    .P
2129    The new verbs make use of what was previously invalid syntax: an opening
2130    parenthesis followed by an asterisk. In Perl, they are generally of the form
2131    (*VERB:ARG) but PCRE does not support the use of arguments, so its general
2132    form is just (*VERB). Any number of these verbs may occur in a pattern. There
2133    are two kinds:
2134    .
2135    .SS "Verbs that act immediately"
2136    .rs
2137    .sp
2138    The following verbs act as soon as they are encountered:
2139    .sp
2140       (*ACCEPT)
2141    .sp
2142    This verb causes the match to end successfully, skipping the remainder of the
2143    pattern. When inside a recursion, only the innermost pattern is ended
2144    immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside
2145    capturing parentheses. In Perl, the data so far is captured: in PCRE no data is
2146    captured. For example:
2147    .sp
2148      A(A|B(*ACCEPT)|C)D
2149    .sp
2150    This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is
2151    captured.
2152    .sp
2153      (*FAIL) or (*F)
2154    .sp
2155    This verb causes the match to fail, forcing backtracking to occur. It is
2156    equivalent to (?!) but easier to read. The Perl documentation notes that it is
2157    probably useful only when combined with (?{}) or (??{}). Those are, of course,
2158    Perl features that are not present in PCRE. The nearest equivalent is the
2159    callout feature, as for example in this pattern:
2160    .sp
2161      a+(?C)(*FAIL)
2162    .sp
2163    A match with the string "aaaa" always fails, but the callout is taken before
2164    each backtrack happens (in this example, 10 times).
2165    .
2166    .SS "Verbs that act after backtracking"
2167    .rs
2168    .sp
2169    The following verbs do nothing when they are encountered. Matching continues
2170    with what follows, but if there is no subsequent match, a failure is forced.
2171    The verbs differ in exactly what kind of failure occurs.
2172    .sp
2173      (*COMMIT)
2174    .sp
2175    This verb causes the whole match to fail outright if the rest of the pattern
2176    does not match. Even if the pattern is unanchored, no further attempts to find
2177    a match by advancing the start point take place. Once (*COMMIT) has been
2178    passed, \fBpcre_exec()\fP is committed to finding a match at the current
2179    starting point, or not at all. For example:
2180    .sp
2181      a+(*COMMIT)b
2182    .sp
2183    This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2184    dynamic anchor, or "I've started, so I must finish."
2185    .sp
2186      (*PRUNE)
2187    .sp
2188    This verb causes the match to fail at the current position if the rest of the
2189    pattern does not match. If the pattern is unanchored, the normal "bumpalong"
2190    advance to the next starting character then happens. Backtracking can occur as
2191    usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but
2192    if there is no match to the right, backtracking cannot cross (*PRUNE).
2193    In simple cases, the use of (*PRUNE) is just an alternative to an atomic
2194    group or possessive quantifier, but there are some uses of (*PRUNE) that cannot
2195    be expressed in any other way.
2196    .sp
2197      (*SKIP)
2198    .sp
2199    This verb is like (*PRUNE), except that if the pattern is unanchored, the
2200    "bumpalong" advance is not to the next character, but to the position in the
2201    subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text
2202    was matched leading up to it cannot be part of a successful match. Consider:
2203    .sp
2204      a+(*SKIP)b
2205    .sp
2206    If the subject is "aaaac...", after the first match attempt fails (starting at
2207    the first character in the string), the starting point skips on to start the
2208    next attempt at "c". Note that a possessive quantifer does not have the same
2209    effect in this example; although it would suppress backtracking during the
2210    first match attempt, the second attempt would start at the second character
2211    instead of skipping on to "c".
2212    .sp
2213      (*THEN)
2214    .sp
2215    This verb causes a skip to the next alternation if the rest of the pattern does
2216    not match. That is, it cancels pending backtracking, but only within the
2217    current alternation. Its name comes from the observation that it can be used
2218    for a pattern-based if-then-else block:
2219    .sp
2220      ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2221    .sp
2222    If the COND1 pattern matches, FOO is tried (and possibly further items after
2223    the end of the group if FOO succeeds); on failure the matcher skips to the
2224    second alternative and tries COND2, without backtracking into COND1. If (*THEN)
2225    is used outside of any alternation, it acts exactly like (*PRUNE).
2226    .
2227    .
2228  .SH "SEE ALSO"  .SH "SEE ALSO"
2229  .rs  .rs
2230  .sp  .sp
2231  \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3).  \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3), \fBpcre\fP(3).
2232  .P  .
2233  .in 0  .
2234  Last updated: 06 December 2006  .SH AUTHOR
2235  .br  .rs
2236  Copyright (c) 1997-2006 University of Cambridge.  .sp
2237    .nf
2238    Philip Hazel
2239    University Computing Service
2240    Cambridge CB2 3QH, England.
2241    .fi
2242    .
2243    .
2244    .SH REVISION
2245    .rs
2246    .sp
2247    .nf
2248    Last updated: 18 March 2009
2249    Copyright (c) 1997-2009 University of Cambridge.
2250    .fi

Legend:
Removed from v.93  
changed lines
  Added in v.394

  ViewVC Help
Powered by ViewVC 1.1.5