new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / external / pcre2-10.32 / doc / pcre2matching.3
1 .TH PCRE2MATCHING 3 "29 September 2014" "PCRE2 10.00"
2 .SH NAME
3 PCRE2 - Perl-compatible regular expressions (revised API)
4 .SH "PCRE2 MATCHING ALGORITHMS"
5 .rs
6 .sp
7 This document describes the two different algorithms that are available in
8 PCRE2 for matching a compiled regular expression against a given subject
9 string. The "standard" algorithm is the one provided by the \fBpcre2_match()\fP
10 function. This works in the same as as Perl's matching function, and provide a
11 Perl-compatible matching operation. The just-in-time (JIT) optimization that is
12 described in the
13 .\" HREF
14 \fBpcre2jit\fP
15 .\"
16 documentation is compatible with this function.
17 .P
18 An alternative algorithm is provided by the \fBpcre2_dfa_match()\fP function;
19 it operates in a different way, and is not Perl-compatible. This alternative
20 has advantages and disadvantages compared with the standard algorithm, and
21 these are described below.
22 .P
23 When there is only one possible way in which a given subject string can match a
24 pattern, the two algorithms give the same answer. A difference arises, however,
25 when there are multiple possibilities. For example, if the pattern
26 .sp
27   ^<.*>
28 .sp
29 is matched against the string
30 .sp
31   <something> <something else> <something further>
32 .sp
33 there are three possible answers. The standard algorithm finds only one of
34 them, whereas the alternative algorithm finds all three.
35 .
36 .
37 .SH "REGULAR EXPRESSIONS AS TREES"
38 .rs
39 .sp
40 The set of strings that are matched by a regular expression can be represented
41 as a tree structure. An unlimited repetition in the pattern makes the tree of
42 infinite size, but it is still a tree. Matching the pattern to a given subject
43 string (from a given starting point) can be thought of as a search of the tree.
44 There are two ways to search a tree: depth-first and breadth-first, and these
45 correspond to the two matching algorithms provided by PCRE2.
46 .
47 .
48 .SH "THE STANDARD MATCHING ALGORITHM"
49 .rs
50 .sp
51 In the terminology of Jeffrey Friedl's book "Mastering Regular Expressions",
52 the standard algorithm is an "NFA algorithm". It conducts a depth-first search
53 of the pattern tree. That is, it proceeds along a single path through the tree,
54 checking that the subject matches what is required. When there is a mismatch,
55 the algorithm tries any alternatives at the current point, and if they all
56 fail, it backs up to the previous branch point in the tree, and tries the next
57 alternative branch at that level. This often involves backing up (moving to the
58 left) in the subject string as well. The order in which repetition branches are
59 tried is controlled by the greedy or ungreedy nature of the quantifier.
60 .P
61 If a leaf node is reached, a matching string has been found, and at that point
62 the algorithm stops. Thus, if there is more than one possible match, this
63 algorithm returns the first one that it finds. Whether this is the shortest,
64 the longest, or some intermediate length depends on the way the greedy and
65 ungreedy repetition quantifiers are specified in the pattern.
66 .P
67 Because it ends up with a single path through the tree, it is relatively
68 straightforward for this algorithm to keep track of the substrings that are
69 matched by portions of the pattern in parentheses. This provides support for
70 capturing parentheses and backreferences.
71 .
72 .
73 .SH "THE ALTERNATIVE MATCHING ALGORITHM"
74 .rs
75 .sp
76 This algorithm conducts a breadth-first search of the tree. Starting from the
77 first matching point in the subject, it scans the subject string from left to
78 right, once, character by character, and as it does this, it remembers all the
79 paths through the tree that represent valid matches. In Friedl's terminology,
80 this is a kind of "DFA algorithm", though it is not implemented as a
81 traditional finite state machine (it keeps multiple states active
82 simultaneously).
83 .P
84 Although the general principle of this matching algorithm is that it scans the
85 subject string only once, without backtracking, there is one exception: when a
86 lookaround assertion is encountered, the characters following or preceding the
87 current point have to be independently inspected.
88 .P
89 The scan continues until either the end of the subject is reached, or there are
90 no more unterminated paths. At this point, terminated paths represent the
91 different matching possibilities (if there are none, the match has failed).
92 Thus, if there is more than one possible match, this algorithm finds all of
93 them, and in particular, it finds the longest. The matches are returned in
94 decreasing order of length. There is an option to stop the algorithm after the
95 first match (which is necessarily the shortest) is found.
96 .P
97 Note that all the matches that are found start at the same point in the
98 subject. If the pattern
99 .sp
100   cat(er(pillar)?)?
101 .sp
102 is matched against the string "the caterpillar catchment", the result is the
103 three strings "caterpillar", "cater", and "cat" that start at the fifth
104 character of the subject. The algorithm does not automatically move on to find
105 matches that start at later positions.
106 .P
107 PCRE2's "auto-possessification" optimization usually applies to character
108 repeats at the end of a pattern (as well as internally). For example, the
109 pattern "a\ed+" is compiled as if it were "a\ed++" because there is no point
110 even considering the possibility of backtracking into the repeated digits. For
111 DFA matching, this means that only one possible match is found. If you really
112 do want multiple matches in such cases, either use an ungreedy repeat
113 ("a\ed+?") or set the PCRE2_NO_AUTO_POSSESS option when compiling.
114 .P
115 There are a number of features of PCRE2 regular expressions that are not
116 supported by the alternative matching algorithm. They are as follows:
117 .P
118 1. Because the algorithm finds all possible matches, the greedy or ungreedy
119 nature of repetition quantifiers is not relevant (though it may affect
120 auto-possessification, as just described). During matching, greedy and ungreedy
121 quantifiers are treated in exactly the same way. However, possessive
122 quantifiers can make a difference when what follows could also match what is
123 quantified, for example in a pattern like this:
124 .sp
125   ^a++\ew!
126 .sp
127 This pattern matches "aaab!" but not "aaa!", which would be matched by a
128 non-possessive quantifier. Similarly, if an atomic group is present, it is
129 matched as if it were a standalone pattern at the current point, and the
130 longest match is then "locked in" for the rest of the overall pattern.
131 .P
132 2. When dealing with multiple paths through the tree simultaneously, it is not
133 straightforward to keep track of captured substrings for the different matching
134 possibilities, and PCRE2's implementation of this algorithm does not attempt to
135 do this. This means that no captured substrings are available.
136 .P
137 3. Because no substrings are captured, backreferences within the pattern are
138 not supported, and cause errors if encountered.
139 .P
140 4. For the same reason, conditional expressions that use a backreference as the
141 condition or test for a specific group recursion are not supported.
142 .P
143 5. Because many paths through the tree may be active, the \eK escape sequence,
144 which resets the start of the match when encountered (but may be on some paths
145 and not on others), is not supported. It causes an error if encountered.
146 .P
147 6. Callouts are supported, but the value of the \fIcapture_top\fP field is
148 always 1, and the value of the \fIcapture_last\fP field is always 0.
149 .P
150 7. The \eC escape sequence, which (in the standard algorithm) always matches a
151 single code unit, even in a UTF mode, is not supported in these modes, because
152 the alternative algorithm moves through the subject string one character (not
153 code unit) at a time, for all active paths through the tree.
154 .P
155 8. Except for (*FAIL), the backtracking control verbs such as (*PRUNE) are not
156 supported. (*FAIL) is supported, and behaves like a failing negative assertion.
157 .
158 .
159 .SH "ADVANTAGES OF THE ALTERNATIVE ALGORITHM"
160 .rs
161 .sp
162 Using the alternative matching algorithm provides the following advantages:
163 .P
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 .P
169 2. Because the alternative algorithm scans the subject string just once, and
170 never needs to backtrack (except for lookbehinds), it is possible to pass very
171 long subject strings to the matching function in several pieces, checking for
172 partial matching each time. Although it is also possible to do multi-segment
173 matching using the standard algorithm, by retaining partially matched
174 substrings, it is more complicated. The
175 .\" HREF
176 \fBpcre2partial\fP
177 .\"
178 documentation gives details of partial matching and discusses multi-segment
179 matching.
180 .
181 .
182 .SH "DISADVANTAGES OF THE ALTERNATIVE ALGORITHM"
183 .rs
184 .sp
185 The alternative algorithm suffers from a number of disadvantages:
186 .P
187 1. It is substantially slower than the standard algorithm. This is partly
188 because it has to search for all possible matches, but is also because it is
189 less susceptible to optimization.
190 .P
191 2. Capturing parentheses and backreferences are not supported.
192 .P
193 3. Although atomic groups are supported, their use does not provide the
194 performance advantage that it does for the standard algorithm.
195 .
196 .
197 .SH AUTHOR
198 .rs
199 .sp
200 .nf
201 Philip Hazel
202 University Computing Service
203 Cambridge, England.
204 .fi
205 .
206 .
207 .SH REVISION
208 .rs
209 .sp
210 .nf
211 Last updated: 29 September 2014
212 Copyright (c) 1997-2014 University of Cambridge.
213 .fi