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 There are a number of features of PCRE regular expressions that are not
111 supported by the alternative matching algorithm. They are as follows:
112
113 1. Because the algorithm finds all possible matches, the greedy or
114 ungreedy nature of repetition quantifiers is not relevant. Greedy and
115 ungreedy quantifiers are treated in exactly the same way. However, pos‐
116 sessive quantifiers can make a difference when what follows could also
117 match what is quantified, for example in a pattern like this:
118
119 ^a++\w!
120
121 This pattern matches "aaab!" but not "aaa!", which would be matched by
122 a non-possessive quantifier. Similarly, if an atomic group is present,
123 it is matched as if it were a standalone pattern at the current point,
124 and the longest match is then "locked in" for the rest of the overall
125 pattern.
126
127 2. When dealing with multiple paths through the tree simultaneously, it
128 is not straightforward to keep track of captured substrings for the
129 different matching possibilities, and PCRE's implementation of this
130 algorithm does not attempt to do this. This means that no captured sub‐
131 strings are available.
132
133 3. Because no substrings are captured, back references within the pat‐
134 tern are not supported, and cause errors if encountered.
135
136 4. For the same reason, conditional expressions that use a backrefer‐
137 ence as the condition or test for a specific group recursion are not
138 supported.
139
140 5. Because many paths through the tree may be active, the \K escape
141 sequence, which resets the start of the match when encountered (but may
142 be on some paths and not on others), is not supported. It causes an
143 error if encountered.
144
145 6. Callouts are supported, but the value of the capture_top field is
146 always 1, and the value of the capture_last field is always -1.
147
148 7. The \C escape sequence, which (in the standard algorithm) always
149 matches a single data unit, even in UTF-8, UTF-16 or UTF-32 modes, is
150 not supported in these modes, because the alternative algorithm moves
151 through the subject string one character (not data unit) at a time, for
152 all active paths through the tree.
153
154 8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
155 are not supported. (*FAIL) is supported, and behaves like a failing
156 negative assertion.
157
159
160 Using the alternative matching algorithm provides the following advan‐
161 tages:
162
163 1. All possible matches (at a single point in the subject) are automat‐
164 ically found, and in particular, the longest match is found. To find
165 more than one match using the standard algorithm, you have to do kludgy
166 things with callouts.
167
168 2. Because the alternative algorithm scans the subject string just
169 once, and never needs to backtrack (except for lookbehinds), it is pos‐
170 sible to pass very long subject strings to the matching function in
171 several pieces, checking for partial matching each time. Although it is
172 possible to do multi-segment matching using the standard algorithm by
173 retaining partially matched substrings, it is more complicated. The
174 pcrepartial documentation gives details of partial matching and dis‐
175 cusses multi-segment matching.
176
178
179 The alternative algorithm suffers from a number of disadvantages:
180
181 1. It is substantially slower than the standard algorithm. This is
182 partly because it has to search for all possible matches, but is also
183 because it is less susceptible to optimization.
184
185 2. Capturing parentheses and back references are not supported.
186
187 3. Although atomic groups are supported, their use does not provide the
188 performance advantage that it does for the standard algorithm.
189
191
192 Philip Hazel
193 University Computing Service
194 Cambridge CB2 3QH, England.
195
197
198 Last updated: 08 January 2012
199 Copyright (c) 1997-2012 University of Cambridge.
200
201
202
203PCRE 8.30 08 January 2012 PCREMATCHING(3)