/[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 139 by ph10, Fri Mar 30 13:41:47 2007 UTC revision 168 by ph10, Tue May 29 15:18:18 2007 UTC
# Line 30  The remainder of this document discusses Line 30  The remainder of this document discusses
30  PCRE when its main matching function, \fBpcre_exec()\fP, is used.  PCRE when its main matching function, \fBpcre_exec()\fP, is used.
31  From release 6.0, PCRE offers a second matching function,  From release 6.0, PCRE offers a second matching function,
32  \fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not  \fBpcre_dfa_exec()\fP, which matches using a different algorithm that is not
33  Perl-compatible. The advantages and disadvantages of the alternative function,  Perl-compatible. Some of the features discussed below are not available when
34  and how it differs from the normal function, are discussed in the  \fBpcre_dfa_exec()\fP is used. The advantages and disadvantages of the
35    alternative function, and how it differs from the normal function, are
36    discussed in the
37  .\" HREF  .\" HREF
38  \fBpcrematching\fP  \fBpcrematching\fP
39  .\"  .\"
# Line 519  why the traditional escape sequences suc Line 521  why the traditional escape sequences suc
521  properties in PCRE.  properties in PCRE.
522  .  .
523  .  .
524    .\" HTML <a name="resetmatchstart"></a>
525    .SS "Resetting the match start"
526    .rs
527    .sp
528    The escape sequence \eK, which is a Perl 5.10 feature, causes any previously
529    matched characters not to be included in the final matched sequence. For
530    example, the pattern:
531    .sp
532      foo\eKbar
533    .sp
534    matches "foobar", but reports that it has matched "bar". This feature is
535    similar to a lookbehind assertion
536    .\" HTML <a href="#lookbehind">
537    .\" </a>
538    (described below).
539    .\"
540    However, in this case, the part of the subject before the real match does not
541    have to be of fixed length, as lookbehind assertions do. The use of \eK does
542    not interfere with the setting of
543    .\" HTML <a href="#subpattern">
544    .\" </a>
545    captured substrings.
546    .\"
547    For example, when the pattern
548    .sp
549      (foo)\eKbar
550    .sp
551    matches "foobar", the first substring is still set to "foo".
552    .
553    .
554  .\" HTML <a name="smallassertions"></a>  .\" HTML <a name="smallassertions"></a>
555  .SS "Simple assertions"  .SS "Simple assertions"
556  .rs  .rs
# Line 1421  lengths, but it is acceptable if rewritt Line 1453  lengths, but it is acceptable if rewritt
1453  .sp  .sp
1454    (?<=abc|abde)    (?<=abc|abde)
1455  .sp  .sp
1456    In some cases, the Perl 5.10 escape sequence \eK
1457    .\" HTML <a href="#resetmatchstart">
1458    .\" </a>
1459    (see above)
1460    .\"
1461    can be used instead of a lookbehind assertion; this is not restricted to a
1462    fixed-length.
1463    .P
1464  The implementation of lookbehind assertions is, for each alternative, to  The implementation of lookbehind assertions is, for each alternative, to
1465  temporarily move the current position back by the fixed length and then try to  temporarily move the current position back by the fixed length and then try to
1466  match. If there are insufficient characters before the current position, the  match. If there are insufficient characters before the current position, the
# Line 1515  recursion, a pseudo-condition called DEF Line 1555  recursion, a pseudo-condition called DEF
1555  .sp  .sp
1556  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
1557  condition is true if the capturing subpattern of that number has previously  condition is true if the capturing subpattern of that number has previously
1558  matched.  matched. An alternative notation is to precede the digits with a plus or minus
1559    sign. In this case, the subpattern number is relative rather than absolute.
1560    The most recently opened parentheses can be referenced by (?(-1), the next most
1561    recent by (?(-2), and so on. In looping constructs it can also make sense to
1562    refer to subsequent groups with constructs such as (?(+2).
1563  .P  .P
1564  Consider the following pattern, which contains non-significant white space to  Consider the following pattern, which contains non-significant white space to
1565  make it more readable (assume the PCRE_EXTENDED option) and to divide it into  make it more readable (assume the PCRE_EXTENDED option) and to divide it into
# Line 1532  the condition is true, and so the yes-pa Line 1576  the condition is true, and so the yes-pa
1576  parenthesis is required. Otherwise, since no-pattern is not present, the  parenthesis is required. Otherwise, since no-pattern is not present, the
1577  subpattern matches nothing. In other words, this pattern matches a sequence of  subpattern matches nothing. In other words, this pattern matches a sequence of
1578  non-parentheses, optionally enclosed in parentheses.  non-parentheses, optionally enclosed in parentheses.
1579    .P
1580    If you were embedding this pattern in a larger one, you could use a relative
1581    reference:
1582    .sp
1583      ...other stuff... ( \e( )?    [^()]+    (?(-1) \e) ) ...
1584    .sp
1585    This makes the fragment independent of the parentheses in the larger pattern.
1586  .  .
1587  .SS "Checking for a used subpattern by name"  .SS "Checking for a used subpattern by name"
1588  .rs  .rs
# Line 1674  pattern, so instead you could use this: Line 1725  pattern, so instead you could use this:
1725    ( \e( ( (?>[^()]+) | (?1) )* \e) )    ( \e( ( (?>[^()]+) | (?1) )* \e) )
1726  .sp  .sp
1727  We have put the pattern into parentheses, and caused the recursion to refer to  We have put the pattern into parentheses, and caused the recursion to refer to
1728  them instead of the whole pattern. In a larger pattern, keeping track of  them instead of the whole pattern.
1729  parenthesis numbers can be tricky. It may be more convenient to use named  .P
1730  parentheses instead. The Perl syntax for this is (?&name); PCRE's earlier  In a larger pattern, keeping track of parenthesis numbers can be tricky. This
1731  syntax (?P>name) is also supported. We could rewrite the above example as  is made easier by the use of relative references. (A Perl 5.10 feature.)
1732  follows:  Instead of (?1) in the pattern above you can write (?-2) to refer to the second
1733    most recently opened parentheses preceding the recursion. In other words, a
1734    negative number counts capturing parentheses leftwards from the point at which
1735    it is encountered.
1736    .P
1737    It is also possible to refer to subsequently opened parentheses, by writing
1738    references such as (?+2). However, these cannot be recursive because the
1739    reference is not inside the parentheses that are referenced. They are always
1740    "subroutine" calls, as described in the next section.
1741    .P
1742    An alternative approach is to use named parentheses instead. The Perl syntax
1743    for this is (?&name); PCRE's earlier syntax (?P>name) is also supported. We
1744    could rewrite the above example as follows:
1745  .sp  .sp
1746    (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )    (?<pn> \e( ( (?>[^()]+) | (?&pn) )* \e) )
1747  .sp  .sp
1748  If there is more than one subpattern with the same name, the earliest one is  If there is more than one subpattern with the same name, the earliest one is
1749  used. This particular example pattern contains nested unlimited repeats, and so  used.
1750  the use of atomic grouping for matching strings of non-parentheses is important  .P
1751  when applying the pattern to strings that do not match. For example, when this  This particular example pattern that we have been looking at contains nested
1752  pattern is applied to  unlimited repeats, and so the use of atomic grouping for matching strings of
1753    non-parentheses is important when applying the pattern to strings that do not
1754    match. For example, when this pattern is applied to
1755  .sp  .sp
1756    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
1757  .sp  .sp
# Line 1738  is the actual recursive call. Line 1803  is the actual recursive call.
1803  If the syntax for a recursive subpattern reference (either by number or by  If the syntax for a recursive subpattern reference (either by number or by
1804  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
1805  subroutine in a programming language. The "called" subpattern may be defined  subroutine in a programming language. The "called" subpattern may be defined
1806  before or after the reference. An earlier example pointed out that the pattern  before or after the reference. A numbered reference can be absolute or
1807    relative, as in these examples:
1808    .sp
1809      (...(absolute)...)...(?2)...
1810      (...(relative)...)...(?-1)...
1811      (...(?+1)...(relative)...
1812    .sp
1813    An earlier example pointed out that the pattern
1814  .sp  .sp
1815    (sens|respons)e and \e1ibility    (sens|respons)e and \e1ibility
1816  .sp  .sp
# Line 1759  When a subpattern is used as a subroutin Line 1831  When a subpattern is used as a subroutin
1831  case-independence are fixed when the subpattern is defined. They cannot be  case-independence are fixed when the subpattern is defined. They cannot be
1832  changed for different calls. For example, consider this pattern:  changed for different calls. For example, consider this pattern:
1833  .sp  .sp
1834    (abc)(?i:(?1))    (abc)(?i:(?-1))
1835  .sp  .sp
1836  It matches "abcabc". It does not match "abcABC" because the change of  It matches "abcabc". It does not match "abcABC" because the change of
1837  processing option does not affect the called subpattern.  processing option does not affect the called subpattern.
# Line 1783  function is to be called. If you want to Line 1855  function is to be called. If you want to
1855  can put a number less than 256 after the letter C. The default value is zero.  can put a number less than 256 after the letter C. The default value is zero.
1856  For example, this pattern has two callout points:  For example, this pattern has two callout points:
1857  .sp  .sp
1858    (?C1)\dabc(?C2)def    (?C1)abc(?C2)def
1859  .sp  .sp
1860  If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are  If the PCRE_AUTO_CALLOUT flag is passed to \fBpcre_compile()\fP, callouts are
1861  automatically installed before each item in the pattern. They are all numbered  automatically installed before each item in the pattern. They are all numbered
# Line 1821  Cambridge CB2 3QH, England. Line 1893  Cambridge CB2 3QH, England.
1893  .rs  .rs
1894  .sp  .sp
1895  .nf  .nf
1896  Last updated: 06 March 2007  Last updated: 29 May 2007
1897  Copyright (c) 1997-2007 University of Cambridge.  Copyright (c) 1997-2007 University of Cambridge.
1898  .fi  .fi

Legend:
Removed from v.139  
changed lines
  Added in v.168

  ViewVC Help
Powered by ViewVC 1.1.5