ViewVC logotype

Contents of /code/trunk/doc/html/pcrematching.html

Parent Directory Parent Directory | Revision Log Revision Log

Revision 91 - (show annotations)
Sat Feb 24 21:41:34 2007 UTC (14 years, 2 months ago) by nigel
File MIME type: text/html
File size: 8345 byte(s)
Error occurred while calculating annotation data.
Load pcre-6.7 into code/trunk.
1 <html>
2 <head>
3 <title>pcrematching specification</title>
4 </head>
5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 <h1>pcrematching man page</h1>
7 <p>
8 Return to the <a href="index.html">PCRE index page</a>.
9 </p>
10 <p>
11 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
13 man page, in case the conversion went wrong.
14 <br>
15 <ul>
16 <li><a name="TOC1" href="#SEC1">PCRE MATCHING ALGORITHMS</a>
17 <li><a name="TOC2" href="#SEC2">REGULAR EXPRESSIONS AS TREES</a>
18 <li><a name="TOC3" href="#SEC3">THE STANDARD MATCHING ALGORITHM</a>
19 <li><a name="TOC4" href="#SEC4">THE DFA MATCHING ALGORITHM</a>
20 <li><a name="TOC5" href="#SEC5">ADVANTAGES OF THE DFA ALGORITHM</a>
21 <li><a name="TOC6" href="#SEC6">DISADVANTAGES OF THE DFA ALGORITHM</a>
22 </ul>
23 <br><a name="SEC1" href="#TOC1">PCRE MATCHING ALGORITHMS</a><br>
24 <P>
25 This document describes the two different algorithms that are available in PCRE
26 for matching a compiled regular expression against a given subject string. The
27 "standard" algorithm is the one provided by the <b>pcre_exec()</b> function.
28 This works in the same was as Perl's matching function, and provides a
29 Perl-compatible matching operation.
30 </P>
31 <P>
32 An alternative algorithm is provided by the <b>pcre_dfa_exec()</b> function;
33 this operates in a different way, and is not Perl-compatible. It has advantages
34 and disadvantages compared with the standard algorithm, and these are described
35 below.
36 </P>
37 <P>
38 When there is only one possible way in which a given subject string can match a
39 pattern, the two algorithms give the same answer. A difference arises, however,
40 when there are multiple possibilities. For example, if the pattern
41 <pre>
42 ^&#60;.*&#62;
43 </pre>
44 is matched against the string
45 <pre>
46 &#60;something&#62; &#60;something else&#62; &#60;something further&#62;
47 </pre>
48 there are three possible answers. The standard algorithm finds only one of
49 them, whereas the DFA algorithm finds all three.
50 </P>
51 <br><a name="SEC2" href="#TOC1">REGULAR EXPRESSIONS AS TREES</a><br>
52 <P>
53 The set of strings that are matched by a regular expression can be represented
54 as a tree structure. An unlimited repetition in the pattern makes the tree of
55 infinite size, but it is still a tree. Matching the pattern to a given subject
56 string (from a given starting point) can be thought of as a search of the tree.
57 There are two ways to search a tree: depth-first and breadth-first, and these
58 correspond to the two matching algorithms provided by PCRE.
59 </P>
60 <br><a name="SEC3" href="#TOC1">THE STANDARD MATCHING ALGORITHM</a><br>
61 <P>
62 In the terminology of Jeffrey Friedl's book \fIMastering Regular
63 Expressions\fP, the standard algorithm is an "NFA algorithm". It conducts a
64 depth-first search of the pattern tree. That is, it proceeds along a single
65 path through the tree, checking that the subject matches what is required. When
66 there is a mismatch, the algorithm tries any alternatives at the current point,
67 and if they all fail, it backs up to the previous branch point in the tree, and
68 tries the next alternative branch at that level. This often involves backing up
69 (moving to the left) in the subject string as well. The order in which
70 repetition branches are tried is controlled by the greedy or ungreedy nature of
71 the quantifier.
72 </P>
73 <P>
74 If a leaf node is reached, a matching string has been found, and at that point
75 the algorithm stops. Thus, if there is more than one possible match, this
76 algorithm returns the first one that it finds. Whether this is the shortest,
77 the longest, or some intermediate length depends on the way the greedy and
78 ungreedy repetition quantifiers are specified in the pattern.
79 </P>
80 <P>
81 Because it ends up with a single path through the tree, it is relatively
82 straightforward for this algorithm to keep track of the substrings that are
83 matched by portions of the pattern in parentheses. This provides support for
84 capturing parentheses and back references.
85 </P>
86 <br><a name="SEC4" href="#TOC1">THE DFA MATCHING ALGORITHM</a><br>
87 <P>
88 DFA stands for "deterministic finite automaton", but you do not need to
89 understand the origins of that name. This algorithm conducts a breadth-first
90 search of the tree. Starting from the first matching point in the subject, it
91 scans the subject string from left to right, once, character by character, and
92 as it does this, it remembers all the paths through the tree that represent
93 valid matches.
94 </P>
95 <P>
96 The scan continues until either the end of the subject is reached, or there are
97 no more unterminated paths. At this point, terminated paths represent the
98 different matching possibilities (if there are none, the match has failed).
99 Thus, if there is more than one possible match, this algorithm finds all of
100 them, and in particular, it finds the longest. In PCRE, there is an option to
101 stop the algorithm after the first match (which is necessarily the shortest)
102 has been found.
103 </P>
104 <P>
105 Note that all the matches that are found start at the same point in the
106 subject. If the pattern
107 <pre>
108 cat(er(pillar)?)
109 </pre>
110 is matched against the string "the caterpillar catchment", the result will be
111 the three strings "cat", "cater", and "caterpillar" that start at the fourth
112 character of the subject. The algorithm does not automatically move on to find
113 matches that start at later positions.
114 </P>
115 <P>
116 There are a number of features of PCRE regular expressions that are not
117 supported by the DFA matching algorithm. They are as follows:
118 </P>
119 <P>
120 1. Because the algorithm finds all possible matches, the greedy or ungreedy
121 nature of repetition quantifiers is not relevant. Greedy and ungreedy
122 quantifiers are treated in exactly the same way.
123 </P>
124 <P>
125 2. When dealing with multiple paths through the tree simultaneously, it is not
126 straightforward to keep track of captured substrings for the different matching
127 possibilities, and PCRE's implementation of this algorithm does not attempt to
128 do this. This means that no captured substrings are available.
129 </P>
130 <P>
131 3. Because no substrings are captured, back references within the pattern are
132 not supported, and cause errors if encountered.
133 </P>
134 <P>
135 4. For the same reason, conditional expressions that use a backreference as the
136 condition are not supported.
137 </P>
138 <P>
139 5. Callouts are supported, but the value of the <i>capture_top</i> field is
140 always 1, and the value of the <i>capture_last</i> field is always -1.
141 </P>
142 <P>
143 6.
144 The \C escape sequence, which (in the standard algorithm) matches a single
145 byte, even in UTF-8 mode, is not supported because the DFA algorithm moves
146 through the subject string one character at a time, for all active paths
147 through the tree.
148 </P>
149 <br><a name="SEC5" href="#TOC1">ADVANTAGES OF THE DFA ALGORITHM</a><br>
150 <P>
151 Using the DFA matching algorithm provides the following advantages:
152 </P>
153 <P>
154 1. All possible matches (at a single point in the subject) are automatically
155 found, and in particular, the longest match is found. To find more than one
156 match using the standard algorithm, you have to do kludgy things with
157 callouts.
158 </P>
159 <P>
160 2. There is much better support for partial matching. The restrictions on the
161 content of the pattern that apply when using the standard algorithm for partial
162 matching do not apply to the DFA algorithm. For non-anchored patterns, the
163 starting position of a partial match is available.
164 </P>
165 <P>
166 3. Because the DFA algorithm scans the subject string just once, and never
167 needs to backtrack, it is possible to pass very long subject strings to the
168 matching function in several pieces, checking for partial matching each time.
169 </P>
170 <br><a name="SEC6" href="#TOC1">DISADVANTAGES OF THE DFA ALGORITHM</a><br>
171 <P>
172 The DFA algorithm suffers from a number of disadvantages:
173 </P>
174 <P>
175 1. It is substantially slower than the standard algorithm. This is partly
176 because it has to search for all possible matches, but is also because it is
177 less susceptible to optimization.
178 </P>
179 <P>
180 2. Capturing parentheses and back references are not supported.
181 </P>
182 <P>
183 3. The "atomic group" feature of PCRE regular expressions is supported, but
184 does not provide the advantage that it does for the standard algorithm.
185 </P>
186 <P>
187 Last updated: 06 June 2006
188 <br>
189 Copyright &copy; 1997-2006 University of Cambridge.
190 <p>
191 Return to the <a href="index.html">PCRE index page</a>.
192 </p>

  ViewVC Help
Powered by ViewVC 1.1.5