1PCREMATCHING(3) Library Functions Manual PCREMATCHING(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
9
10 This document describes the two different algorithms that are available
11 in PCRE for matching a compiled regular expression against a given sub‐
12 ject string. The "standard" algorithm is the one provided by the
13 pcre_exec(), pcre16_exec() and pcre32_exec() functions. These work in
14 the same as as Perl's matching function, and provide a Perl-compatible
15 matching operation. The just-in-time (JIT) optimization that is
16 described in the pcrejit documentation is compatible with these func‐
17 tions.
18
19 An alternative algorithm is provided by the pcre_dfa_exec(),
20 pcre16_dfa_exec() and pcre32_dfa_exec() functions; they operate in a
21 different way, and are not Perl-compatible. This alternative has advan‐
22 tages and disadvantages compared with the standard algorithm, and these
23 are described below.
24
25 When there is only one possible way in which a given subject string can
26 match a pattern, the two algorithms give the same answer. A difference
27 arises, however, when there are multiple possibilities. For example, if
28 the pattern
29
30 ^<.*>
31
32 is matched against the string
33
34 <something> <something else> <something further>
35
36 there are three possible answers. The standard algorithm finds only one
37 of them, whereas the alternative algorithm finds all three.
38
40
41 The set of strings that are matched by a regular expression can be rep‐
42 resented as a tree structure. An unlimited repetition in the pattern
43 makes the tree of infinite size, but it is still a tree. Matching the
44 pattern to a given subject string (from a given starting point) can be
45 thought of as a search of the tree. There are two ways to search a
46 tree: depth-first and breadth-first, and these correspond to the two
47 matching algorithms provided by PCRE.
48
50
51 In the terminology of Jeffrey Friedl's book "Mastering Regular Expres‐
52 sions", the standard algorithm is an "NFA algorithm". It conducts a
53 depth-first search of the pattern tree. That is, it proceeds along a
54 single path through the tree, checking that the subject matches what is
55 required. When there is a mismatch, the algorithm tries any alterna‐
56 tives at the current point, and if they all fail, it backs up to the
57 previous branch point in the tree, and tries the next alternative
58 branch at that level. This often involves backing up (moving to the
59 left) in the subject string as well. The order in which repetition
60 branches are tried is controlled by the greedy or ungreedy nature of
61 the quantifier.
62
63 If a leaf node is reached, a matching string has been found, and at
64 that point the algorithm stops. Thus, if there is more than one possi‐
65 ble match, this algorithm returns the first one that it finds. Whether
66 this is the shortest, the longest, or some intermediate length depends
67 on the way the greedy and ungreedy repetition quantifiers are specified
68 in the pattern.
69
70 Because it ends up with a single path through the tree, it is rela‐
71 tively straightforward for this algorithm to keep track of the sub‐
72 strings that are matched by portions of the pattern in parentheses.
73 This provides support for capturing parentheses and back references.
74
76
77 This algorithm conducts a breadth-first search of the tree. Starting
78 from the first matching point in the subject, it scans the subject
79 string from left to right, once, character by character, and as it does
80 this, it remembers all the paths through the tree that represent valid
81 matches. In Friedl's terminology, this is a kind of "DFA algorithm",
82 though it is not implemented as a traditional finite state machine (it
83 keeps multiple states active simultaneously).
84
85 Although the general principle of this matching algorithm is that it
86 scans the subject string only once, without backtracking, there is one
87 exception: when a lookaround assertion is encountered, the characters
88 following or preceding the current point have to be independently
89 inspected.
90
91 The scan continues until either the end of the subject is reached, or
92 there are no more unterminated paths. At this point, terminated paths
93 represent the different matching possibilities (if there are none, the
94 match has failed). Thus, if there is more than one possible match,
95 this algorithm finds all of them, and in particular, it finds the long‐
96 est. The matches are returned in decreasing order of length. There is
97 an option to stop the algorithm after the first match (which is neces‐
98 sarily the shortest) is found.
99
100 Note that all the matches that are found start at the same point in the
101 subject. If the pattern
102
103 cat(er(pillar)?)?
104
105 is matched against the string "the caterpillar catchment", the result
106 will be the three strings "caterpillar", "cater", and "cat" that start
107 at the fifth character of the subject. The algorithm does not automati‐
108 cally move on to find matches that start at later positions.
109
110 PCRE's "auto-possessification" optimization usually applies to charac‐
111 ter repeats at the end of a pattern (as well as internally). For exam‐
112 ple, the pattern "a\d+" is compiled as if it were "a\d++" because there
113 is no point even considering the possibility of backtracking into the
114 repeated digits. For DFA matching, this means that only one possible
115 match is found. If you really do want multiple matches in such cases,
116 either use an ungreedy repeat ("a\d+?") or set the PCRE_NO_AUTO_POSSESS
117 option when compiling.
118
119 There are a number of features of PCRE regular expressions that are not
120 supported by the alternative matching algorithm. They are as follows:
121
122 1. Because the algorithm finds all possible matches, the greedy or
123 ungreedy nature of repetition quantifiers is not relevant. Greedy and
124 ungreedy quantifiers are treated in exactly the same way. However, pos‐
125 sessive quantifiers can make a difference when what follows could also
126 match what is quantified, for example in a pattern like this:
127
128 ^a++\w!
129
130 This pattern matches "aaab!" but not "aaa!", which would be matched by
131 a non-possessive quantifier. Similarly, if an atomic group is present,
132 it is matched as if it were a standalone pattern at the current point,
133 and the longest match is then "locked in" for the rest of the overall
134 pattern.
135
136 2. When dealing with multiple paths through the tree simultaneously, it
137 is not straightforward to keep track of captured substrings for the
138 different matching possibilities, and PCRE's implementation of this
139 algorithm does not attempt to do this. This means that no captured sub‐
140 strings are available.
141
142 3. Because no substrings are captured, back references within the pat‐
143 tern are not supported, and cause errors if encountered.
144
145 4. For the same reason, conditional expressions that use a backrefer‐
146 ence as the condition or test for a specific group recursion are not
147 supported.
148
149 5. Because many paths through the tree may be active, the \K escape
150 sequence, which resets the start of the match when encountered (but may
151 be on some paths and not on others), is not supported. It causes an
152 error if encountered.
153
154 6. Callouts are supported, but the value of the capture_top field is
155 always 1, and the value of the capture_last field is always -1.
156
157 7. The \C escape sequence, which (in the standard algorithm) always
158 matches a single data unit, even in UTF-8, UTF-16 or UTF-32 modes, is
159 not supported in these modes, because the alternative algorithm moves
160 through the subject string one character (not data unit) at a time, for
161 all active paths through the tree.
162
163 8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
164 are not supported. (*FAIL) is supported, and behaves like a failing
165 negative assertion.
166
168
169 Using the alternative matching algorithm provides the following advan‐
170 tages:
171
172 1. All possible matches (at a single point in the subject) are automat‐
173 ically found, and in particular, the longest match is found. To find
174 more than one match using the standard algorithm, you have to do kludgy
175 things with callouts.
176
177 2. Because the alternative algorithm scans the subject string just
178 once, and never needs to backtrack (except for lookbehinds), it is pos‐
179 sible to pass very long subject strings to the matching function in
180 several pieces, checking for partial matching each time. Although it is
181 possible to do multi-segment matching using the standard algorithm by
182 retaining partially matched substrings, it is more complicated. The
183 pcrepartial documentation gives details of partial matching and dis‐
184 cusses multi-segment matching.
185
187
188 The alternative algorithm suffers from a number of disadvantages:
189
190 1. It is substantially slower than the standard algorithm. This is
191 partly because it has to search for all possible matches, but is also
192 because it is less susceptible to optimization.
193
194 2. Capturing parentheses and back references are not supported.
195
196 3. Although atomic groups are supported, their use does not provide the
197 performance advantage that it does for the standard algorithm.
198
200
201 Philip Hazel
202 University Computing Service
203 Cambridge CB2 3QH, England.
204
206
207 Last updated: 12 November 2013
208 Copyright (c) 1997-2012 University of Cambridge.
209
210
211
212PCRE 8.34 12 November 2013 PCREMATCHING(3)