/[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 456 by ph10, Fri Oct 2 08:53:31 2009 UTC revision 517 by ph10, Wed May 5 10:44:20 2010 UTC
# Line 21  published by O'Reilly, covers regular ex Line 21  published by O'Reilly, covers regular ex
21  description of PCRE's regular expressions is intended as reference material.  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,  there is now also support for UTF-8 character strings. To use this,
25  PCRE must be built to include UTF-8 support, and you must call  PCRE must be built to include UTF-8 support, and you must call
26  \fBpcre_compile()\fP or \fBpcre_compile2()\fP with the PCRE_UTF8 option. There  \fBpcre_compile()\fP or \fBpcre_compile2()\fP with the PCRE_UTF8 option. There
27  is also a special sequence that can be given at the start of a pattern:  is also a special sequence that can be given at the start of a pattern:
# Line 83  string with one of the following five se Line 83  string with one of the following five se
83    (*ANYCRLF)   any of the three above    (*ANYCRLF)   any of the three above
84    (*ANY)       all Unicode newline sequences    (*ANY)       all Unicode newline sequences
85  .sp  .sp
86  These override the default and the options given to \fBpcre_compile()\fP or  These override the default and the options given to \fBpcre_compile()\fP or
87  \fBpcre_compile2()\fP. For example, on a Unix system where LF is the default  \fBpcre_compile2()\fP. For example, on a Unix system where LF is the default
88  newline sequence, the pattern  newline sequence, the pattern
89  .sp  .sp
# Line 95  Perl-compatible, are recognized only at Line 95  Perl-compatible, are recognized only at
95  they must be in upper case. If more than one of them is present, the last one  they must be in upper case. If more than one of them is present, the last one
96  is used.  is used.
97  .P  .P
98  The newline convention does not affect what the \eR escape sequence matches. By  The newline convention affects the interpretation of the dot metacharacter when
99  default, this is any Unicode newline sequence, for Perl compatibility. However,  PCRE_DOTALL is not set, and also the behaviour of \eN. However, it does not
100  this can be changed; see the description of \eR in the section entitled  affect what the \eR escape sequence matches. By default, this is any Unicode
101    newline sequence, for Perl compatibility. However, this can be changed; see the
102    description of \eR in the section entitled
103  .\" HTML <a href="#newlineseq">  .\" HTML <a href="#newlineseq">
104  .\" </a>  .\" </a>
105  "Newline sequences"  "Newline sequences"
# Line 217  one of the following escape sequences th Line 219  one of the following escape sequences th
219    \en        linefeed (hex 0A)    \en        linefeed (hex 0A)
220    \er        carriage return (hex 0D)    \er        carriage return (hex 0D)
221    \et        tab (hex 09)    \et        tab (hex 09)
222    \eddd      character with octal code ddd, or backreference    \eddd      character with octal code ddd, or back reference
223    \exhh      character with hex code hh    \exhh      character with hex code hh
224    \ex{hhh..} character with hex code hhh..    \ex{hhh..} character with hex code hhh..
225  .sp  .sp
# Line 295  zero, because no more than three octal d Line 297  zero, because no more than three octal d
297  .P  .P
298  All the sequences that define a single character value can be used both inside  All the sequences that define a single character value can be used both inside
299  and outside character classes. In addition, inside a character class, the  and outside character classes. In addition, inside a character class, the
300  sequence \eb is interpreted as the backspace character (hex 08), and the  sequence \eb is interpreted as the backspace character (hex 08). The sequences
301  sequences \eR and \eX are interpreted as the characters "R" and "X",  \eB, \eN, \eR, and \eX are not special inside a character class. Like any other
302  respectively. Outside a character class, these sequences have different  unrecognized escape sequences, they are treated as the literal characters "B",
303  meanings  "N", "R", and "X" by default, but cause an error if the PCRE_EXTRA option is
304  .\" HTML <a href="#uniextseq">  set. Outside a character class, these sequences have different meanings.
 .\" </a>  
 (see below).  
 .\"  
305  .  .
306  .  .
307  .SS "Absolute and relative back references"  .SS "Absolute and relative back references"
# Line 333  syntax for referencing a subpattern as a Line 332  syntax for referencing a subpattern as a
332  later.  later.
333  .\"  .\"
334  Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP  Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
335  synonymous. The former is a back reference; the latter is a  synonymous. The former is a back reference; the latter is a
336  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
337  .\" </a>  .\" </a>
338  subroutine  subroutine
# Line 344  call. Line 343  call.
343  .SS "Generic character types"  .SS "Generic character types"
344  .rs  .rs
345  .sp  .sp
346  Another use of backslash is for specifying generic character types. The  Another use of backslash is for specifying generic character types:
 following are always recognized:  
347  .sp  .sp
348    \ed     any decimal digit    \ed     any decimal digit
349    \eD     any character that is not a decimal digit    \eD     any character that is not a decimal digit
# Line 358  following are always recognized: Line 356  following are always recognized:
356    \ew     any "word" character    \ew     any "word" character
357    \eW     any "non-word" character    \eW     any "non-word" character
358  .sp  .sp
359  Each pair of escape sequences partitions the complete set of characters into  There is also the single sequence \eN, which matches a non-newline character.
360  two disjoint sets. Any given character matches one, and only one, of each pair.  This is the same as
361    .\" HTML <a href="#fullstopdot">
362    .\" </a>
363    the "." metacharacter
364    .\"
365    when PCRE_DOTALL is not set.
366    .P
367    Each pair of lower and upper case escape sequences partitions the complete set
368    of characters into two disjoint sets. Any given character matches one, and only
369    one, of each pair.
370  .P  .P
371  These character type sequences can appear both inside and outside character  These character type sequences can appear both inside and outside character
372  classes. They each match one character of the appropriate type. If the current  classes. They each match one character of the appropriate type. If the current
# Line 468  one of the following sequences: Line 475  one of the following sequences:
475    (*BSR_ANYCRLF)   CR, LF, or CRLF only    (*BSR_ANYCRLF)   CR, LF, or CRLF only
476    (*BSR_UNICODE)   any Unicode newline sequence    (*BSR_UNICODE)   any Unicode newline sequence
477  .sp  .sp
478  These override the default and the options given to \fBpcre_compile()\fP or  These override the default and the options given to \fBpcre_compile()\fP or
479  \fBpcre_compile2()\fP, but they can be overridden by options given to  \fBpcre_compile2()\fP, but they can be overridden by options given to
480  \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. Note that these special settings,  \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. Note that these special settings,
481  which are not Perl-compatible, are recognized only at the very start of a  which are not Perl-compatible, are recognized only at the very start of a
# Line 478  convention, for example, a pattern can s Line 485  convention, for example, a pattern can s
485  .sp  .sp
486    (*ANY)(*BSR_ANYCRLF)    (*ANY)(*BSR_ANYCRLF)
487  .sp  .sp
488  Inside a character class, \eR matches the letter "R".  Inside a character class, \eR is treated as an unrecognized escape sequence,
489    and so matches the letter "R" by default, but causes an error if PCRE_EXTRA is
490    set.
491  .  .
492  .  .
493  .\" HTML <a name="uniextseq"></a>  .\" HTML <a name="uniextseq"></a>
# Line 496  The extra escape sequences are: Line 505  The extra escape sequences are:
505    \eX       an extended Unicode sequence    \eX       an extended Unicode sequence
506  .sp  .sp
507  The property names represented by \fIxx\fP above are limited to the Unicode  The property names represented by \fIxx\fP above are limited to the Unicode
508  script names, the general category properties, and "Any", which matches any  script names, the general category properties, "Any", which matches any
509  character (including newline). Other properties such as "InMusicalSymbols" are  character (including newline), and some special PCRE properties (described
510  not currently supported by PCRE. Note that \eP{Any} does not match any  in the
511  characters, so always causes a match failure.  .\" HTML <a href="#extraprops">
512    .\" </a>
513    next section).
514    .\"
515    Other Perl properties such as "InMusicalSymbols" are not currently supported by
516    PCRE. Note that \eP{Any} does not match any characters, so always causes a
517    match failure.
518  .P  .P
519  Sets of Unicode characters are defined as belonging to certain scripts. A  Sets of Unicode characters are defined as belonging to certain scripts. A
520  character from one of these sets can be matched using a script name. For  character from one of these sets can be matched using a script name. For
# Line 513  Those that are not part of an identified Line 528  Those that are not part of an identified
528  .P  .P
529  Arabic,  Arabic,
530  Armenian,  Armenian,
531    Avestan,
532  Balinese,  Balinese,
533    Bamum,
534  Bengali,  Bengali,
535  Bopomofo,  Bopomofo,
536  Braille,  Braille,
537  Buginese,  Buginese,
538  Buhid,  Buhid,
539  Canadian_Aboriginal,  Canadian_Aboriginal,
540    Carian,
541    Cham,
542  Cherokee,  Cherokee,
543  Common,  Common,
544  Coptic,  Coptic,
# Line 528  Cypriot, Line 547  Cypriot,
547  Cyrillic,  Cyrillic,
548  Deseret,  Deseret,
549  Devanagari,  Devanagari,
550    Egyptian_Hieroglyphs,
551  Ethiopic,  Ethiopic,
552  Georgian,  Georgian,
553  Glagolitic,  Glagolitic,
# Line 540  Hangul, Line 560  Hangul,
560  Hanunoo,  Hanunoo,
561  Hebrew,  Hebrew,
562  Hiragana,  Hiragana,
563    Imperial_Aramaic,
564  Inherited,  Inherited,
565    Inscriptional_Pahlavi,
566    Inscriptional_Parthian,
567    Javanese,
568    Kaithi,
569  Kannada,  Kannada,
570  Katakana,  Katakana,
571    Kayah_Li,
572  Kharoshthi,  Kharoshthi,
573  Khmer,  Khmer,
574  Lao,  Lao,
575  Latin,  Latin,
576    Lepcha,
577  Limbu,  Limbu,
578  Linear_B,  Linear_B,
579    Lisu,
580    Lycian,
581    Lydian,
582  Malayalam,  Malayalam,
583    Meetei_Mayek,
584  Mongolian,  Mongolian,
585  Myanmar,  Myanmar,
586  New_Tai_Lue,  New_Tai_Lue,
# Line 557  Nko, Line 588  Nko,
588  Ogham,  Ogham,
589  Old_Italic,  Old_Italic,
590  Old_Persian,  Old_Persian,
591    Old_South_Arabian,
592    Old_Turkic,
593    Ol_Chiki,
594  Oriya,  Oriya,
595  Osmanya,  Osmanya,
596  Phags_Pa,  Phags_Pa,
597  Phoenician,  Phoenician,
598    Rejang,
599  Runic,  Runic,
600    Samaritan,
601    Saurashtra,
602  Shavian,  Shavian,
603  Sinhala,  Sinhala,
604    Sundanese,
605  Syloti_Nagri,  Syloti_Nagri,
606  Syriac,  Syriac,
607  Tagalog,  Tagalog,
608  Tagbanwa,  Tagbanwa,
609  Tai_Le,  Tai_Le,
610    Tai_Tham,
611    Tai_Viet,
612  Tamil,  Tamil,
613  Telugu,  Telugu,
614  Thaana,  Thaana,
# Line 576  Thai, Line 616  Thai,
616  Tibetan,  Tibetan,
617  Tifinagh,  Tifinagh,
618  Ugaritic,  Ugaritic,
619    Vai,
620  Yi.  Yi.
621  .P  .P
622  Each character has exactly one general category property, specified by a  Each character has exactly one Unicode general category property, specified by
623  two-letter abbreviation. For compatibility with Perl, negation can be specified  a two-letter abbreviation. For compatibility with Perl, negation can be
624  by including a circumflex between the opening brace and the property name. For  specified by including a circumflex between the opening brace and the property
625  example, \ep{^Lu} is the same as \eP{Lu}.  name. For example, \ep{^Lu} is the same as \eP{Lu}.
626  .P  .P
627  If only one letter is specified with \ep or \eP, it includes all the general  If only one letter is specified with \ep or \eP, it includes all the general
628  category properties that start with that letter. In this case, in the absence  category properties that start with that letter. In this case, in the absence
# Line 683  why the traditional escape sequences suc Line 724  why the traditional escape sequences suc
724  properties in PCRE.  properties in PCRE.
725  .  .
726  .  .
727    .\" HTML <a name="extraprops"></a>
728    .SS PCRE's additional properties
729    .rs
730    .sp
731    As well as the standard Unicode properties described in the previous
732    section, PCRE supports four more that make it possible to convert traditional
733    escape sequences such as \ew and \es and POSIX character classes to use Unicode
734    properties. These are:
735    .sp
736      Xan   Any alphanumeric character
737      Xps   Any POSIX space character
738      Xsp   Any Perl space character
739      Xwd   Any Perl "word" character
740    .sp
741    Xan matches characters that have either the L (letter) or the N (number)
742    property. Xps matches the characters tab, linefeed, vertical tab, formfeed, or
743    carriage return, and any other character that has the Z (separator) property.
744    Xsp is the same as Xps, except that vertical tab is excluded. Xwd matches the
745    same characters as Xan, plus underscore.
746    .
747    .
748  .\" HTML <a name="resetmatchstart"></a>  .\" HTML <a name="resetmatchstart"></a>
749  .SS "Resetting the match start"  .SS "Resetting the match start"
750  .rs  .rs
# Line 711  For example, when the pattern Line 773  For example, when the pattern
773    (foo)\eKbar    (foo)\eKbar
774  .sp  .sp
775  matches "foobar", the first substring is still set to "foo".  matches "foobar", the first substring is still set to "foo".
776    .P
777    Perl documents that the use of \eK within assertions is "not well defined". In
778    PCRE, \eK is acted upon when it occurs inside positive assertions, but is
779    ignored in negative assertions.
780  .  .
781  .  .
782  .\" HTML <a name="smallassertions"></a>  .\" HTML <a name="smallassertions"></a>
# Line 735  The backslashed assertions are: Line 801  The backslashed assertions are:
801    \ez     matches only at the end of the subject    \ez     matches only at the end of the subject
802    \eG     matches at the first matching position in the subject    \eG     matches at the first matching position in the subject
803  .sp  .sp
804  These assertions may not appear in character classes (but note that \eb has a  Inside a character class, \eb has a different meaning; it matches the backspace
805  different meaning, namely the backspace character, inside a character class).  character. If any other of these assertions appears in a character class, by
806    default it matches the corresponding literal character (for example, \eB
807    matches the letter B). However, if the PCRE_EXTRA option is set, an "invalid
808    escape sequence" error is generated instead.
809  .P  .P
810  A word boundary is a position in the subject string where the current character  A word boundary is a position in the subject string where the current character
811  and the previous character do not both match \ew or \eW (i.e. one matches  and the previous character do not both match \ew or \eW (i.e. one matches
812  \ew and the other matches \eW), or the start or end of the string if the  \ew and the other matches \eW), or the start or end of the string if the
813  first or last character matches \ew, respectively. Neither PCRE nor Perl has a  first or last character matches \ew, respectively. Neither PCRE nor Perl has a
814  separte "start of word" or "end of word" metasequence. However, whatever  separte "start of word" or "end of word" metasequence. However, whatever
815  follows \eb normally determines which it is. For example, the fragment  follows \eb normally determines which it is. For example, the fragment
816  \eba matches "a" at the start of a word.  \eba matches "a" at the start of a word.
817  .P  .P
818  The \eA, \eZ, and \ez assertions differ from the traditional circumflex and  The \eA, \eZ, and \ez assertions differ from the traditional circumflex and
# Line 828  end of the subject in both modes, and if Line 897  end of the subject in both modes, and if
897  \eA it is always anchored, whether or not PCRE_MULTILINE is set.  \eA it is always anchored, whether or not PCRE_MULTILINE is set.
898  .  .
899  .  .
900  .SH "FULL STOP (PERIOD, DOT)"  .\" HTML <a name="fullstopdot"></a>
901    .SH "FULL STOP (PERIOD, DOT) AND \eN"
902  .rs  .rs
903  .sp  .sp
904  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
# Line 850  to match it. Line 920  to match it.
920  The handling of dot is entirely independent of the handling of circumflex and  The handling of dot is entirely independent of the handling of circumflex and
921  dollar, the only relationship being that they both involve newlines. Dot has no  dollar, the only relationship being that they both involve newlines. Dot has no
922  special meaning in a character class.  special meaning in a character class.
923    .P
924    The escape sequence \eN always behaves as a dot does when PCRE_DOTALL is not
925    set. In other words, it matches any one character except one that signifies the
926    end of a line.
927  .  .
928  .  .
929  .SH "MATCHING A SINGLE BYTE"  .SH "MATCHING A SINGLE BYTE"
# Line 876  the lookbehind. Line 950  the lookbehind.
950  .rs  .rs
951  .sp  .sp
952  An opening square bracket introduces a character class, terminated by a closing  An opening square bracket introduces a character class, terminated by a closing
953  square bracket. A closing square bracket on its own is not special by default.  square bracket. A closing square bracket on its own is not special by default.
954  However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square  However, if the PCRE_JAVASCRIPT_COMPAT option is set, a lone closing square
955  bracket causes a compile-time error. If a closing square bracket is required as  bracket causes a compile-time error. If a closing square bracket is required as
956  a member of the class, it should be the first data character in the class  a member of the class, it should be the first data character in the class
957  (after an initial circumflex, if present) or escaped with a backslash.  (after an initial circumflex, if present) or escaped with a backslash.
# Line 1163  stored. Line 1237  stored.
1237    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x    / ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
1238    # 1            2         2  3        2     3     4    # 1            2         2  3        2     3     4
1239  .sp  .sp
1240  A backreference to a numbered subpattern uses the most recent value that is set  A back reference to a numbered subpattern uses the most recent value that is
1241  for that number by any subpattern. The following pattern matches "abcabc" or  set for that number by any subpattern. The following pattern matches "abcabc"
1242  "defdef":  or "defdef":
1243  .sp  .sp
1244    /(?|(abc)|(def))\1/    /(?|(abc)|(def))\e1/
1245  .sp  .sp
1246  In contrast, a recursive or "subroutine" call to a numbered subpattern always  In contrast, a recursive or "subroutine" call to a numbered subpattern always
1247  refers to the first one in the pattern with the given number. The following  refers to the first one in the pattern with the given number. The following
1248  pattern matches "abcabc" or "defabc":  pattern matches "abcabc" or "defabc":
1249  .sp  .sp
1250    /(?|(abc)|(def))(?1)/    /(?|(abc)|(def))(?1)/
1251  .sp  .sp
1252    If a
1253    .\" HTML <a href="#conditions">
1254    .\" </a>
1255    condition test
1256    .\"
1257    for a subpattern's having matched refers to a non-unique number, the test is
1258    true if any of the subpatterns of that number have matched.
1259  .P  .P
1260  An alternative approach to using the "branch reset" feature is to use  An alternative approach to using this "branch reset" feature is to use
1261  duplicate named subpatterns, as described in the next section.  duplicate named subpatterns, as described in the next section.
1262  .  .
1263  .  .
# Line 1189  if an expression is modified, the number Line 1270  if an expression is modified, the number
1270  difficulty, PCRE supports the naming of subpatterns. This feature was not  difficulty, PCRE supports the naming of subpatterns. This feature was not
1271  added to Perl until release 5.10. Python had the feature earlier, and PCRE  added to Perl until release 5.10. Python had the feature earlier, and PCRE
1272  introduced it at release 4.0, using the Python syntax. PCRE now supports both  introduced it at release 4.0, using the Python syntax. PCRE now supports both
1273  the Perl and the Python syntax.  the Perl and the Python syntax. Perl allows identically numbered subpatterns to
1274    have different names, but PCRE does not.
1275  .P  .P
1276  In PCRE, a subpattern can be named in one of three ways: (?<name>...) or  In PCRE, a subpattern can be named in one of three ways: (?<name>...) or
1277  (?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing  (?'name'...) as in Perl, or (?P<name>...) as in Python. References to capturing
1278  parentheses from other parts of the pattern, such as  parentheses from other parts of the pattern, such as
1279  .\" HTML <a href="#backreferences">  .\" HTML <a href="#backreferences">
1280  .\" </a>  .\" </a>
1281  backreferences,  back references,
1282  .\"  .\"
1283  .\" HTML <a href="#recursion">  .\" HTML <a href="#recursion">
1284  .\" </a>  .\" </a>
# Line 1216  extracting the name-to-number translatio Line 1298  extracting the name-to-number translatio
1298  is also a convenience function for extracting a captured substring by name.  is also a convenience function for extracting a captured substring by name.
1299  .P  .P
1300  By default, a name must be unique within a pattern, but it is possible to relax  By default, a name must be unique within a pattern, but it is possible to relax
1301  this constraint by setting the PCRE_DUPNAMES option at compile time. This can  this constraint by setting the PCRE_DUPNAMES option at compile time. (Duplicate
1302  be useful for patterns where only one instance of the named parentheses can  names are also always permitted for subpatterns with the same number, set up as
1303  match. Suppose you want to match the name of a weekday, either as a 3-letter  described in the previous section.) Duplicate names can be useful for patterns
1304  abbreviation or as the full name, and in both cases you want to extract the  where only one instance of the named parentheses can match. Suppose you want to
1305  abbreviation. This pattern (ignoring the line breaks) does the job:  match the name of a weekday, either as a 3-letter abbreviation or as the full
1306    name, and in both cases you want to extract the abbreviation. This pattern
1307    (ignoring the line breaks) does the job:
1308  .sp  .sp
1309    (?<DN>Mon|Fri|Sun)(?:day)?|    (?<DN>Mon|Fri|Sun)(?:day)?|
1310    (?<DN>Tue)(?:sday)?|    (?<DN>Tue)(?:sday)?|
# Line 1234  subpattern, as described in the previous Line 1318  subpattern, as described in the previous
1318  .P  .P
1319  The convenience function for extracting the data by name returns the substring  The convenience function for extracting the data by name returns the substring
1320  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
1321  matched. This saves searching to find which numbered subpattern it was. If you  matched. This saves searching to find which numbered subpattern it was.
1322  make a reference to a non-unique named subpattern from elsewhere in the  .P
1323  pattern, the one that corresponds to the lowest number is used. For further  If you make a back reference to a non-unique named subpattern from elsewhere in
1324  details of the interfaces for handling named subpatterns, see the  the pattern, the one that corresponds to the first occurrence of the name is
1325    used. In the absence of duplicate numbers (see the previous section) this is
1326    the one with the lowest number. If you use a named reference in a condition
1327    test (see the
1328    .\"
1329    .\" HTML <a href="#conditions">
1330    .\" </a>
1331    section about conditions
1332    .\"
1333    below), either to check whether a subpattern has matched, or to check for
1334    recursion, all subpatterns with the same name are tested. If the condition is
1335    true for any one of them, the overall condition is true. This is the same
1336    behaviour as testing by number. For further details of the interfaces for
1337    handling named subpatterns, see the
1338  .\" HREF  .\" HREF
1339  \fBpcreapi\fP  \fBpcreapi\fP
1340  .\"  .\"
1341  documentation.  documentation.
1342  .P  .P
1343  \fBWarning:\fP You cannot use different names to distinguish between two  \fBWarning:\fP You cannot use different names to distinguish between two
1344  subpatterns with the same number (see the previous section) because PCRE uses  subpatterns with the same number because PCRE uses only the numbers when
1345  only the numbers when matching.  matching. For this reason, an error is given at compile time if different names
1346    are given to subpatterns with the same number. However, you can give the same
1347    name to subpatterns with the same number, even when PCRE_DUPNAMES is not set.
1348  .  .
1349  .  .
1350  .SH REPETITION  .SH REPETITION
# Line 1263  items: Line 1362  items:
1362    a character class    a character class
1363    a back reference (see next section)    a back reference (see next section)
1364    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (unless it is an assertion)
1365    a recursive or "subroutine" call to a subpattern    a recursive or "subroutine" call to a subpattern
1366  .sp  .sp
1367  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1368  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
# Line 1374  worth setting PCRE_DOTALL in order to ob Line 1473  worth setting PCRE_DOTALL in order to ob
1473  alternatively using ^ to indicate anchoring explicitly.  alternatively using ^ to indicate anchoring explicitly.
1474  .P  .P
1475  However, there is one situation where the optimization cannot be used. When .*  However, there is one situation where the optimization cannot be used. When .*
1476  is inside capturing parentheses that are the subject of a backreference  is inside capturing parentheses that are the subject of a back reference
1477  elsewhere in the pattern, a match at the start may fail where a later one  elsewhere in the pattern, a match at the start may fail where a later one
1478  succeeds. Consider, for example:  succeeds. Consider, for example:
1479  .sp  .sp
# Line 1589  references to it always fail by default. Line 1688  references to it always fail by default.
1688  .sp  .sp
1689    (a|(bc))\e2    (a|(bc))\e2
1690  .sp  .sp
1691  always fails if it starts to match "a" rather than "bc". However, if the  always fails if it starts to match "a" rather than "bc". However, if the
1692  PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an  PCRE_JAVASCRIPT_COMPAT option is set at compile time, a back reference to an
1693  unset value matches an empty string.  unset value matches an empty string.
1694  .P  .P
1695  Because there may be many capturing parentheses in a pattern, all digits  Because there may be many capturing parentheses in a pattern, all digits
# Line 1603  whitespace. Otherwise, the \eg{ syntax o Line 1702  whitespace. Otherwise, the \eg{ syntax o
1702  "Comments"  "Comments"
1703  .\"  .\"
1704  below) can be used.  below) can be used.
1705  .P  .
1706    .SS "Recursive back references"
1707    .rs
1708    .sp
1709  A back reference that occurs inside the parentheses to which it refers fails  A back reference that occurs inside the parentheses to which it refers fails
1710  when the subpattern is first used, so, for example, (a\e1) never matches.  when the subpattern is first used, so, for example, (a\e1) never matches.
1711  However, such references can be useful inside repeated subpatterns. For  However, such references can be useful inside repeated subpatterns. For
# Line 1617  to the previous iteration. In order for Line 1719  to the previous iteration. In order for
1719  that the first iteration does not need to match the back reference. This can be  that the first iteration does not need to match the back reference. This can be
1720  done using alternation, as in the example above, or by a quantifier with a  done using alternation, as in the example above, or by a quantifier with a
1721  minimum of zero.  minimum of zero.
1722    .P
1723    Back references of this type cause the group that they reference to be treated
1724    as an
1725    .\" HTML <a href="#atomicgroup">
1726    .\" </a>
1727    atomic group.
1728    .\"
1729    Once the whole group has been matched, a subsequent matching failure cannot
1730    cause backtracking into the middle of the group.
1731  .  .
1732  .  .
1733  .\" HTML <a name="bigassertions"></a>  .\" HTML <a name="bigassertions"></a>
# Line 1712  In some cases, the Perl 5.10 escape sequ Line 1823  In some cases, the Perl 5.10 escape sequ
1823  .\" </a>  .\" </a>
1824  (see above)  (see above)
1825  .\"  .\"
1826  can be used instead of a lookbehind assertion to get round the fixed-length  can be used instead of a lookbehind assertion to get round the fixed-length
1827  restriction.  restriction.
1828  .P  .P
1829  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
# Line 1730  different numbers of bytes, are also not Line 1841  different numbers of bytes, are also not
1841  "Subroutine"  "Subroutine"
1842  .\"  .\"
1843  calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long  calls (see below) such as (?2) or (?&X) are permitted in lookbehinds, as long
1844  as the subpattern matches a fixed-length string.  as the subpattern matches a fixed-length string.
1845  .\" HTML <a href="#recursion">  .\" HTML <a href="#recursion">
1846  .\" </a>  .\" </a>
1847  Recursion,  Recursion,
# Line 1803  characters that are not "999". Line 1914  characters that are not "999".
1914  .sp  .sp
1915  It is possible to cause the matching process to obey a subpattern  It is possible to cause the matching process to obey a subpattern
1916  conditionally or to choose between two alternative subpatterns, depending on  conditionally or to choose between two alternative subpatterns, depending on
1917  the result of an assertion, or whether a specific capturing subpattern has  the result of an assertion, or whether a specific capturing subpattern has
1918  already been matched. The two possible forms of conditional subpattern are:  already been matched. The two possible forms of conditional subpattern are:
1919  .sp  .sp
1920    (?(condition)yes-pattern)    (?(condition)yes-pattern)
# Line 1821  recursion, a pseudo-condition called DEF Line 1932  recursion, a pseudo-condition called DEF
1932  .sp  .sp
1933  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
1934  condition is true if a capturing subpattern of that number has previously  condition is true if a capturing subpattern of that number has previously
1935  matched. If there is more than one capturing subpattern with the same number  matched. If there is more than one capturing subpattern with the same number
1936  (see the earlier  (see the earlier
1937  .\"  .\"
1938  .\" HTML <a href="#recursion">  .\" HTML <a href="#recursion">
1939  .\" </a>  .\" </a>
# Line 1874  Rewriting the above example to use a nam Line 1985  Rewriting the above example to use a nam
1985  .sp  .sp
1986    (?<OPEN> \e( )?    [^()]+    (?(<OPEN>) \e) )    (?<OPEN> \e( )?    [^()]+    (?(<OPEN>) \e) )
1987  .sp  .sp
1988    If the name used in a condition of this kind is a duplicate, the test is
1989    applied to all subpatterns of the same name, and is true if any one of them has
1990    matched.
1991  .  .
1992  .SS "Checking for pattern recursion"  .SS "Checking for pattern recursion"
1993  .rs  .rs
# Line 1887  letter R, for example: Line 2001  letter R, for example:
2001  .sp  .sp
2002  the condition is true if the most recent recursion is into a subpattern whose  the condition is true if the most recent recursion is into a subpattern whose
2003  number or name is given. This condition does not check the entire recursion  number or name is given. This condition does not check the entire recursion
2004  stack.  stack. If the name used in a condition of this kind is a duplicate, the test is
2005    applied to all subpatterns of the same name, and is true if any one of them is
2006    the most recent recursion.
2007  .P  .P
2008  At "top level", all these recursion test conditions are false.  At "top level", all these recursion test conditions are false.
2009  .\" HTML <a href="#recursion">  .\" HTML <a href="#recursion">
2010  .\" </a>  .\" </a>
2011  Recursive patterns  The syntax for recursive patterns
2012  .\"  .\"
2013  are described below.  is described below.
2014  .  .
2015  .SS "Defining subpatterns for use by reference only"  .SS "Defining subpatterns for use by reference only"
2016  .rs  .rs
# Line 1903  If the condition is the string (DEFINE), Line 2019  If the condition is the string (DEFINE),
2019  name DEFINE, the condition is always false. In this case, there may be only one  name DEFINE, the condition is always false. In this case, there may be only one
2020  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
2021  point in the pattern; the idea of DEFINE is that it can be used to define  point in the pattern; the idea of DEFINE is that it can be used to define
2022  "subroutines" that can be referenced from elsewhere. (The use of  "subroutines" that can be referenced from elsewhere. (The use of
2023  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
2024  .\" </a>  .\" </a>
2025  "subroutines"  "subroutines"
# Line 1980  this kind of recursion was subsequently Line 2096  this kind of recursion was subsequently
2096  .P  .P
2097  A special item that consists of (? followed by a number greater than zero and a  A special item that consists of (? followed by a number greater than zero and a
2098  closing parenthesis is a recursive call of the subpattern of the given number,  closing parenthesis is a recursive call of the subpattern of the given number,
2099  provided that it occurs inside that subpattern. (If not, it is a  provided that it occurs inside that subpattern. (If not, it is a
2100  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
2101  .\" </a>  .\" </a>
2102  "subroutine"  "subroutine"
# Line 1996  PCRE_EXTENDED option is set so that whit Line 2112  PCRE_EXTENDED option is set so that whit
2112  First it matches an opening parenthesis. Then it matches any number of  First it matches an opening parenthesis. Then it matches any number of
2113  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
2114  match of the pattern itself (that is, a correctly parenthesized substring).  match of the pattern itself (that is, a correctly parenthesized substring).
2115  Finally there is a closing parenthesis. Note the use of a possessive quantifier  Finally there is a closing parenthesis. Note the use of a possessive quantifier
2116  to avoid backtracking into sequences of non-parentheses.  to avoid backtracking into sequences of non-parentheses.
2117  .P  .P
2118  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 2044  the match runs for a very long time inde Line 2160  the match runs for a very long time inde
2160  ways the + and * repeats can carve up the subject, and all have to be tested  ways the + and * repeats can carve up the subject, and all have to be tested
2161  before failure can be reported.  before failure can be reported.
2162  .P  .P
2163  At the end of a match, the values set for any capturing subpatterns are those  At the end of a match, the values of capturing parentheses are those from
2164  from the outermost level of the recursion at which the subpattern value is set.  the outermost level. If you want to obtain intermediate values, a callout
2165  If you want to obtain intermediate values, a callout function can be used (see  function can be used (see below and the
 below and the  
2166  .\" HREF  .\" HREF
2167  \fBpcrecallout\fP  \fBpcrecallout\fP
2168  .\"  .\"
# Line 2055  documentation). If the pattern above is Line 2170  documentation). If the pattern above is
2170  .sp  .sp
2171    (ab(cd)ef)    (ab(cd)ef)
2172  .sp  .sp
2173  the value for the capturing parentheses is "ef", which is the last value taken  the value for the inner capturing parentheses (numbered 2) is "ef", which is
2174  on at the top level. If additional parentheses are added, giving  the last value taken on at the top level. If a capturing subpattern is not
2175  .sp  matched at the top level, its final value is unset, even if it is (temporarily)
2176    \e( ( ( [^()]++ | (?R) )* ) \e)  set at a deeper level.
2177       ^                        ^  .P
2178       ^                        ^  If there are more than 15 capturing parentheses in a pattern, PCRE has to
2179  .sp  obtain extra memory to store data during a recursion, which it does by using
2180  the string they capture is "ab(cd)ef", the contents of the top level  \fBpcre_malloc\fP, freeing it via \fBpcre_free\fP afterwards. If no memory can
2181  parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE  be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.
 has to obtain extra memory to store data during a recursion, which it does by  
 using \fBpcre_malloc\fP, freeing it via \fBpcre_free\fP afterwards. If no  
 memory can be obtained, the match fails with the PCRE_ERROR_NOMEMORY error.  
2182  .P  .P
2183  Do not confuse the (?R) item with the condition (R), which tests for recursion.  Do not confuse the (?R) item with the condition (R), which tests for recursion.
2184  Consider this pattern, which matches text in angle brackets, allowing for  Consider this pattern, which matches text in angle brackets, allowing for
# Line 2087  is the actual recursive call. Line 2199  is the actual recursive call.
2199  In PCRE (like Python, but unlike Perl), a recursive subpattern call is always  In PCRE (like Python, but unlike Perl), a recursive subpattern call is always
2200  treated as an atomic group. That is, once it has matched some of the subject  treated as an atomic group. That is, once it has matched some of the subject
2201  string, it is never re-entered, even if it contains untried alternatives and  string, it is never re-entered, even if it contains untried alternatives and
2202  there is a subsequent matching failure. This can be illustrated by the  there is a subsequent matching failure. This can be illustrated by the
2203  following pattern, which purports to match a palindromic string that contains  following pattern, which purports to match a palindromic string that contains
2204  an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):  an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):
2205  .sp  .sp
2206    ^(.|(.)(?1)\e2)$    ^(.|(.)(?1)\e2)$
2207  .sp  .sp
2208  The idea is that it either matches a single character, or two identical  The idea is that it either matches a single character, or two identical
2209  characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE  characters surrounding a sub-palindrome. In Perl, this pattern works; in PCRE
2210  it does not if the pattern is longer than three characters. Consider the  it does not if the pattern is longer than three characters. Consider the
2211  subject string "abcba":  subject string "abcba":
2212  .P  .P
2213  At the top level, the first character is matched, but as it is not at the end  At the top level, the first character is matched, but as it is not at the end
2214  of the string, the first alternative fails; the second alternative is taken  of the string, the first alternative fails; the second alternative is taken
2215  and the recursion kicks in. The recursive call to subpattern 1 successfully  and the recursion kicks in. The recursive call to subpattern 1 successfully
2216  matches the next character ("b"). (Note that the beginning and end of line  matches the next character ("b"). (Note that the beginning and end of line
2217  tests are not part of the recursion).  tests are not part of the recursion).
2218  .P  .P
2219  Back at the top level, the next character ("c") is compared with what  Back at the top level, the next character ("c") is compared with what
2220  subpattern 2 matched, which was "a". This fails. Because the recursion is  subpattern 2 matched, which was "a". This fails. Because the recursion is
2221  treated as an atomic group, there are now no backtracking points, and so the  treated as an atomic group, there are now no backtracking points, and so the
2222  entire match fails. (Perl is able, at this point, to re-enter the recursion and  entire match fails. (Perl is able, at this point, to re-enter the recursion and
2223  try the second alternative.) However, if the pattern is written with the  try the second alternative.) However, if the pattern is written with the
# Line 2113  alternatives in the other order, things Line 2225  alternatives in the other order, things
2225  .sp  .sp
2226    ^((.)(?1)\e2|.)$    ^((.)(?1)\e2|.)$
2227  .sp  .sp
2228  This time, the recursing alternative is tried first, and continues to recurse  This time, the recursing alternative is tried first, and continues to recurse
2229  until it runs out of characters, at which point the recursion fails. But this  until it runs out of characters, at which point the recursion fails. But this
2230  time we do have another alternative to try at the higher level. That is the big  time we do have another alternative to try at the higher level. That is the big
2231  difference: in the previous case the remaining alternative is at a deeper  difference: in the previous case the remaining alternative is at a deeper
2232  recursion level, which PCRE cannot use.  recursion level, which PCRE cannot use.
2233  .P  .P
2234  To change the pattern so that matches all palindromic strings, not just those  To change the pattern so that matches all palindromic strings, not just those
2235  with an odd number of characters, it is tempting to change the pattern to this:  with an odd number of characters, it is tempting to change the pattern to this:
2236  .sp  .sp
2237    ^((.)(?1)\e2|.?)$    ^((.)(?1)\e2|.?)$
2238  .sp  .sp
2239  Again, this works in Perl, but not in PCRE, and for the same reason. When a  Again, this works in Perl, but not in PCRE, and for the same reason. When a
2240  deeper recursion has matched a single character, it cannot be entered again in  deeper recursion has matched a single character, it cannot be entered again in
2241  order to match an empty string. The solution is to separate the two cases, and  order to match an empty string. The solution is to separate the two cases, and
2242  write out the odd and even cases as alternatives at the higher level:  write out the odd and even cases as alternatives at the higher level:
2243  .sp  .sp
2244    ^(?:((.)(?1)\e2|)|((.)(?3)\e4|.))    ^(?:((.)(?1)\e2|)|((.)(?3)\e4|.))
2245  .sp  .sp
2246  If you want to match typical palindromic phrases, the pattern has to ignore all  If you want to match typical palindromic phrases, the pattern has to ignore all
2247  non-word characters, which can be done like this:  non-word characters, which can be done like this:
2248  .sp  .sp
2249    ^\eW*+(?:((.)\eW*+(?1)\eW*+\e2|)|((.)\eW*+(?3)\eW*+\4|\eW*+.\eW*+))\eW*+$    ^\eW*+(?:((.)\eW*+(?1)\eW*+\e2|)|((.)\eW*+(?3)\eW*+\e4|\eW*+.\eW*+))\eW*+$
2250  .sp  .sp
2251  If run with the PCRE_CASELESS option, this pattern matches phrases such as "A  If run with the PCRE_CASELESS option, this pattern matches phrases such as "A
2252  man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note  man, a plan, a canal: Panama!" and it works well in both PCRE and Perl. Note
2253  the use of the possessive quantifier *+ to avoid backtracking into sequences of  the use of the possessive quantifier *+ to avoid backtracking into sequences of
2254  non-word characters. Without this, PCRE takes a great deal longer (ten times or  non-word characters. Without this, PCRE takes a great deal longer (ten times or
2255  more) to match typical phrases, and Perl takes so long that you think it has  more) to match typical phrases, and Perl takes so long that you think it has
2256  gone into a loop.  gone into a loop.
# Line 2177  matches "sense and sensibility" and "res Line 2289  matches "sense and sensibility" and "res
2289  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
2290  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2291  .P  .P
2292  Like recursive subpatterns, a "subroutine" call is always treated as an atomic  Like recursive subpatterns, a subroutine call is always treated as an atomic
2293  group. That is, once it has matched some of the subject string, it is never  group. That is, once it has matched some of the subject string, it is never
2294  re-entered, even if it contains untried alternatives and there is a subsequent  re-entered, even if it contains untried alternatives and there is a subsequent
2295  matching failure.  matching failure. Any capturing parentheses that are set during the subroutine
2296    call revert to their previous values afterwards.
2297  .P  .P
2298  When a subpattern is used as a subroutine, processing options such as  When a subpattern is used as a subroutine, processing options such as
2299  case-independence are fixed when the subpattern is defined. They cannot be  case-independence are fixed when the subpattern is defined. They cannot be
# Line 2249  description of the interface to the call Line 2362  description of the interface to the call
2362  documentation.  documentation.
2363  .  .
2364  .  .
2365    .\" HTML <a name="backtrackcontrol"></a>
2366  .SH "BACKTRACKING CONTROL"  .SH "BACKTRACKING CONTROL"
2367  .rs  .rs
2368  .sp  .sp
# Line 2264  a backtracking algorithm. With the excep Line 2378  a backtracking algorithm. With the excep
2378  failing negative assertion, they cause an error if encountered by  failing negative assertion, they cause an error if encountered by
2379  \fBpcre_dfa_exec()\fP.  \fBpcre_dfa_exec()\fP.
2380  .P  .P
2381  If any of these verbs are used in an assertion subpattern, their effect is  If any of these verbs are used in an assertion or subroutine subpattern
2382  confined to that subpattern; it does not extend to the surrounding pattern.  (including recursive subpatterns), their effect is confined to that subpattern;
2383  Note that assertion subpatterns are processed as anchored at the point where  it does not extend to the surrounding pattern. Note that such subpatterns are
2384  they are tested.  processed as anchored at the point where they are tested.
2385  .P  .P
2386  The new verbs make use of what was previously invalid syntax: an opening  The new verbs make use of what was previously invalid syntax: an opening
2387  parenthesis followed by an asterisk. In Perl, they are generally of the form  parenthesis followed by an asterisk. They are generally of the form
2388  (*VERB:ARG) but PCRE does not support the use of arguments, so its general  (*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour,
2389  form is just (*VERB). Any number of these verbs may occur in a pattern. There  depending on whether or not an argument is present. An name is a sequence of
2390  are two kinds:  letters, digits, and underscores. If the name is empty, that is, if the closing
2391    parenthesis immediately follows the colon, the effect is as if the colon were
2392    not there. Any number of these verbs may occur in a pattern.
2393    .P
2394    PCRE contains some optimizations that are used to speed up matching by running
2395    some checks at the start of each match attempt. For example, it may know the
2396    minimum length of matching subject, or that a particular character must be
2397    present. When one of these optimizations suppresses the running of a match, any
2398    included backtracking verbs will not, of course, be processed. You can suppress
2399    the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2400    when calling \fBpcre_exec()\fP.
2401    .
2402  .  .
2403  .SS "Verbs that act immediately"  .SS "Verbs that act immediately"
2404  .rs  .rs
2405  .sp  .sp
2406  The following verbs act as soon as they are encountered:  The following verbs act as soon as they are encountered. They may not be
2407    followed by a name.
2408  .sp  .sp
2409     (*ACCEPT)     (*ACCEPT)
2410  .sp  .sp
# Line 2289  captured. (This feature was added to PCR Line 2415  captured. (This feature was added to PCR
2415  .sp  .sp
2416    A((?:A|B(*ACCEPT)|C)D)    A((?:A|B(*ACCEPT)|C)D)
2417  .sp  .sp
2418  This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by  This matches "AB", "AAD", or "ACD"; when it matches "AB", "B" is captured by
2419  the outer parentheses.  the outer parentheses.
2420  .sp  .sp
2421    (*FAIL) or (*F)    (*FAIL) or (*F)
# Line 2305  callout feature, as for example in this Line 2431  callout feature, as for example in this
2431  A match with the string "aaaa" always fails, but the callout is taken before  A match with the string "aaaa" always fails, but the callout is taken before
2432  each backtrack happens (in this example, 10 times).  each backtrack happens (in this example, 10 times).
2433  .  .
2434    .
2435    .SS "Recording which path was taken"
2436    .rs
2437    .sp
2438    There is one verb whose main purpose is to track how a match was arrived at,
2439    though it also has a secondary use in conjunction with advancing the match
2440    starting point (see (*SKIP) below).
2441    .sp
2442      (*MARK:NAME) or (*:NAME)
2443    .sp
2444    A name is always required with this verb. There may be as many instances of
2445    (*MARK) as you like in a pattern, and their names do not have to be unique.
2446    .P
2447    When a match succeeds, the name of the last-encountered (*MARK) is passed back
2448    to the caller via the \fIpcre_extra\fP data structure, as described in the
2449    .\" HTML <a href="pcreapi.html#extradata">
2450    .\" </a>
2451    section on \fIpcre_extra\fP
2452    .\"
2453    in the
2454    .\" HREF
2455    \fBpcreapi\fP
2456    .\"
2457    documentation. No data is returned for a partial match. Here is an example of
2458    \fBpcretest\fP output, where the /K modifier requests the retrieval and
2459    outputting of (*MARK) data:
2460    .sp
2461      /X(*MARK:A)Y|X(*MARK:B)Z/K
2462      XY
2463       0: XY
2464      MK: A
2465      XZ
2466       0: XZ
2467      MK: B
2468    .sp
2469    The (*MARK) name is tagged with "MK:" in this output, and in this example it
2470    indicates which of the two alternatives matched. This is a more efficient way
2471    of obtaining this information than putting each alternative in its own
2472    capturing parentheses.
2473    .P
2474    A name may also be returned after a failed match if the final path through the
2475    pattern involves (*MARK). However, unless (*MARK) used in conjunction with
2476    (*COMMIT), this is unlikely to happen for an unanchored pattern because, as the
2477    starting point for matching is advanced, the final check is often with an empty
2478    string, causing a failure before (*MARK) is reached. For example:
2479    .sp
2480      /X(*MARK:A)Y|X(*MARK:B)Z/K
2481      XP
2482      No match
2483    .sp
2484    There are three potential starting points for this match (starting with X,
2485    starting with P, and with an empty string). If the pattern is anchored, the
2486    result is different:
2487    .sp
2488      /^X(*MARK:A)Y|^X(*MARK:B)Z/K
2489      XP
2490      No match, mark = B
2491    .sp
2492    PCRE's start-of-match optimizations can also interfere with this. For example,
2493    if, as a result of a call to \fBpcre_study()\fP, it knows the minimum
2494    subject length for a match, a shorter subject will not be scanned at all.
2495    .P
2496    Note that similar anomalies (though different in detail) exist in Perl, no
2497    doubt for the same reasons. The use of (*MARK) data after a failed match of an
2498    unanchored pattern is not recommended, unless (*COMMIT) is involved.
2499    .
2500    .
2501  .SS "Verbs that act after backtracking"  .SS "Verbs that act after backtracking"
2502  .rs  .rs
2503  .sp  .sp
2504  The following verbs do nothing when they are encountered. Matching continues  The following verbs do nothing when they are encountered. Matching continues
2505  with what follows, but if there is no subsequent match, a failure is forced.  with what follows, but if there is no subsequent match, causing a backtrack to
2506  The verbs differ in exactly what kind of failure occurs.  the verb, a failure is forced. That is, backtracking cannot pass to the left of
2507    the verb. However, when one of these verbs appears inside an atomic group, its
2508    effect is confined to that group, because once the group has been matched,
2509    there is never any backtracking into it. In this situation, backtracking can
2510    "jump back" to the left of the entire atomic group. (Remember also, as stated
2511    above, that this localization also applies in subroutine calls and assertions.)
2512    .P
2513    These verbs differ in exactly what kind of failure occurs when backtracking
2514    reaches them.
2515  .sp  .sp
2516    (*COMMIT)    (*COMMIT)
2517  .sp  .sp
2518  This verb causes the whole match to fail outright if the rest of the pattern  This verb, which may not be followed by a name, causes the whole match to fail
2519  does not match. Even if the pattern is unanchored, no further attempts to find  outright if the rest of the pattern does not match. Even if the pattern is
2520  a match by advancing the starting point take place. Once (*COMMIT) has been  unanchored, no further attempts to find a match by advancing the starting point
2521  passed, \fBpcre_exec()\fP is committed to finding a match at the current  take place. Once (*COMMIT) has been passed, \fBpcre_exec()\fP is committed to
2522  starting point, or not at all. For example:  finding a match at the current starting point, or not at all. For example:
2523  .sp  .sp
2524    a+(*COMMIT)b    a+(*COMMIT)b
2525  .sp  .sp
2526  This matches "xxaab" but not "aacaab". It can be thought of as a kind of  This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2527  dynamic anchor, or "I've started, so I must finish."  dynamic anchor, or "I've started, so I must finish." The name of the most
2528  .sp  recently passed (*MARK) in the path is passed back when (*COMMIT) forces a
2529    (*PRUNE)  match failure.
2530  .sp  .P
2531  This verb causes the match to fail at the current position if the rest of the  Note that (*COMMIT) at the start of a pattern is not the same as an anchor,
2532  pattern does not match. If the pattern is unanchored, the normal "bumpalong"  unless PCRE's start-of-match optimizations are turned off, as shown in this
2533  advance to the next starting character then happens. Backtracking can occur as  \fBpcretest\fP example:
2534  usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but  .sp
2535  if there is no match to the right, backtracking cannot cross (*PRUNE).    /(*COMMIT)abc/
2536  In simple cases, the use of (*PRUNE) is just an alternative to an atomic    xyzabc
2537  group or possessive quantifier, but there are some uses of (*PRUNE) that cannot     0: abc
2538  be expressed in any other way.    xyzabc\eY
2539      No match
2540    .sp
2541    PCRE knows that any match must start with "a", so the optimization skips along
2542    the subject to "a" before running the first match attempt, which succeeds. When
2543    the optimization is disabled by the \eY escape in the second subject, the match
2544    starts at "x" and so the (*COMMIT) causes it to fail without trying any other
2545    starting points.
2546    .sp
2547      (*PRUNE) or (*PRUNE:NAME)
2548    .sp
2549    This verb causes the match to fail at the current starting position in the
2550    subject if the rest of the pattern does not match. If the pattern is
2551    unanchored, the normal "bumpalong" advance to the next starting character then
2552    happens. Backtracking can occur as usual to the left of (*PRUNE), before it is
2553    reached, or when matching to the right of (*PRUNE), but if there is no match to
2554    the right, backtracking cannot cross (*PRUNE). In simple cases, the use of
2555    (*PRUNE) is just an alternative to an atomic group or possessive quantifier,
2556    but there are some uses of (*PRUNE) that cannot be expressed in any other way.
2557    The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE) when the
2558    match fails completely; the name is passed back if this is the final attempt.
2559    (*PRUNE:NAME) does not pass back a name if the match succeeds. In an anchored
2560    pattern (*PRUNE) has the same effect as (*COMMIT).
2561  .sp  .sp
2562    (*SKIP)    (*SKIP)
2563  .sp  .sp
2564  This verb is like (*PRUNE), except that if the pattern is unanchored, the  This verb, when given without a name, is like (*PRUNE), except that if the
2565  "bumpalong" advance is not to the next character, but to the position in the  pattern is unanchored, the "bumpalong" advance is not to the next character,
2566  subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text  but to the position in the subject where (*SKIP) was encountered. (*SKIP)
2567  was matched leading up to it cannot be part of a successful match. Consider:  signifies that whatever text was matched leading up to it cannot be part of a
2568    successful match. Consider:
2569  .sp  .sp
2570    a+(*SKIP)b    a+(*SKIP)b
2571  .sp  .sp
# Line 2352  effect as this example; although it woul Line 2576  effect as this example; although it woul
2576  first match attempt, the second attempt would start at the second character  first match attempt, the second attempt would start at the second character
2577  instead of skipping on to "c".  instead of skipping on to "c".
2578  .sp  .sp
2579    (*THEN)    (*SKIP:NAME)
2580    .sp
2581    When (*SKIP) has an associated name, its behaviour is modified. If the
2582    following pattern fails to match, the previous path through the pattern is
2583    searched for the most recent (*MARK) that has the same name. If one is found,
2584    the "bumpalong" advance is to the subject position that corresponds to that
2585    (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a
2586    matching name is found, normal "bumpalong" of one character happens (the
2587    (*SKIP) is ignored).
2588    .sp
2589      (*THEN) or (*THEN:NAME)
2590  .sp  .sp
2591  This verb causes a skip to the next alternation if the rest of the pattern does  This verb causes a skip to the next alternation if the rest of the pattern does
2592  not match. That is, it cancels pending backtracking, but only within the  not match. That is, it cancels pending backtracking, but only within the
# Line 2363  for a pattern-based if-then-else block: Line 2597  for a pattern-based if-then-else block:
2597  .sp  .sp
2598  If the COND1 pattern matches, FOO is tried (and possibly further items after  If the COND1 pattern matches, FOO is tried (and possibly further items after
2599  the end of the group if FOO succeeds); on failure the matcher skips to the  the end of the group if FOO succeeds); on failure the matcher skips to the
2600  second alternative and tries COND2, without backtracking into COND1. If (*THEN)  second alternative and tries COND2, without backtracking into COND1. The
2601  is used outside of any alternation, it acts exactly like (*PRUNE).  behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the
2602    overall match fails. If (*THEN) is not directly inside an alternation, it acts
2603    like (*PRUNE).
2604  .  .
2605  .  .
2606  .SH "SEE ALSO"  .SH "SEE ALSO"
2607  .rs  .rs
2608  .sp  .sp
2609  \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3),  \fBpcreapi\fP(3), \fBpcrecallout\fP(3), \fBpcrematching\fP(3),
2610  \fBpcresyntax\fP(3), \fBpcre\fP(3).  \fBpcresyntax\fP(3), \fBpcre\fP(3).
2611  .  .
2612  .  .
# Line 2388  Cambridge CB2 3QH, England. Line 2624  Cambridge CB2 3QH, England.
2624  .rs  .rs
2625  .sp  .sp
2626  .nf  .nf
2627  Last updated: 30 September 2009  Last updated: 05 May 2010
2628  Copyright (c) 1997-2009 University of Cambridge.  Copyright (c) 1997-2010 University of Cambridge.
2629  .fi  .fi

Legend:
Removed from v.456  
changed lines
  Added in v.517

  ViewVC Help
Powered by ViewVC 1.1.5