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

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

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

revision 182 by ph10, Wed Jun 13 15:09:54 2007 UTC revision 211 by ph10, Thu Aug 9 09:52:43 2007 UTC
# Line 35  man page, in case the conversion went wr Line 35  man page, in case the conversion went wr
35  <li><a name="TOC20" href="#SEC20">RECURSIVE PATTERNS</a>  <li><a name="TOC20" href="#SEC20">RECURSIVE PATTERNS</a>
36  <li><a name="TOC21" href="#SEC21">SUBPATTERNS AS SUBROUTINES</a>  <li><a name="TOC21" href="#SEC21">SUBPATTERNS AS SUBROUTINES</a>
37  <li><a name="TOC22" href="#SEC22">CALLOUTS</a>  <li><a name="TOC22" href="#SEC22">CALLOUTS</a>
38  <li><a name="TOC23" href="#SEC23">SEE ALSO</a>  <li><a name="TOC23" href="#SEC23">BACTRACKING CONTROL</a>
39  <li><a name="TOC24" href="#SEC24">AUTHOR</a>  <li><a name="TOC24" href="#SEC24">SEE ALSO</a>
40  <li><a name="TOC25" href="#SEC25">REVISION</a>  <li><a name="TOC25" href="#SEC25">AUTHOR</a>
41    <li><a name="TOC26" href="#SEC26">REVISION</a>
42  </ul>  </ul>
43  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>  <br><a name="SEC1" href="#TOC1">PCRE REGULAR EXPRESSION DETAILS</a><br>
44  <P>  <P>
45  The syntax and semantics of the regular expressions supported by PCRE are  The syntax and semantics of the regular expressions that are supported by PCRE
46  described below. Regular expressions are also described in the Perl  are described in detail below. There is a quick-reference syntax summary in the
47  documentation and in a number of books, some of which have copious examples.  <a href="pcresyntax.html"><b>pcresyntax</b></a>
48  Jeffrey Friedl's "Mastering Regular Expressions", published by O'Reilly, covers  page. Perl's regular expressions are described in its own documentation, and
49  regular expressions in great detail. This description of PCRE's regular  regular expressions in general are covered in a number of books, some of which
50  expressions is intended as reference material.  have copious examples. Jeffrey Friedl's "Mastering Regular Expressions",
51    published by O'Reilly, covers regular expressions in great detail. This
52    description of PCRE's regular expressions is intended as reference material.
53  </P>  </P>
54  <P>  <P>
55  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,
# Line 193  Thus \cz becomes hex 1A, but \c{ becomes Line 196  Thus \cz becomes hex 1A, but \c{ becomes
196  After \x, from zero to two hexadecimal digits are read (letters can be in  After \x, from zero to two hexadecimal digits are read (letters can be in
197  upper or lower case). Any number of hexadecimal digits may appear between \x{  upper or lower case). Any number of hexadecimal digits may appear between \x{
198  and }, but the value of the character code must be less than 256 in non-UTF-8  and }, but the value of the character code must be less than 256 in non-UTF-8
199  mode, and less than 2**31 in UTF-8 mode (that is, the maximum hexadecimal value  mode, and less than 2**31 in UTF-8 mode. That is, the maximum value in
200  is 7FFFFFFF). If characters other than hexadecimal digits appear between \x{  hexadecimal is 7FFFFFFF. Note that this is bigger than the largest Unicode code
201  and }, or if there is no terminating }, this form of escape is not recognized.  point, which is 10FFFF.
202  Instead, the initial \x will be interpreted as a basic hexadecimal escape,  </P>
203  with no following digits, giving a character whose value is zero.  <P>
204    If characters other than hexadecimal digits appear between \x{ and }, or if
205    there is no terminating }, this form of escape is not recognized. Instead, the
206    initial \x will be interpreted as a basic hexadecimal escape, with no
207    following digits, giving a character whose value is zero.
208  </P>  </P>
209  <P>  <P>
210  Characters whose value is less than 256 can be defined by either of the two  Characters whose value is less than 256 can be defined by either of the two
# Line 255  meanings Line 262  meanings
262  Absolute and relative back references  Absolute and relative back references
263  </b><br>  </b><br>
264  <P>  <P>
265  The sequence \g followed by a positive or negative number, optionally enclosed  The sequence \g followed by an unsigned or a negative number, optionally
266  in braces, is an absolute or relative back reference. A named back reference  enclosed in braces, is an absolute or relative back reference. A named back
267  can be coded as \g{name}. Back references are discussed  reference can be coded as \g{name}. Back references are discussed
268  <a href="#backreferences">later,</a>  <a href="#backreferences">later,</a>
269  following the discussion of  following the discussion of
270  <a href="#subpattern">parenthesized subpatterns.</a>  <a href="#subpattern">parenthesized subpatterns.</a>
# Line 384  Unicode character properties Line 391  Unicode character properties
391  </b><br>  </b><br>
392  <P>  <P>
393  When PCRE is built with Unicode character property support, three additional  When PCRE is built with Unicode character property support, three additional
394  escape sequences to match character properties are available when UTF-8 mode  escape sequences that match characters with specific properties are available.
395  is selected. They are:  When not in UTF-8 mode, these sequences are of course limited to testing
396    characters whose codepoints are less than 256, but they do work in this mode.
397    The extra escape sequences are:
398  <pre>  <pre>
399    \p{<i>xx</i>}   a character with the <i>xx</i> property    \p{<i>xx</i>}   a character with the <i>xx</i> property
400    \P{<i>xx</i>}   a character without the <i>xx</i> property    \P{<i>xx</i>}   a character without the <i>xx</i> property
# Line 542  the Lu, Ll, or Lt property, in other wor Line 551  the Lu, Ll, or Lt property, in other wor
551  a modifier or "other".  a modifier or "other".
552  </P>  </P>
553  <P>  <P>
554    The Cs (Surrogate) property applies only to characters in the range U+D800 to
555    U+DFFF. Such characters are not valid in UTF-8 strings (see RFC 3629) and so
556    cannot be tested by PCRE, unless UTF-8 validity checking has been turned off
557    (see the discussion of PCRE_NO_UTF8_CHECK in the
558    <a href="pcreapi.html"><b>pcreapi</b></a>
559    page).
560    </P>
561    <P>
562  The long synonyms for these properties that Perl supports (such as \p{Letter})  The long synonyms for these properties that Perl supports (such as \p{Letter})
563  are not supported by PCRE, nor is it permitted to prefix any of these  are not supported by PCRE, nor is it permitted to prefix any of these
564  properties with "Is".  properties with "Is".
# Line 566  or more characters with the "mark" prope Line 583  or more characters with the "mark" prope
583  atomic group  atomic group
584  <a href="#atomicgroup">(see below).</a>  <a href="#atomicgroup">(see below).</a>
585  Characters with the "mark" property are typically accents that affect the  Characters with the "mark" property are typically accents that affect the
586  preceding character.  preceding character. None of them have codepoints less than 256, so in
587    non-UTF-8 mode \X matches any one character.
588  </P>  </P>
589  <P>  <P>
590  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
# Line 1300  previous example can be rewritten as Line 1318  previous example can be rewritten as
1318  <pre>  <pre>
1319    \d++foo    \d++foo
1320  </pre>  </pre>
1321    Note that a possessive quantifier can be used with an entire group, for
1322    example:
1323    <pre>
1324      (abc|xyz){2,3}+
1325    </pre>
1326  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY  Possessive quantifiers are always greedy; the setting of the PCRE_UNGREEDY
1327  option is ignored. They are a convenient notation for the simpler forms of  option is ignored. They are a convenient notation for the simpler forms of
1328  atomic group. However, there is no difference in the meaning of a possessive  atomic group. However, there is no difference in the meaning of a possessive
# Line 1374  subpattern is possible using named paren Line 1397  subpattern is possible using named paren
1397  <P>  <P>
1398  Another way of avoiding the ambiguity inherent in the use of digits following a  Another way of avoiding the ambiguity inherent in the use of digits following a
1399  backslash is to use the \g escape sequence, which is a feature introduced in  backslash is to use the \g escape sequence, which is a feature introduced in
1400  Perl 5.10. This escape must be followed by a positive or a negative number,  Perl 5.10. This escape must be followed by an unsigned number or a negative
1401  optionally enclosed in braces. These examples are all identical:  number, optionally enclosed in braces. These examples are all identical:
1402  <pre>  <pre>
1403    (ring), \1    (ring), \1
1404    (ring), \g1    (ring), \g1
1405    (ring), \g{1}    (ring), \g{1}
1406  </pre>  </pre>
1407  A positive number specifies an absolute reference without the ambiguity that is  An unsigned number specifies an absolute reference without the ambiguity that
1408  present in the older syntax. It is also useful when literal digits follow the  is present in the older syntax. It is also useful when literal digits follow
1409  reference. A negative number is a relative reference. Consider this example:  the reference. A negative number is a relative reference. Consider this
1410    example:
1411  <pre>  <pre>
1412    (abc(def)ghi)\g{-1}    (abc(def)ghi)\g{-1}
1413  </pre>  </pre>
# Line 1972  description of the interface to the call Line 1996  description of the interface to the call
1996  <a href="pcrecallout.html"><b>pcrecallout</b></a>  <a href="pcrecallout.html"><b>pcrecallout</b></a>
1997  documentation.  documentation.
1998  </P>  </P>
1999  <br><a name="SEC23" href="#TOC1">SEE ALSO</a><br>  <br><a name="SEC23" href="#TOC1">BACTRACKING CONTROL</a><br>
2000    <P>
2001    Perl 5.10 introduced a number of "Special Backtracking Control Verbs", which
2002    are described in the Perl documentation as "experimental and subject to change
2003    or removal in a future version of Perl". It goes on to say: "Their usage in
2004    production code should be noted to avoid problems during upgrades." The same
2005    remarks apply to the PCRE features described in this section.
2006    </P>
2007    <P>
2008    Since these verbs are specifically related to backtracking, they can be used
2009    only when the pattern is to be matched using <b>pcre_exec()</b>, which uses a
2010    backtracking algorithm. They cause an error if encountered by
2011    <b>pcre_dfa_exec()</b>.
2012    </P>
2013    <P>
2014    The new verbs make use of what was previously invalid syntax: an opening
2015    parenthesis followed by an asterisk. In Perl, they are generally of the form
2016    (*VERB:ARG) but PCRE does not support the use of arguments, so its general
2017    form is just (*VERB). Any number of these verbs may occur in a pattern. There
2018    are two kinds:
2019    </P>
2020    <br><b>
2021    Verbs that act immediately
2022    </b><br>
2023    <P>
2024    The following verbs act as soon as they are encountered:
2025    <pre>
2026       (*ACCEPT)
2027    </pre>
2028    This verb causes the match to end successfully, skipping the remainder of the
2029    pattern. When inside a recursion, only the innermost pattern is ended
2030    immediately. PCRE differs from Perl in what happens if the (*ACCEPT) is inside
2031    capturing parentheses. In Perl, the data so far is captured: in PCRE no data is
2032    captured. For example:
2033    <pre>
2034      A(A|B(*ACCEPT)|C)D
2035    </pre>
2036    This matches "AB", "AAD", or "ACD", but when it matches "AB", no data is
2037    captured.
2038    <pre>
2039      (*FAIL) or (*F)
2040    </pre>
2041    This verb causes the match to fail, forcing backtracking to occur. It is
2042    equivalent to (?!) but easier to read. The Perl documentation notes that it is
2043    probably useful only when combined with (?{}) or (??{}). Those are, of course,
2044    Perl features that are not present in PCRE. The nearest equivalent is the
2045    callout feature, as for example in this pattern:
2046    <pre>
2047      a+(?C)(*FAIL)
2048    </pre>
2049    A match with the string "aaaa" always fails, but the callout is taken before
2050    each backtrack happens (in this example, 10 times).
2051    </P>
2052    <br><b>
2053    Verbs that act after backtracking
2054    </b><br>
2055    <P>
2056    The following verbs do nothing when they are encountered. Matching continues
2057    with what follows, but if there is no subsequent match, a failure is forced.
2058    The verbs differ in exactly what kind of failure occurs.
2059    <pre>
2060      (*COMMIT)
2061    </pre>
2062    This verb causes the whole match to fail outright if the rest of the pattern
2063    does not match. Even if the pattern is unanchored, no further attempts to find
2064    a match by advancing the start point take place. Once (*COMMIT) has been
2065    passed, <b>pcre_exec()</b> is committed to finding a match at the current
2066    starting point, or not at all. For example:
2067    <pre>
2068      a+(*COMMIT)b
2069    </pre>
2070    This matches "xxaab" but not "aacaab". It can be thought of as a kind of
2071    dynamic anchor, or "I've started, so I must finish."
2072    <pre>
2073      (*PRUNE)
2074    </pre>
2075    This verb causes the match to fail at the current position if the rest of the
2076    pattern does not match. If the pattern is unanchored, the normal "bumpalong"
2077    advance to the next starting character then happens. Backtracking can occur as
2078    usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but
2079    if there is no match to the right, backtracking cannot cross (*PRUNE).
2080    In simple cases, the use of (*PRUNE) is just an alternative to an atomic
2081    group or possessive quantifier, but there are some uses of (*PRUNE) that cannot
2082    be expressed in any other way.
2083    <pre>
2084      (*SKIP)
2085    </pre>
2086    This verb is like (*PRUNE), except that if the pattern is unanchored, the
2087    "bumpalong" advance is not to the next character, but to the position in the
2088    subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text
2089    was matched leading up to it cannot be part of a successful match. Consider:
2090    <pre>
2091      a+(*SKIP)b
2092    </pre>
2093    If the subject is "aaaac...", after the first match attempt fails (starting at
2094    the first character in the string), the starting point skips on to start the
2095    next attempt at "c". Note that a possessive quantifer does not have the same
2096    effect in this example; although it would suppress backtracking during the
2097    first match attempt, the second attempt would start at the second character
2098    instead of skipping on to "c".
2099    <pre>
2100      (*THEN)
2101    </pre>
2102    This verb causes a skip to the next alternation if the rest of the pattern does
2103    not match. That is, it cancels pending backtracking, but only within the
2104    current alternation. Its name comes from the observation that it can be used
2105    for a pattern-based if-then-else block:
2106    <pre>
2107      ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ...
2108    </pre>
2109    If the COND1 pattern matches, FOO is tried (and possibly further items after
2110    the end of the group if FOO succeeds); on failure the matcher skips to the
2111    second alternative and tries COND2, without backtracking into COND1. If (*THEN)
2112    is used outside of any alternation, it acts exactly like (*PRUNE).
2113    </P>
2114    <br><a name="SEC24" href="#TOC1">SEE ALSO</a><br>
2115  <P>  <P>
2116  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).  <b>pcreapi</b>(3), <b>pcrecallout</b>(3), <b>pcrematching</b>(3), <b>pcre</b>(3).
2117  </P>  </P>
2118  <br><a name="SEC24" href="#TOC1">AUTHOR</a><br>  <br><a name="SEC25" href="#TOC1">AUTHOR</a><br>
2119  <P>  <P>
2120  Philip Hazel  Philip Hazel
2121  <br>  <br>
# Line 1985  University Computing Service Line 2124  University Computing Service
2124  Cambridge CB2 3QH, England.  Cambridge CB2 3QH, England.
2125  <br>  <br>
2126  </P>  </P>
2127  <br><a name="SEC25" href="#TOC1">REVISION</a><br>  <br><a name="SEC26" href="#TOC1">REVISION</a><br>
2128  <P>  <P>
2129  Last updated: 13 June 2007  Last updated: 09 August 2007
2130  <br>  <br>
2131  Copyright &copy; 1997-2007 University of Cambridge.  Copyright &copy; 1997-2007 University of Cambridge.
2132  <br>  <br>

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

  ViewVC Help
Powered by ViewVC 1.1.5