# Diff of /code/trunk/doc/html/pcrematching.html

revision 99 by ph10, Tue Mar 6 12:27:42 2007 UTC revision 567 by ph10, Sat Nov 6 17:10:00 2010 UTC
# Line 7  Line 7
7  <p>  <p>
9  </p>  </p>
10  <p>  <p>
11  This page is part of the PCRE HTML documentation. It was generated automatically  This page is part of the PCRE HTML documentation. It was generated automatically
12  from the original man page. If there is any nonsense in it, please consult the  from the original man page. If there is any nonsense in it, please consult the
13  man page, in case the conversion went wrong.  man page, in case the conversion went wrong.
14  <br>  <br>
15  <ul>  <ul>
16  <li><a name="TOC1" href="#SEC1">PCRE MATCHING ALGORITHMS</a>  <li><a name="TOC1" href="#SEC1">PCRE MATCHING ALGORITHMS</a>
17  <li><a name="TOC2" href="#SEC2">REGULAR EXPRESSIONS AS TREES</a>  <li><a name="TOC2" href="#SEC2">REGULAR EXPRESSIONS AS TREES</a>
# Line 61  correspond to the two matching algorithm Line 61  correspond to the two matching algorithm
61  </P>  </P>
62  <br><a name="SEC3" href="#TOC1">THE STANDARD MATCHING ALGORITHM</a><br>  <br><a name="SEC3" href="#TOC1">THE STANDARD MATCHING ALGORITHM</a><br>
63  <P>  <P>
64  In the terminology of Jeffrey Friedl's book \fIMastering Regular  In the terminology of Jeffrey Friedl's book "Mastering Regular
65  Expressions\fP, the standard algorithm is an "NFA algorithm". It conducts a  Expressions", the standard algorithm is an "NFA algorithm". It conducts a
66  depth-first search of the pattern tree. That is, it proceeds along a single  depth-first search of the pattern tree. That is, it proceeds along a single
67  path through the tree, checking that the subject matches what is required. When  path through the tree, checking that the subject matches what is required. When
68  there is a mismatch, the algorithm tries any alternatives at the current point,  there is a mismatch, the algorithm tries any alternatives at the current point,
# Line 96  traditional finite state machine (it kee Line 96  traditional finite state machine (it kee
96  simultaneously).  simultaneously).
97  </P>  </P>
98  <P>  <P>
99    Although the general principle of this matching algorithm is that it scans the
100    subject string only once, without backtracking, there is one exception: when a
101    lookaround assertion is encountered, the characters following or preceding the
102    current point have to be independently inspected.
103    </P>
104    <P>
105  The scan continues until either the end of the subject is reached, or there are  The scan continues until either the end of the subject is reached, or there are
106  no more unterminated paths. At this point, terminated paths represent the  no more unterminated paths. At this point, terminated paths represent the
107  different matching possibilities (if there are none, the match has failed).  different matching possibilities (if there are none, the match has failed).
108  Thus, if there is more than one possible match, this algorithm finds all of  Thus, if there is more than one possible match, this algorithm finds all of
109  them, and in particular, it finds the longest. In PCRE, there is an option to  them, and in particular, it finds the longest. There is an option to stop the
110  stop the algorithm after the first match (which is necessarily the shortest)  algorithm after the first match (which is necessarily the shortest) is found.
has been found.
111  </P>  </P>
112  <P>  <P>
113  Note that all the matches that are found start at the same point in the  Note that all the matches that are found start at the same point in the
# Line 148  not supported, and cause errors if encou Line 153  not supported, and cause errors if encou
153  condition or test for a specific group recursion are not supported.  condition or test for a specific group recursion are not supported.
154  </P>  </P>
155  <P>  <P>
156  5. Callouts are supported, but the value of the <i>capture_top</i> field is  5. Because many paths through the tree may be active, the \K escape sequence,
157    which resets the start of the match when encountered (but may be on some paths
158    and not on others), is not supported. It causes an error if encountered.
159    </P>
160    <P>
161    6. Callouts are supported, but the value of the <i>capture_top</i> field is
162  always 1, and the value of the <i>capture_last</i> field is always -1.  always 1, and the value of the <i>capture_last</i> field is always -1.
163  </P>  </P>
164  <P>  <P>
165  6.  7. The \C escape sequence, which (in the standard algorithm) matches a single
The \C escape sequence, which (in the standard algorithm) matches a single
166  byte, even in UTF-8 mode, is not supported because the alternative algorithm  byte, even in UTF-8 mode, is not supported because the alternative algorithm
167  moves through the subject string one character at a time, for all active paths  moves through the subject string one character at a time, for all active paths
168  through the tree.  through the tree.
169  </P>  </P>
170    <P>
171    8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE) are not
172    supported. (*FAIL) is supported, and behaves like a failing negative assertion.
173    </P>
174  <br><a name="SEC5" href="#TOC1">ADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>  <br><a name="SEC5" href="#TOC1">ADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>
175  <P>  <P>
176  Using the alternative matching algorithm provides the following advantages:  Using the alternative matching algorithm provides the following advantages:
# Line 169  match using the standard algorithm, you Line 182  match using the standard algorithm, you
182  callouts.  callouts.
183  </P>  </P>
184  <P>  <P>
185  2. There is much better support for partial matching. The restrictions on the  2. Because the alternative algorithm scans the subject string just once, and
content of the pattern that apply when using the standard algorithm for partial
matching do not apply to the alternative algorithm. For non-anchored patterns,
the starting position of a partial match is available.
</P>
<P>
3. Because the alternative algorithm scans the subject string just once, and
186  never needs to backtrack, it is possible to pass very long subject strings to  never needs to backtrack, it is possible to pass very long subject strings to
187  the matching function in several pieces, checking for partial matching each  the matching function in several pieces, checking for partial matching each
188  time.  time. It is possible to do multi-segment matching using <b>pcre_exec()</b> (by
189    retaining partially matched substrings), but it is more complicated. The
190    <a href="pcrepartial.html"><b>pcrepartial</b></a>
191    documentation gives details of partial matching and discusses multi-segment
192    matching.
193  </P>  </P>
194  <br><a name="SEC6" href="#TOC1">DISADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>  <br><a name="SEC6" href="#TOC1">DISADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>
195  <P>  <P>
# Line 207  Cambridge CB2 3QH, England. Line 218  Cambridge CB2 3QH, England.
218  </P>  </P>
219  <br><a name="SEC8" href="#TOC1">REVISION</a><br>  <br><a name="SEC8" href="#TOC1">REVISION</a><br>
220  <P>  <P>
221  Last updated: 06 March 2007  Last updated: 22 October 2010
222  <br>  <br>
223  Copyright &copy; 1997-2007 University of Cambridge.  Copyright &copy; 1997-2010 University of Cambridge.
224  <br>  <br>
225  <p>  <p>