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

Fri Mar 2 13:10:43 2007 UTC (6 years, 3 months ago) by nigel
File MIME type: text/html
File size: 9007 byte(s)
``` r6896@hex:  nm | 2007-03-02 13:09:14 +0000
Added EOL and keywork properties throughout

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

pcrematching man page

7

10

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
15

23
PCRE MATCHING ALGORITHMS
24

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 pcre_exec() function. 28 This works in the same was as Perl's matching function, and provides a 29 Perl-compatible matching operation. 30

31

32 An alternative algorithm is provided by the pcre_dfa_exec() 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

37

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

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

51
REGULAR EXPRESSIONS AS TREES
52

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 nigel 91 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 nigel 77

60
THE STANDARD MATCHING ALGORITHM
61

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

73

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

80

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

86 nigel 93
THE ALTERNATIVE MATCHING ALGORITHM
87 nigel 77

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

96

97 The scan continues until either the end of the subject is reached, or there are 98 no more unterminated paths. At this point, terminated paths represent the 99 different matching possibilities (if there are none, the match has failed). 100 Thus, if there is more than one possible match, this algorithm finds all of 101 them, and in particular, it finds the longest. In PCRE, there is an option to 102 stop the algorithm after the first match (which is necessarily the shortest) 103 has been found. 104

105

106 Note that all the matches that are found start at the same point in the 107 subject. If the pattern 108

109             cat(er(pillar)?)
110
111 is matched against the string "the caterpillar catchment", the result will be 112 the three strings "cat", "cater", and "caterpillar" that start at the fourth 113 character of the subject. The algorithm does not automatically move on to find 114 matches that start at later positions. 115

116

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

120

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

127             ^a++\w!
128
129 This pattern matches "aaab!" but not "aaa!", which would be matched by a 130 non-possessive quantifier. Similarly, if an atomic group is present, it is 131 matched as if it were a standalone pattern at the current point, and the 132 longest match is then "locked in" for the rest of the overall pattern. 133 nigel 77

134

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

140

141 3. Because no substrings are captured, back references within the pattern are 142 not supported, and cause errors if encountered. 143

144

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

148

149 5. Callouts are supported, but the value of the capture_top field is 150 always 1, and the value of the capture_last field is always -1. 151

152

153 6. 154 The \C escape sequence, which (in the standard algorithm) matches a single 155 nigel 93 byte, even in UTF-8 mode, is not supported because the alternative algorithm 156 moves through the subject string one character at a time, for all active paths 157 nigel 77 through the tree. 158

159 nigel 93
160 nigel 77

161 nigel 93 Using the alternative matching algorithm provides the following advantages: 162 nigel 77

163

164 1. All possible matches (at a single point in the subject) are automatically 165 found, and in particular, the longest match is found. To find more than one 166 match using the standard algorithm, you have to do kludgy things with 167 callouts. 168

169

170 2. There is much better support for partial matching. The restrictions on the 171 content of the pattern that apply when using the standard algorithm for partial 172 nigel 93 matching do not apply to the alternative algorithm. For non-anchored patterns, 173 the starting position of a partial match is available. 174 nigel 77

175

176 nigel 93 3. Because the alternative algorithm scans the subject string just once, and 177 never needs to backtrack, it is possible to pass very long subject strings to 178 the matching function in several pieces, checking for partial matching each 179 time. 180 nigel 77

181 nigel 93
182 nigel 77

183 nigel 93 The alternative algorithm suffers from a number of disadvantages: 184 nigel 77

185

186 1. It is substantially slower than the standard algorithm. This is partly 187 because it has to search for all possible matches, but is also because it is 188 less susceptible to optimization. 189

190

191 2. Capturing parentheses and back references are not supported. 192

193

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

197

198 nigel 93 Last updated: 24 November 2006 199 nigel 77
200 nigel 91 Copyright © 1997-2006 University of Cambridge. 201 nigel 77