/[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 575 by ph10, Sun Nov 21 12:55:42 2010 UTC revision 724 by ph10, Sun Oct 9 16:23:45 2011 UTC
# Line 32  Starting a pattern with this sequence is Line 32  Starting a pattern with this sequence is
32  option. This feature is not Perl-compatible. How setting UTF-8 mode affects  option. This feature is not Perl-compatible. How setting UTF-8 mode affects
33  pattern matching is mentioned in several places below. There is also a summary  pattern matching is mentioned in several places below. There is also a summary
34  of UTF-8 features in the  of UTF-8 features in the
 .\" HTML <a href="pcre.html#utf8support">  
 .\" </a>  
 section on UTF-8 support  
 .\"  
 in the main  
35  .\" HREF  .\" HREF
36  \fBpcre\fP  \fBpcreunicode\fP
37  .\"  .\"
38  page.  page.
39  .P  .P
# Line 52  such as \ed and \ew to use Unicode prope Line 47  such as \ed and \ew to use Unicode prope
47  instead of recognizing only characters with codes less than 128 via a lookup  instead of recognizing only characters with codes less than 128 via a lookup
48  table.  table.
49  .P  .P
50    If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
51    PCRE_NO_START_OPTIMIZE option either at compile or matching time. There are
52    also some more of these special sequences that are concerned with the handling
53    of newlines; they are described below.
54    .P
55  The remainder of this document discusses the patterns that are supported by  The remainder of this document discusses the patterns that are supported by
56  PCRE when its main matching function, \fBpcre_exec()\fP, is used.  PCRE when its main matching function, \fBpcre_exec()\fP, is used.
57  From release 6.0, PCRE offers a second matching function,  From release 6.0, PCRE offers a second matching function,
# Line 184  The following sections describe the use Line 184  The following sections describe the use
184  The backslash character has several uses. Firstly, if it is followed by a  The backslash character has several uses. Firstly, if it is followed by a
185  character that is not a number or a letter, it takes away any special meaning  character that is not a number or a letter, it takes away any special meaning
186  that character may have. This use of backslash as an escape character applies  that character may have. This use of backslash as an escape character applies
187  both inside and outside character classes.  both inside and outside character classes.
188  .P  .P
189  For example, if you want to match a * character, you write \e* in the pattern.  For example, if you want to match a * character, you write \e* in the pattern.
190  This escaping action applies whether or not the following character would  This escaping action applies whether or not the following character would
# Line 193  non-alphanumeric with backslash to speci Line 193  non-alphanumeric with backslash to speci
193  particular, if you want to match a backslash, you write \e\e.  particular, if you want to match a backslash, you write \e\e.
194  .P  .P
195  In UTF-8 mode, only ASCII numbers and letters have any special meaning after a  In UTF-8 mode, only ASCII numbers and letters have any special meaning after a
196  backslash. All other characters (in particular, those whose codepoints are  backslash. All other characters (in particular, those whose codepoints are
197  greater than 127) are treated as literals.  greater than 127) are treated as literals.
198  .P  .P
199  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the  If a pattern is compiled with the PCRE_EXTENDED option, whitespace in the
# Line 215  Perl, $ and @ cause variable interpolati Line 215  Perl, $ and @ cause variable interpolati
215    \eQabc\eE\e$\eQxyz\eE   abc$xyz        abc$xyz    \eQabc\eE\e$\eQxyz\eE   abc$xyz        abc$xyz
216  .sp  .sp
217  The \eQ...\eE sequence is recognized both inside and outside character classes.  The \eQ...\eE sequence is recognized both inside and outside character classes.
218  An isolated \eE that is not preceded by \eQ is ignored.  An isolated \eE that is not preceded by \eQ is ignored. If \eQ is not followed
219    by \eE later in the pattern, the literal interpretation continues to the end of
220    the pattern (that is, \eE is assumed at the end). If the isolated \eQ is inside
221    a character class, this causes an error, because the character class is not
222    terminated.
223  .  .
224  .  .
225  .\" HTML <a name="digitsafterbackslash"></a>  .\" HTML <a name="digitsafterbackslash"></a>
# Line 242  one of the following escape sequences th Line 246  one of the following escape sequences th
246  The precise effect of \ecx is as follows: if x is a lower case letter, it  The precise effect of \ecx is as follows: if x is a lower case letter, it
247  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.  is converted to upper case. Then bit 6 of the character (hex 40) is inverted.
248  Thus \ecz becomes hex 1A (z is 7A), but \ec{ becomes hex 3B ({ is 7B), while  Thus \ecz becomes hex 1A (z is 7A), but \ec{ becomes hex 3B ({ is 7B), while
249  \ec; becomes hex 7B (; is 3B). If the byte following \ec has a value greater  \ec; becomes hex 7B (; is 3B). If the byte following \ec has a value greater
250  than 127, a compile-time error occurs. This locks out non-ASCII characters in  than 127, a compile-time error occurs. This locks out non-ASCII characters in
251  both byte mode and UTF-8 mode. (When PCRE is compiled in EBCDIC mode, all byte  both byte mode and UTF-8 mode. (When PCRE is compiled in EBCDIC mode, all byte
252  values are valid. A lower case letter is converted to upper case, and then the  values are valid. A lower case letter is converted to upper case, and then the
253  0xc0 bits are flipped.)  0xc0 bits are flipped.)
254  .P  .P
255  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
# Line 432  any Unicode letter, and underscore. Note Line 436  any Unicode letter, and underscore. Note
436  \eB because they are defined in terms of \ew and \eW. Matching these sequences  \eB because they are defined in terms of \ew and \eW. Matching these sequences
437  is noticeably slower when PCRE_UCP is set.  is noticeably slower when PCRE_UCP is set.
438  .P  .P
439  The sequences \eh, \eH, \ev, and \eV are features that were added to Perl at  The sequences \eh, \eH, \ev, and \eV are features that were added to Perl at
440  release 5.10. In contrast to the other sequences, which match only ASCII  release 5.10. In contrast to the other sequences, which match only ASCII
441  characters by default, these always match certain high-valued codepoints in  characters by default, these always match certain high-valued codepoints in
442  UTF-8 mode, whether or not PCRE_UCP is set. The horizontal space characters  UTF-8 mode, whether or not PCRE_UCP is set. The horizontal space characters
# Line 748  Characters with the "mark" property are Line 752  Characters with the "mark" property are
752  preceding character. None of them have codepoints less than 256, so in  preceding character. None of them have codepoints less than 256, so in
753  non-UTF-8 mode \eX matches any one character.  non-UTF-8 mode \eX matches any one character.
754  .P  .P
755    Note that recent versions of Perl have changed \eX to match what Unicode calls
756    an "extended grapheme cluster", which has a more complicated definition.
757    .P
758  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
759  a structure that contains data for over fifteen thousand characters. That is  a structure that contains data for over fifteen thousand characters. That is
760  why the traditional escape sequences such as \ed and \ew do not use Unicode  why the traditional escape sequences such as \ed and \ew do not use Unicode
# Line 955  The handling of dot is entirely independ Line 962  The handling of dot is entirely independ
962  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
963  special meaning in a character class.  special meaning in a character class.
964  .P  .P
965  The escape sequence \eN behaves like a dot, except that it is not affected by  The escape sequence \eN behaves like a dot, except that it is not affected by
966  the PCRE_DOTALL option. In other words, it matches any character except one  the PCRE_DOTALL option. In other words, it matches any character except one
967  that signifies the end of a line.  that signifies the end of a line.
968  .  .
# Line 1071  default, but cause an error if the PCRE_ Line 1078  default, but cause an error if the PCRE_
1078  A circumflex can conveniently be used with the upper case character types to  A circumflex can conveniently be used with the upper case character types to
1079  specify a more restricted set of characters than the matching lower case type.  specify a more restricted set of characters than the matching lower case type.
1080  For example, the class [^\eW_] matches any letter or digit, but not underscore,  For example, the class [^\eW_] matches any letter or digit, but not underscore,
1081  whereas [\ew] includes underscore. A positive character class should be read as  whereas [\ew] includes underscore. A positive character class should be read as
1082  "something OR something OR ..." and a negative class as "NOT something AND NOT  "something OR something OR ..." and a negative class as "NOT something AND NOT
1083  something AND NOT ...".  something AND NOT ...".
1084  .P  .P
1085  The only metacharacters that are recognized in character classes are backslash,  The only metacharacters that are recognized in character classes are backslash,
# Line 1308  or "defdef": Line 1315  or "defdef":
1315  .sp  .sp
1316    /(?|(abc)|(def))\e1/    /(?|(abc)|(def))\e1/
1317  .sp  .sp
1318  In contrast, a recursive or "subroutine" call to a numbered subpattern always  In contrast, a subroutine call to a numbered subpattern always refers to the
1319  refers to the first one in the pattern with the given number. The following  first one in the pattern with the given number. The following pattern matches
1320  pattern matches "abcabc" or "defabc":  "abcabc" or "defabc":
1321  .sp  .sp
1322    /(?|(abc)|(def))(?1)/    /(?|(abc)|(def))(?1)/
1323  .sp  .sp
# Line 1426  items: Line 1433  items:
1433    an escape such as \ed or \epL that matches a single character    an escape such as \ed or \epL that matches a single character
1434    a character class    a character class
1435    a back reference (see next section)    a back reference (see next section)
1436    a parenthesized subpattern (unless it is an assertion)    a parenthesized subpattern (including assertions)
1437    a recursive or "subroutine" call to a subpattern    a subroutine call to a subpattern (recursive or otherwise)
1438  .sp  .sp
1439  The general repetition quantifier specifies a minimum and maximum number of  The general repetition quantifier specifies a minimum and maximum number of
1440  permitted matches, by giving the two numbers in curly brackets (braces),  permitted matches, by giving the two numbers in curly brackets (braces),
# Line 1817  those that look ahead of the current pos Line 1824  those that look ahead of the current pos
1824  that look behind it. An assertion subpattern is matched in the normal way,  that look behind it. An assertion subpattern is matched in the normal way,
1825  except that it does not cause the current matching position to be changed.  except that it does not cause the current matching position to be changed.
1826  .P  .P
1827  Assertion subpatterns are not capturing subpatterns, and may not be repeated,  Assertion subpatterns are not capturing subpatterns. If such an assertion
1828  because it makes no sense to assert the same thing several times. If any kind  contains capturing subpatterns within it, these are counted for the purposes of
1829  of assertion contains capturing subpatterns within it, these are counted for  numbering the capturing subpatterns in the whole pattern. However, substring
1830  the purposes of numbering the capturing subpatterns in the whole pattern.  capturing is carried out only for positive assertions, because it does not make
1831  However, substring capturing is carried out only for positive assertions,  sense for negative assertions.
1832  because it does not make sense for negative assertions.  .P
1833    For compatibility with Perl, assertion subpatterns may be repeated; though
1834    it makes no sense to assert the same thing several times, the side effect of
1835    capturing parentheses may occasionally be useful. In practice, there only three
1836    cases:
1837    .sp
1838    (1) If the quantifier is {0}, the assertion is never obeyed during matching.
1839    However, it may contain internal capturing parenthesized groups that are called
1840    from elsewhere via the
1841    .\" HTML <a href="#subpatternsassubroutines">
1842    .\" </a>
1843    subroutine mechanism.
1844    .\"
1845    .sp
1846    (2) If quantifier is {0,n} where n is greater than zero, it is treated as if it
1847    were {0,1}. At run time, the rest of the pattern match is tried with and
1848    without the assertion, the order depending on the greediness of the quantifier.
1849    .sp
1850    (3) If the minimum repetition is greater than zero, the quantifier is ignored.
1851    The assertion is obeyed just once when encountered during matching.
1852  .  .
1853  .  .
1854  .SS "Lookahead assertions"  .SS "Lookahead assertions"
# Line 1992  already been matched. The two possible f Line 2018  already been matched. The two possible f
2018  If the condition is satisfied, the yes-pattern is used; otherwise the  If the condition is satisfied, the yes-pattern is used; otherwise the
2019  no-pattern (if present) is used. If there are more than two alternatives in the  no-pattern (if present) is used. If there are more than two alternatives in the
2020  subpattern, a compile-time error occurs. Each of the two alternatives may  subpattern, a compile-time error occurs. Each of the two alternatives may
2021  itself contain nested subpatterns of any form, including conditional  itself contain nested subpatterns of any form, including conditional
2022  subpatterns; the restriction to two alternatives applies only at the level of  subpatterns; the restriction to two alternatives applies only at the level of
2023  the condition. This pattern fragment is an example where the alternatives are  the condition. This pattern fragment is an example where the alternatives are
2024  complex:  complex:
2025  .sp  .sp
2026    (?(1) (A|B|C) | (D | (?(2)E|F) | E) )    (?(1) (A|B|C) | (D | (?(2)E|F) | E) )
# Line 2019  the condition is true if any of them hav Line 2045  the condition is true if any of them hav
2045  to precede the digits with a plus or minus sign. In this case, the subpattern  to precede the digits with a plus or minus sign. In this case, the subpattern
2046  number is relative rather than absolute. The most recently opened parentheses  number is relative rather than absolute. The most recently opened parentheses
2047  can be referenced by (?(-1), the next most recent by (?(-2), and so on. Inside  can be referenced by (?(-1), the next most recent by (?(-2), and so on. Inside
2048  loops it can also make sense to refer to subsequent groups. The next  loops it can also make sense to refer to subsequent groups. The next
2049  parentheses to be opened can be referenced as (?(+1), and so on. (The value  parentheses to be opened can be referenced as (?(+1), and so on. (The value
2050  zero in any of these forms is not used; it provokes a compile-time error.)  zero in any of these forms is not used; it provokes a compile-time error.)
2051  .P  .P
# Line 2097  If the condition is the string (DEFINE), Line 2123  If the condition is the string (DEFINE),
2123  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
2124  alternative in the subpattern. It is always skipped if control reaches this  alternative in the subpattern. It is always skipped if control reaches this
2125  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
2126  "subroutines" that can be referenced from elsewhere. (The use of  subroutines that can be referenced from elsewhere. (The use of
2127  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
2128  .\" </a>  .\" </a>
2129  "subroutines"  subroutines
2130  .\"  .\"
2131  is described below.) For example, a pattern to match an IPv4 address such as  is described below.) For example, a pattern to match an IPv4 address such as
2132  "192.168.23.245" could be written like this (ignore whitespace and line  "192.168.23.245" could be written like this (ignore whitespace and line
# Line 2139  dd-aaa-dd or dd-dd-dd, where aaa are let Line 2165  dd-aaa-dd or dd-dd-dd, where aaa are let
2165  .SH COMMENTS  .SH COMMENTS
2166  .rs  .rs
2167  .sp  .sp
2168  There are two ways of including comments in patterns that are processed by  There are two ways of including comments in patterns that are processed by
2169  PCRE. In both cases, the start of the comment must not be in a character class,  PCRE. In both cases, the start of the comment must not be in a character class,
2170  nor in the middle of any other sequence of related characters such as (?: or a  nor in the middle of any other sequence of related characters such as (?: or a
2171  subpattern name or number. The characters that make up a comment play no part  subpattern name or number. The characters that make up a comment play no part
# Line 2163  default newline convention is in force: Line 2189  default newline convention is in force:
2189  .sp  .sp
2190    abc #comment \en still comment    abc #comment \en still comment
2191  .sp  .sp
2192  On encountering the # character, \fBpcre_compile()\fP skips along, looking for  On encountering the # character, \fBpcre_compile()\fP skips along, looking for
2193  a newline in the pattern. The sequence \en is still literal at this stage, so  a newline in the pattern. The sequence \en is still literal at this stage, so
2194  it does not terminate the comment. Only an actual character with the code value  it does not terminate the comment. Only an actual character with the code value
2195  0x0a (the default newline) does so.  0x0a (the default newline) does so.
# Line 2195  individual subpattern recursion. After i Line 2221  individual subpattern recursion. After i
2221  this kind of recursion was subsequently introduced into Perl at release 5.10.  this kind of recursion was subsequently introduced into Perl at release 5.10.
2222  .P  .P
2223  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
2224  closing parenthesis is a recursive call of the subpattern of the given number,  closing parenthesis is a recursive subroutine call of the subpattern of the
2225  provided that it occurs inside that subpattern. (If not, it is a  given number, provided that it occurs inside that subpattern. (If not, it is a
2226  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
2227  .\" </a>  .\" </a>
2228  "subroutine"  non-recursive subroutine
2229  .\"  .\"
2230  call, which is described in the next section.) The special item (?R) or (?0) is  call, which is described in the next section.) The special item (?R) or (?0) is
2231  a recursive call of the entire regular expression.  a recursive call of the entire regular expression.
# Line 2234  references such as (?+2). However, these Line 2260  references such as (?+2). However, these
2260  reference is not inside the parentheses that are referenced. They are always  reference is not inside the parentheses that are referenced. They are always
2261  .\" HTML <a href="#subpatternsassubroutines">  .\" HTML <a href="#subpatternsassubroutines">
2262  .\" </a>  .\" </a>
2263  "subroutine"  non-recursive subroutine
2264  .\"  .\"
2265  calls, as described in the next section.  calls, as described in the next section.
2266  .P  .P
# Line 2271  documentation). If the pattern above is Line 2297  documentation). If the pattern above is
2297  .sp  .sp
2298  the value for the inner capturing parentheses (numbered 2) is "ef", which is  the value for the inner capturing parentheses (numbered 2) is "ef", which is
2299  the last value taken on at the top level. If a capturing subpattern is not  the last value taken on at the top level. If a capturing subpattern is not
2300  matched at the top level, its final value is unset, even if it is (temporarily)  matched at the top level, its final captured value is unset, even if it was
2301  set at a deeper level.  (temporarily) set at a deeper level during the matching process.
2302  .P  .P
2303  If there are more than 15 capturing parentheses in a pattern, PCRE has to  If there are more than 15 capturing parentheses in a pattern, PCRE has to
2304  obtain extra memory to store data during a recursion, which it does by using  obtain extra memory to store data during a recursion, which it does by using
# Line 2292  is the actual recursive call. Line 2318  is the actual recursive call.
2318  .  .
2319  .  .
2320  .\" HTML <a name="recursiondifference"></a>  .\" HTML <a name="recursiondifference"></a>
2321  .SS "Recursion difference from Perl"  .SS "Differences in recursion processing between PCRE and Perl"
2322  .rs  .rs
2323  .sp  .sp
2324  In PCRE (like Python, but unlike Perl), a recursive subpattern call is always  Recursion processing in PCRE differs from Perl in two important ways. In PCRE
2325  treated as an atomic group. That is, once it has matched some of the subject  (like Python, but unlike Perl), a recursive subpattern call is always treated
2326  string, it is never re-entered, even if it contains untried alternatives and  as an atomic group. That is, once it has matched some of the subject string, it
2327  there is a subsequent matching failure. This can be illustrated by the  is never re-entered, even if it contains untried alternatives and there is a
2328  following pattern, which purports to match a palindromic string that contains  subsequent matching failure. This can be illustrated by the following pattern,
2329  an odd number of characters (for example, "a", "aba", "abcba", "abcdcba"):  which purports to match a palindromic string that contains an odd number of
2330    characters (for example, "a", "aba", "abcba", "abcdcba"):
2331  .sp  .sp
2332    ^(.|(.)(?1)\e2)$    ^(.|(.)(?1)\e2)$
2333  .sp  .sp
# Line 2361  For example, although "abcba" is correct Line 2388  For example, although "abcba" is correct
2388  PCRE finds the palindrome "aba" at the start, then fails at top level because  PCRE finds the palindrome "aba" at the start, then fails at top level because
2389  the end of the string does not follow. Once again, it cannot jump back into the  the end of the string does not follow. Once again, it cannot jump back into the
2390  recursion to try other alternatives, so the entire match fails.  recursion to try other alternatives, so the entire match fails.
2391    .P
2392    The second way in which PCRE and Perl differ in their recursion processing is
2393    in the handling of captured values. In Perl, when a subpattern is called
2394    recursively or as a subpattern (see the next section), it has no access to any
2395    values that were captured outside the recursion, whereas in PCRE these values
2396    can be referenced. Consider this pattern:
2397    .sp
2398      ^(.)(\e1|a(?2))
2399    .sp
2400    In PCRE, this pattern matches "bab". The first capturing parentheses match "b",
2401    then in the second group, when the back reference \e1 fails to match "b", the
2402    second alternative matches "a" and then recurses. In the recursion, \e1 does
2403    now match "b" and so the whole match succeeds. In Perl, the pattern fails to
2404    match because inside the recursive call \e1 cannot access the externally set
2405    value.
2406  .  .
2407  .  .
2408  .\" HTML <a name="subpatternsassubroutines"></a>  .\" HTML <a name="subpatternsassubroutines"></a>
2409  .SH "SUBPATTERNS AS SUBROUTINES"  .SH "SUBPATTERNS AS SUBROUTINES"
2410  .rs  .rs
2411  .sp  .sp
2412  If the syntax for a recursive subpattern reference (either by number or by  If the syntax for a recursive subpattern call (either by number or by
2413  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
2414  subroutine in a programming language. The "called" subpattern may be defined  subroutine in a programming language. The called subpattern may be defined
2415  before or after the reference. A numbered reference can be absolute or  before or after the reference. A numbered reference can be absolute or
2416  relative, as in these examples:  relative, as in these examples:
2417  .sp  .sp
# Line 2389  matches "sense and sensibility" and "res Line 2431  matches "sense and sensibility" and "res
2431  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
2432  strings. Another example is given in the discussion of DEFINE above.  strings. Another example is given in the discussion of DEFINE above.
2433  .P  .P
2434  Like recursive subpatterns, a subroutine call is always treated as an atomic  All subroutine calls, whether recursive or not, are always treated as atomic
2435  group. That is, once it has matched some of the subject string, it is never  groups. That is, once a subroutine has matched some of the subject string, it
2436  re-entered, even if it contains untried alternatives and there is a subsequent  is never re-entered, even if it contains untried alternatives and there is a
2437  matching failure. Any capturing parentheses that are set during the subroutine  subsequent matching failure. Any capturing parentheses that are set during the
2438  call revert to their previous values afterwards.  subroutine call revert to their previous values afterwards.
2439  .P  .P
2440  When a subpattern is used as a subroutine, processing options such as  Processing options such as case-independence are fixed when a subpattern is
2441  case-independence are fixed when the subpattern is defined. They cannot be  defined, so if it is used as a subroutine, such options cannot be changed for
2442  changed for different calls. For example, consider this pattern:  different calls. For example, consider this pattern:
2443  .sp  .sp
2444    (abc)(?i:(?-1))    (abc)(?i:(?-1))
2445  .sp  .sp
# Line 2478  a backtracking algorithm. With the excep Line 2520  a backtracking algorithm. With the excep
2520  failing negative assertion, they cause an error if encountered by  failing negative assertion, they cause an error if encountered by
2521  \fBpcre_dfa_exec()\fP.  \fBpcre_dfa_exec()\fP.
2522  .P  .P
2523  If any of these verbs are used in an assertion or subroutine subpattern  If any of these verbs are used in an assertion or in a subpattern that is
2524  (including recursive subpatterns), their effect is confined to that subpattern;  called as a subroutine (whether or not recursively), their effect is confined
2525  it does not extend to the surrounding pattern. Note that such subpatterns are  to that subpattern; it does not extend to the surrounding pattern, with one
2526  processed as anchored at the point where they are tested.  exception: a *MARK that is encountered in a positive assertion \fIis\fP passed
2527    back (compare capturing parentheses in assertions). Note that such subpatterns
2528    are processed as anchored at the point where they are tested. Note also that
2529    Perl's treatment of subroutines is different in some cases.
2530  .P  .P
2531  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
2532  parenthesis followed by an asterisk. They are generally of the form  parenthesis followed by an asterisk. They are generally of the form
2533  (*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour,  (*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour,
2534  depending on whether or not an argument is present. An name is a sequence of  depending on whether or not an argument is present. A name is any sequence of
2535  letters, digits, and underscores. If the name is empty, that is, if the closing  characters that does not include a closing parenthesis. If the name is empty,
2536  parenthesis immediately follows the colon, the effect is as if the colon were  that is, if the closing parenthesis immediately follows the colon, the effect
2537  not there. Any number of these verbs may occur in a pattern.  is as if the colon were not there. Any number of these verbs may occur in a
2538    pattern.
2539  .P  .P
2540  PCRE contains some optimizations that are used to speed up matching by running  PCRE contains some optimizations that are used to speed up matching by running
2541  some checks at the start of each match attempt. For example, it may know the  some checks at the start of each match attempt. For example, it may know the
# Line 2497  minimum length of matching subject, or t Line 2543  minimum length of matching subject, or t
2543  present. When one of these optimizations suppresses the running of a match, any  present. When one of these optimizations suppresses the running of a match, any
2544  included backtracking verbs will not, of course, be processed. You can suppress  included backtracking verbs will not, of course, be processed. You can suppress
2545  the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option  the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option
2546  when calling \fBpcre_exec()\fP.  when calling \fBpcre_compile()\fP or \fBpcre_exec()\fP, or by starting the
2547    pattern with (*NO_START_OPT).
2548  .  .
2549  .  .
2550  .SS "Verbs that act immediately"  .SS "Verbs that act immediately"
# Line 2509  followed by a name. Line 2556  followed by a name.
2556     (*ACCEPT)     (*ACCEPT)
2557  .sp  .sp
2558  This verb causes the match to end successfully, skipping the remainder of the  This verb causes the match to end successfully, skipping the remainder of the
2559  pattern. When inside a recursion, only the innermost pattern is ended  pattern. However, when it is inside a subpattern that is called as a
2560  immediately. If (*ACCEPT) is inside capturing parentheses, the data so far is  subroutine, only that subpattern is ended successfully. Matching then continues
2561  captured. (This feature was added to PCRE at release 8.00.) For example:  at the outer level. If (*ACCEPT) is inside capturing parentheses, the data so
2562    far is captured. For example:
2563  .sp  .sp
2564    A((?:A|B(*ACCEPT)|C)D)    A((?:A|B(*ACCEPT)|C)D)
2565  .sp  .sp
# Line 2520  the outer parentheses. Line 2568  the outer parentheses.
2568  .sp  .sp
2569    (*FAIL) or (*F)    (*FAIL) or (*F)
2570  .sp  .sp
2571  This verb causes the match to fail, forcing backtracking to occur. It is  This verb causes a matching failure, forcing backtracking to occur. It is
2572  equivalent to (?!) but easier to read. The Perl documentation notes that it is  equivalent to (?!) but easier to read. The Perl documentation notes that it is
2573  probably useful only when combined with (?{}) or (??{}). Those are, of course,  probably useful only when combined with (?{}) or (??{}). Those are, of course,
2574  Perl features that are not present in PCRE. The nearest equivalent is the  Perl features that are not present in PCRE. The nearest equivalent is the
# Line 2571  indicates which of the two alternatives Line 2619  indicates which of the two alternatives
2619  of obtaining this information than putting each alternative in its own  of obtaining this information than putting each alternative in its own
2620  capturing parentheses.  capturing parentheses.
2621  .P  .P
2622    If (*MARK) is encountered in a positive assertion, its name is recorded and
2623    passed back if it is the last-encountered. This does not happen for negative
2624    assertions.
2625    .P
2626  A name may also be returned after a failed match if the final path through the  A name may also be returned after a failed match if the final path through the
2627  pattern involves (*MARK). However, unless (*MARK) used in conjunction with  pattern involves (*MARK). However, unless (*MARK) used in conjunction with
2628  (*COMMIT), this is unlikely to happen for an unanchored pattern because, as the  (*COMMIT), this is unlikely to happen for an unanchored pattern because, as the
# Line 2683  following pattern fails to match, the pr Line 2735  following pattern fails to match, the pr
2735  searched for the most recent (*MARK) that has the same name. If one is found,  searched for the most recent (*MARK) that has the same name. If one is found,
2736  the "bumpalong" advance is to the subject position that corresponds to that  the "bumpalong" advance is to the subject position that corresponds to that
2737  (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a  (*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a
2738  matching name is found, normal "bumpalong" of one character happens (the  matching name is found, normal "bumpalong" of one character happens (that is,
2739  (*SKIP) is ignored).  the (*SKIP) is ignored).
2740  .sp  .sp
2741    (*THEN) or (*THEN:NAME)    (*THEN) or (*THEN:NAME)
2742  .sp  .sp
2743  This verb causes a skip to the next alternation in the innermost enclosing  This verb causes a skip to the next innermost alternative if the rest of the
2744  group if the rest of the pattern does not match. That is, it cancels pending  pattern does not match. That is, it cancels pending backtracking, but only
2745  backtracking, but only within the current alternation. Its name comes from the  within the current alternative. Its name comes from the observation that it can
2746  observation that it can be used for a pattern-based if-then-else block:  be used for a pattern-based if-then-else block:
2747  .sp  .sp
2748    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...    ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2749  .sp  .sp
2750  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
2751  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
2752  second alternative and tries COND2, without backtracking into COND1. The  second alternative and tries COND2, without backtracking into COND1. The
2753  behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the  behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the
2754  overall match fails. If (*THEN) is not directly inside an alternation, it acts  overall match fails. If (*THEN) is not inside an alternation, it acts like
2755  like (*PRUNE).  (*PRUNE).
 .  
 .P  
 The above verbs provide four different "strengths" of control when subsequent  
 matching fails. (*THEN) is the weakest, carrying on the match at the next  
 alternation. (*PRUNE) comes next, failing the match at the current starting  
 position, but allowing an advance to the next character (for an unanchored  
 pattern). (*SKIP) is similar, except that the advance may be more than one  
 character. (*COMMIT) is the strongest, causing the entire match to fail.  
2756  .P  .P
2757  If more than one is present in a pattern, the "stongest" one wins. For example,  Note that a subpattern that does not contain a | character is just a part of
2758  consider this pattern, where A, B, etc. are complex pattern fragments:  the enclosing alternative; it is not a nested alternation with only one
2759    alternative. The effect of (*THEN) extends beyond such a subpattern to the
2760    enclosing alternative. Consider this pattern, where A, B, etc. are complex
2761    pattern fragments that do not contain any | characters at this level:
2762    .sp
2763      A (B(*THEN)C) | D
2764    .sp
2765    If A and B are matched, but there is a failure in C, matching does not
2766    backtrack into A; instead it moves to the next alternative, that is, D.
2767    However, if the subpattern containing (*THEN) is given an alternative, it
2768    behaves differently:
2769    .sp
2770      A (B(*THEN)C | (*FAIL)) | D
2771    .sp
2772    The effect of (*THEN) is now confined to the inner subpattern. After a failure
2773    in C, matching moves to (*FAIL), which causes the whole subpattern to fail
2774    because there are no more alternatives to try. In this case, matching does now
2775    backtrack into A.
2776    .P
2777    Note also that a conditional subpattern is not considered as having two
2778    alternatives, because only one is ever used. In other words, the | character in
2779    a conditional subpattern has a different meaning. Ignoring white space,
2780    consider:
2781    .sp
2782      ^.*? (?(?=a) a | b(*THEN)c )
2783    .sp
2784    If the subject is "ba", this pattern does not match. Because .*? is ungreedy,
2785    it initially matches zero characters. The condition (?=a) then fails, the
2786    character "b" is matched, but "c" is not. At this point, matching does not
2787    backtrack to .*? as might perhaps be expected from the presence of the |
2788    character. The conditional subpattern is part of the single alternative that
2789    comprises the whole pattern, and so the match fails. (If there was a backtrack
2790    into .*?, allowing it to match "b", the match would succeed.)
2791    .P
2792    The verbs just described provide four different "strengths" of control when
2793    subsequent matching fails. (*THEN) is the weakest, carrying on the match at the
2794    next alternative. (*PRUNE) comes next, failing the match at the current
2795    starting position, but allowing an advance to the next character (for an
2796    unanchored pattern). (*SKIP) is similar, except that the advance may be more
2797    than one character. (*COMMIT) is the strongest, causing the entire match to
2798    fail.
2799    .P
2800    If more than one such verb is present in a pattern, the "strongest" one wins.
2801    For example, consider this pattern, where A, B, etc. are complex pattern
2802    fragments:
2803  .sp  .sp
2804    (A(*COMMIT)B(*THEN)C|D)    (A(*COMMIT)B(*THEN)C|D)
2805  .sp  .sp
2806  Once A has matched, PCRE is committed to this match, at the current starting  Once A has matched, PCRE is committed to this match, at the current starting
2807  position. If subsequently B matches, but C does not, the normal (*THEN) action  position. If subsequently B matches, but C does not, the normal (*THEN) action
2808  of trying the next alternation (that is, D) does not happen because (*COMMIT)  of trying the next alternative (that is, D) does not happen because (*COMMIT)
2809  overrides.  overrides.
2810  .  .
2811  .  .
# Line 2742  Cambridge CB2 3QH, England. Line 2830  Cambridge CB2 3QH, England.
2830  .rs  .rs
2831  .sp  .sp
2832  .nf  .nf
2833  Last updated: 21 November 2010  Last updated: 09 October 2011
2834  Copyright (c) 1997-2010 University of Cambridge.  Copyright (c) 1997-2011 University of Cambridge.
2835  .fi  .fi

Legend:
Removed from v.575  
changed lines
  Added in v.724

  ViewVC Help
Powered by ViewVC 1.1.5