# Diff of /code/tags/pcre-7.2/doc/pcrematching.3

revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 168 by ph10, Tue May 29 15:18:18 2007 UTC
# Line 26  is matched against the string Line 26  is matched against the string
26    <something> <something else> <something further>    <something> <something else> <something further>
27  .sp  .sp
28  there are three possible answers. The standard algorithm finds only one of  there are three possible answers. The standard algorithm finds only one of
29  them, whereas the DFA algorithm finds all three.  them, whereas the alternative algorithm finds all three.
30  .  .
31  .SH "REGULAR EXPRESSIONS AS TREES"  .SH "REGULAR EXPRESSIONS AS TREES"
32  .rs  .rs
# Line 41  correspond to the two matching algorithm Line 41  correspond to the two matching algorithm
41  .SH "THE STANDARD MATCHING ALGORITHM"  .SH "THE STANDARD MATCHING ALGORITHM"
42  .rs  .rs
43  .sp  .sp
44  In the terminology of Jeffrey Friedl's book \fIMastering Regular  In the terminology of Jeffrey Friedl's book "Mastering Regular
45  Expressions\fP, the standard algorithm is an "NFA algorithm". It conducts a  Expressions", the standard algorithm is an "NFA algorithm". It conducts a
46  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
47  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
48  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 63  straightforward for this algorithm to ke Line 63  straightforward for this algorithm to ke
63  matched by portions of the pattern in parentheses. This provides support for  matched by portions of the pattern in parentheses. This provides support for
64  capturing parentheses and back references.  capturing parentheses and back references.
65  .  .
66  .SH "THE DFA MATCHING ALGORITHM"  .SH "THE ALTERNATIVE MATCHING ALGORITHM"
67  .rs  .rs
68  .sp  .sp
69  DFA stands for "deterministic finite automaton", but you do not need to  This algorithm conducts a breadth-first search of the tree. Starting from the
70  understand the origins of that name. This algorithm conducts a breadth-first  first matching point in the subject, it scans the subject string from left to
71  search of the tree. Starting from the first matching point in the subject, it  right, once, character by character, and as it does this, it remembers all the
72  scans the subject string from left to right, once, character by character, and  paths through the tree that represent valid matches. In Friedl's terminology,
73  as it does this, it remembers all the paths through the tree that represent  this is a kind of "DFA algorithm", though it is not implemented as a
74  valid matches.  traditional finite state machine (it keeps multiple states active
75    simultaneously).
76  .P  .P
77  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
78  no more unterminated paths. At this point, terminated paths represent the  no more unterminated paths. At this point, terminated paths represent the
# Line 92  character of the subject. The algorithm Line 93  character of the subject. The algorithm
93  matches that start at later positions.  matches that start at later positions.
94  .P  .P
95  There are a number of features of PCRE regular expressions that are not  There are a number of features of PCRE regular expressions that are not
96  supported by the DFA matching algorithm. They are as follows:  supported by the alternative matching algorithm. They are as follows:
97  .P  .P
98  1. Because the algorithm finds all possible matches, the greedy or ungreedy  1. Because the algorithm finds all possible matches, the greedy or ungreedy
99  nature of repetition quantifiers is not relevant. Greedy and ungreedy  nature of repetition quantifiers is not relevant. Greedy and ungreedy
100  quantifiers are treated in exactly the same way.  quantifiers are treated in exactly the same way. However, possessive
101    quantifiers can make a difference when what follows could also match what is
102    quantified, for example in a pattern like this:
103    .sp
104      ^a++\ew!
105    .sp
106    This pattern matches "aaab!" but not "aaa!", which would be matched by a
107    non-possessive quantifier. Similarly, if an atomic group is present, it is
108    matched as if it were a standalone pattern at the current point, and the
109    longest match is then "locked in" for the rest of the overall pattern.
110  .P  .P
111  2. When dealing with multiple paths through the tree simultaneously, it is not  2. When dealing with multiple paths through the tree simultaneously, it is not
112  straightforward to keep track of captured substrings for the different matching  straightforward to keep track of captured substrings for the different matching
# Line 107  do this. This means that no captured sub Line 117  do this. This means that no captured sub
117  not supported, and cause errors if encountered.  not supported, and cause errors if encountered.
118  .P  .P
119  4. For the same reason, conditional expressions that use a backreference as the  4. For the same reason, conditional expressions that use a backreference as the
120  condition are not supported.  condition or test for a specific group recursion are not supported.
121  .P  .P
122  5. Callouts are supported, but the value of the \fIcapture_top\fP field is  5. Because many paths through the tree may be active, the \eK escape sequence,
123    which resets the start of the match when encountered (but may be on some paths
124    and not on others), is not supported. It causes an error if encountered.
125    .P
126    6. Callouts are supported, but the value of the \fIcapture_top\fP field is
127  always 1, and the value of the \fIcapture_last\fP field is always -1.  always 1, and the value of the \fIcapture_last\fP field is always -1.
128  .P  .P
129  6.  7.
130  The \eC escape sequence, which (in the standard algorithm) matches a single  The \eC escape sequence, which (in the standard algorithm) matches a single
131  byte, even in UTF-8 mode, is not supported because the DFA algorithm moves  byte, even in UTF-8 mode, is not supported because the alternative algorithm
132  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
133  through the tree.  through the tree.
134  .  .
135  .SH "ADVANTAGES OF THE DFA ALGORITHM"  .SH "ADVANTAGES OF THE ALTERNATIVE ALGORITHM"
136  .rs  .rs
137  .sp  .sp
138  Using the DFA matching algorithm provides the following advantages:  Using the alternative matching algorithm provides the following advantages:
139  .P  .P
140  1. All possible matches (at a single point in the subject) are automatically  1. All possible matches (at a single point in the subject) are automatically
141  found, and in particular, the longest match is found. To find more than one  found, and in particular, the longest match is found. To find more than one
# Line 130  callouts. Line 144  callouts.
144  .P  .P
145  2. There is much better support for partial matching. The restrictions on the  2. There is much better support for partial matching. The restrictions on the
146  content of the pattern that apply when using the standard algorithm for partial  content of the pattern that apply when using the standard algorithm for partial
147  matching do not apply to the DFA algorithm. For non-anchored patterns, the  matching do not apply to the alternative algorithm. For non-anchored patterns,
148  starting position of a partial match is available.  the starting position of a partial match is available.
149  .P  .P
150  3. Because the DFA algorithm scans the subject string just once, and never  3. Because the alternative algorithm scans the subject string just once, and
151  needs to backtrack, it is possible to pass very long subject strings to the  never needs to backtrack, it is possible to pass very long subject strings to
152  matching function in several pieces, checking for partial matching each time.  the matching function in several pieces, checking for partial matching each
153    time.
154  .  .
155  .SH "DISADVANTAGES OF THE DFA ALGORITHM"  .SH "DISADVANTAGES OF THE ALTERNATIVE ALGORITHM"
156  .rs  .rs
157  .sp  .sp
158  The DFA algorithm suffers from a number of disadvantages:  The alternative algorithm suffers from a number of disadvantages:
159  .P  .P
160  1. It is substantially slower than the standard algorithm. This is partly  1. It is substantially slower than the standard algorithm. This is partly
161  because it has to search for all possible matches, but is also because it is  because it has to search for all possible matches, but is also because it is
# Line 148  less susceptible to optimization. Line 163  less susceptible to optimization.
163  .P  .P
164  2. Capturing parentheses and back references are not supported.  2. Capturing parentheses and back references are not supported.
165  .P  .P
166  3. The "atomic group" feature of PCRE regular expressions is supported, but  3. Although atomic groups are supported, their use does not provide the
167  does not provide the advantage that it does for the standard algorithm.  performance advantage that it does for the standard algorithm.
168  .P  .
169  .in 0  .
170  Last updated: 06 June 2006  .SH AUTHOR
171  .br  .rs
172  Copyright (c) 1997-2006 University of Cambridge.  .sp
173    .nf
174    Philip Hazel
175    University Computing Service
176    Cambridge CB2 3QH, England.
177    .fi
178    .
179    .
180    .SH REVISION
181    .rs
182    .sp
183    .nf
184    Last updated: 29 May 2007
185    Copyright (c) 1997-2007 University of Cambridge.
186    .fi

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