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

Wed Nov 17 17:55:57 2010 UTC (4 years, 5 months ago) by ph10
File MIME type: text/html
File size: 10042 byte(s)
```Documentation updates and tidies.

```
 1 nigel 77 2 3 pcrematching specification 4 5 6

pcrematching man page

7

10 ph10 111

11 nigel 77 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 ph10 111
15 nigel 77

25
PCRE MATCHING ALGORITHMS
26

27 This document describes the two different algorithms that are available in PCRE 28 for matching a compiled regular expression against a given subject string. The 29 "standard" algorithm is the one provided by the pcre_exec() function. 30 This works in the same was as Perl's matching function, and provides a 31 Perl-compatible matching operation. 32

33

34 An alternative algorithm is provided by the pcre_dfa_exec() function; 35 this operates in a different way, and is not Perl-compatible. It has advantages 36 and disadvantages compared with the standard algorithm, and these are described 37 below. 38

39

40 When there is only one possible way in which a given subject string can match a 41 pattern, the two algorithms give the same answer. A difference arises, however, 42 when there are multiple possibilities. For example, if the pattern 43

44              ^<.*>
45
46 is matched against the string 47
48              <something> <something else> <something further>
49
50 there are three possible answers. The standard algorithm finds only one of 51 nigel 93 them, whereas the alternative algorithm finds all three. 52 nigel 77

53
REGULAR EXPRESSIONS AS TREES
54

55 The set of strings that are matched by a regular expression can be represented 56 as a tree structure. An unlimited repetition in the pattern makes the tree of 57 infinite size, but it is still a tree. Matching the pattern to a given subject 58 string (from a given starting point) can be thought of as a search of the tree. 59 nigel 91 There are two ways to search a tree: depth-first and breadth-first, and these 60 correspond to the two matching algorithms provided by PCRE. 61 nigel 77

62
THE STANDARD MATCHING ALGORITHM
63

64 ph10 148 In the terminology of Jeffrey Friedl's book "Mastering Regular 65 Expressions", the standard algorithm is an "NFA algorithm". It conducts a 66 nigel 77 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 68 there is a mismatch, the algorithm tries any alternatives at the current point, 69 and if they all fail, it backs up to the previous branch point in the tree, and 70 tries the next alternative branch at that level. This often involves backing up 71 (moving to the left) in the subject string as well. The order in which 72 repetition branches are tried is controlled by the greedy or ungreedy nature of 73 the quantifier. 74

75

76 If a leaf node is reached, a matching string has been found, and at that point 77 the algorithm stops. Thus, if there is more than one possible match, this 78 algorithm returns the first one that it finds. Whether this is the shortest, 79 the longest, or some intermediate length depends on the way the greedy and 80 ungreedy repetition quantifiers are specified in the pattern. 81

82

83 Because it ends up with a single path through the tree, it is relatively 84 straightforward for this algorithm to keep track of the substrings that are 85 matched by portions of the pattern in parentheses. This provides support for 86 capturing parentheses and back references. 87

88 nigel 93
THE ALTERNATIVE MATCHING ALGORITHM
89 nigel 77

90 nigel 93 This algorithm conducts a breadth-first search of the tree. Starting from the 91 first matching point in the subject, it scans the subject string from left to 92 right, once, character by character, and as it does this, it remembers all the 93 paths through the tree that represent valid matches. In Friedl's terminology, 94 this is a kind of "DFA algorithm", though it is not implemented as a 95 traditional finite state machine (it keeps multiple states active 96 simultaneously). 97 nigel 77

98

99 ph10 461 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

104

105 nigel 77 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 107 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 109 ph10 572 them, and in particular, it finds the longest. The matches are returned in 110 decreasing order of length. There is an option to stop the algorithm after the 111 first match (which is necessarily the shortest) is found. 112 nigel 77

113

114 Note that all the matches that are found start at the same point in the 115 subject. If the pattern 116

117  ph10   572  cat(er(pillar)?)?
118  nigel  77
119 is matched against the string "the caterpillar catchment", the result will be 120 ph10 572 the three strings "caterpillar", "cater", and "cat" that start at the fifth 121 nigel 77 character of the subject. The algorithm does not automatically move on to find 122 matches that start at later positions. 123

124

125 There are a number of features of PCRE regular expressions that are not 126 nigel 93 supported by the alternative matching algorithm. They are as follows: 127 nigel 77

128

129 1. Because the algorithm finds all possible matches, the greedy or ungreedy 130 nature of repetition quantifiers is not relevant. Greedy and ungreedy 131 nigel 93 quantifiers are treated in exactly the same way. However, possessive 132 quantifiers can make a difference when what follows could also match what is 133 quantified, for example in a pattern like this: 134

135              ^a++\w!
136
137 This pattern matches "aaab!" but not "aaa!", which would be matched by a 138 non-possessive quantifier. Similarly, if an atomic group is present, it is 139 matched as if it were a standalone pattern at the current point, and the 140 longest match is then "locked in" for the rest of the overall pattern. 141 nigel 77

142

143 2. When dealing with multiple paths through the tree simultaneously, it is not 144 straightforward to keep track of captured substrings for the different matching 145 possibilities, and PCRE's implementation of this algorithm does not attempt to 146 do this. This means that no captured substrings are available. 147

148

149 3. Because no substrings are captured, back references within the pattern are 150 not supported, and cause errors if encountered. 151

152

153 4. For the same reason, conditional expressions that use a backreference as the 154 nigel 93 condition or test for a specific group recursion are not supported. 155 nigel 77

156

157 ph10 172 5. Because many paths through the tree may be active, the \K escape sequence, 158 which resets the start of the match when encountered (but may be on some paths 159 and not on others), is not supported. It causes an error if encountered. 160

161

162 6. Callouts are supported, but the value of the capture_top field is 163 nigel 77 always 1, and the value of the capture_last field is always -1. 164

165

166 ph10 211 7. The \C escape sequence, which (in the standard algorithm) matches a single 167 nigel 93 byte, even in UTF-8 mode, is not supported because the alternative algorithm 168 moves through the subject string one character at a time, for all active paths 169 nigel 77 through the tree. 170

171 ph10 211

172 ph10 345 8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE) are not 173 supported. (*FAIL) is supported, and behaves like a failing negative assertion. 174 ph10 211

175 nigel 93
176 nigel 77

177 nigel 93 Using the alternative matching algorithm provides the following advantages: 178 nigel 77

179

180 1. All possible matches (at a single point in the subject) are automatically 181 found, and in particular, the longest match is found. To find more than one 182 match using the standard algorithm, you have to do kludgy things with 183 callouts. 184

185

186 ph10 429 2. Because the alternative algorithm scans the subject string just once, and 187 nigel 93 never needs to backtrack, it is possible to pass very long subject strings to 188 the matching function in several pieces, checking for partial matching each 189 ph10 572 time. Although it is possible to do multi-segment matching using the standard 190 algorithm (pcre_exec()), by retaining partially matched substrings, it is 191 more complicated. The 192 ph10 461 pcrepartial 193 ph10 567 documentation gives details of partial matching and discusses multi-segment 194 matching. 195 nigel 77

196 nigel 93
197 nigel 77

198 nigel 93 The alternative algorithm suffers from a number of disadvantages: 199 nigel 77

200

201 1. It is substantially slower than the standard algorithm. This is partly 202 because it has to search for all possible matches, but is also because it is 203 less susceptible to optimization. 204

205

206 2. Capturing parentheses and back references are not supported. 207

208

209 nigel 93 3. Although atomic groups are supported, their use does not provide the 210 performance advantage that it does for the standard algorithm. 211 nigel 77

212 ph10 99
AUTHOR
213 nigel 77

214 ph10 99 Philip Hazel 215 nigel 77
216 ph10 99 University Computing Service 217
218 Cambridge CB2 3QH, England. 219
220

221
REVISION
222

223 ph10 572 Last updated: 17 November 2010 224 ph10 99
225 ph10 567 Copyright © 1997-2010 University of Cambridge. 226 ph10 99
227 nigel 77