1<html>
2<head>
3<title>pcre2matching specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcre2matching man page</h1>
7<p>
8Return to the <a href="index.html">PCRE2 index page</a>.
9</p>
10<p>
11This page is part of the PCRE2 HTML documentation. It was generated
12automatically from the original man page. If there is any nonsense in it,
13please consult the man page, in case the conversion went wrong.
14<br>
15<ul>
16<li><a name="TOC1" href="#SEC1">PCRE2 MATCHING ALGORITHMS</a>
17<li><a name="TOC2" href="#SEC2">REGULAR EXPRESSIONS AS TREES</a>
18<li><a name="TOC3" href="#SEC3">THE STANDARD MATCHING ALGORITHM</a>
19<li><a name="TOC4" href="#SEC4">THE ALTERNATIVE MATCHING ALGORITHM</a>
20<li><a name="TOC5" href="#SEC5">ADVANTAGES OF THE ALTERNATIVE ALGORITHM</a>
21<li><a name="TOC6" href="#SEC6">DISADVANTAGES OF THE ALTERNATIVE ALGORITHM</a>
22<li><a name="TOC7" href="#SEC7">AUTHOR</a>
23<li><a name="TOC8" href="#SEC8">REVISION</a>
24</ul>
25<br><a name="SEC1" href="#TOC1">PCRE2 MATCHING ALGORITHMS</a><br>
26<P>
27This document describes the two different algorithms that are available in
28PCRE2 for matching a compiled regular expression against a given subject
29string. The "standard" algorithm is the one provided by the <b>pcre2_match()</b>
30function. This works in the same as as Perl's matching function, and provide a
31Perl-compatible matching operation. The just-in-time (JIT) optimization that is
32described in the
33<a href="pcre2jit.html"><b>pcre2jit</b></a>
34documentation is compatible with this function.
35</P>
36<P>
37An alternative algorithm is provided by the <b>pcre2_dfa_match()</b> function;
38it operates in a different way, and is not Perl-compatible. This alternative
39has advantages and disadvantages compared with the standard algorithm, and
40these are described below.
41</P>
42<P>
43When there is only one possible way in which a given subject string can match a
44pattern, the two algorithms give the same answer. A difference arises, however,
45when there are multiple possibilities. For example, if the pattern
46<pre>
47  ^&#60;.*&#62;
48</pre>
49is matched against the string
50<pre>
51  &#60;something&#62; &#60;something else&#62; &#60;something further&#62;
52</pre>
53there are three possible answers. The standard algorithm finds only one of
54them, whereas the alternative algorithm finds all three.
55</P>
56<br><a name="SEC2" href="#TOC1">REGULAR EXPRESSIONS AS TREES</a><br>
57<P>
58The set of strings that are matched by a regular expression can be represented
59as a tree structure. An unlimited repetition in the pattern makes the tree of
60infinite size, but it is still a tree. Matching the pattern to a given subject
61string (from a given starting point) can be thought of as a search of the tree.
62There are two ways to search a tree: depth-first and breadth-first, and these
63correspond to the two matching algorithms provided by PCRE2.
64</P>
65<br><a name="SEC3" href="#TOC1">THE STANDARD MATCHING ALGORITHM</a><br>
66<P>
67In the terminology of Jeffrey Friedl's book "Mastering Regular Expressions",
68the standard algorithm is an "NFA algorithm". It conducts a depth-first search
69of the pattern tree. That is, it proceeds along a single path through the tree,
70checking that the subject matches what is required. When there is a mismatch,
71the algorithm tries any alternatives at the current point, and if they all
72fail, it backs up to the previous branch point in the tree, and tries the next
73alternative branch at that level. This often involves backing up (moving to the
74left) in the subject string as well. The order in which repetition branches are
75tried is controlled by the greedy or ungreedy nature of the quantifier.
76</P>
77<P>
78If a leaf node is reached, a matching string has been found, and at that point
79the algorithm stops. Thus, if there is more than one possible match, this
80algorithm returns the first one that it finds. Whether this is the shortest,
81the longest, or some intermediate length depends on the way the greedy and
82ungreedy repetition quantifiers are specified in the pattern.
83</P>
84<P>
85Because it ends up with a single path through the tree, it is relatively
86straightforward for this algorithm to keep track of the substrings that are
87matched by portions of the pattern in parentheses. This provides support for
88capturing parentheses and backreferences.
89</P>
90<br><a name="SEC4" href="#TOC1">THE ALTERNATIVE MATCHING ALGORITHM</a><br>
91<P>
92This algorithm conducts a breadth-first search of the tree. Starting from the
93first matching point in the subject, it scans the subject string from left to
94right, once, character by character, and as it does this, it remembers all the
95paths through the tree that represent valid matches. In Friedl's terminology,
96this is a kind of "DFA algorithm", though it is not implemented as a
97traditional finite state machine (it keeps multiple states active
98simultaneously).
99</P>
100<P>
101Although the general principle of this matching algorithm is that it scans the
102subject string only once, without backtracking, there is one exception: when a
103lookaround assertion is encountered, the characters following or preceding the
104current point have to be independently inspected.
105</P>
106<P>
107The scan continues until either the end of the subject is reached, or there are
108no more unterminated paths. At this point, terminated paths represent the
109different matching possibilities (if there are none, the match has failed).
110Thus, if there is more than one possible match, this algorithm finds all of
111them, and in particular, it finds the longest. The matches are returned in
112decreasing order of length. There is an option to stop the algorithm after the
113first match (which is necessarily the shortest) is found.
114</P>
115<P>
116Note that all the matches that are found start at the same point in the
117subject. If the pattern
118<pre>
119  cat(er(pillar)?)?
120</pre>
121is matched against the string "the caterpillar catchment", the result is the
122three strings "caterpillar", "cater", and "cat" that start at the fifth
123character of the subject. The algorithm does not automatically move on to find
124matches that start at later positions.
125</P>
126<P>
127PCRE2's "auto-possessification" optimization usually applies to character
128repeats at the end of a pattern (as well as internally). For example, the
129pattern "a\d+" is compiled as if it were "a\d++" because there is no point
130even considering the possibility of backtracking into the repeated digits. For
131DFA matching, this means that only one possible match is found. If you really
132do want multiple matches in such cases, either use an ungreedy repeat
133("a\d+?") or set the PCRE2_NO_AUTO_POSSESS option when compiling.
134</P>
135<P>
136There are a number of features of PCRE2 regular expressions that are not
137supported or behave differently in the alternative matching function. Those
138that are not supported cause an error if encountered.
139</P>
140<P>
1411. Because the algorithm finds all possible matches, the greedy or ungreedy
142nature of repetition quantifiers is not relevant (though it may affect
143auto-possessification, as just described). During matching, greedy and ungreedy
144quantifiers are treated in exactly the same way. However, possessive
145quantifiers can make a difference when what follows could also match what is
146quantified, for example in a pattern like this:
147<pre>
148  ^a++\w!
149</pre>
150This pattern matches "aaab!" but not "aaa!", which would be matched by a
151non-possessive quantifier. Similarly, if an atomic group is present, it is
152matched as if it were a standalone pattern at the current point, and the
153longest match is then "locked in" for the rest of the overall pattern.
154</P>
155<P>
1562. When dealing with multiple paths through the tree simultaneously, it is not
157straightforward to keep track of captured substrings for the different matching
158possibilities, and PCRE2's implementation of this algorithm does not attempt to
159do this. This means that no captured substrings are available.
160</P>
161<P>
1623. Because no substrings are captured, backreferences within the pattern are
163not supported.
164</P>
165<P>
1664. For the same reason, conditional expressions that use a backreference as the
167condition or test for a specific group recursion are not supported.
168</P>
169<P>
1705. Again for the same reason, script runs are not supported.
171</P>
172<P>
1736. Because many paths through the tree may be active, the \K escape sequence,
174which resets the start of the match when encountered (but may be on some paths
175and not on others), is not supported.
176</P>
177<P>
1787. Callouts are supported, but the value of the <i>capture_top</i> field is
179always 1, and the value of the <i>capture_last</i> field is always 0.
180</P>
181<P>
1828. The \C escape sequence, which (in the standard algorithm) always matches a
183single code unit, even in a UTF mode, is not supported in these modes, because
184the alternative algorithm moves through the subject string one character (not
185code unit) at a time, for all active paths through the tree.
186</P>
187<P>
1889. Except for (*FAIL), the backtracking control verbs such as (*PRUNE) are not
189supported. (*FAIL) is supported, and behaves like a failing negative assertion.
190</P>
191<P>
19210. The PCRE2_MATCH_INVALID_UTF option for <b>pcre2_compile()</b> is not
193supported by <b>pcre2_dfa_match()</b>.
194</P>
195<br><a name="SEC5" href="#TOC1">ADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>
196<P>
197Using the alternative matching algorithm provides the following advantages:
198</P>
199<P>
2001. All possible matches (at a single point in the subject) are automatically
201found, and in particular, the longest match is found. To find more than one
202match using the standard algorithm, you have to do kludgy things with
203callouts.
204</P>
205<P>
2062. Because the alternative algorithm scans the subject string just once, and
207never needs to backtrack (except for lookbehinds), it is possible to pass very
208long subject strings to the matching function in several pieces, checking for
209partial matching each time. Although it is also possible to do multi-segment
210matching using the standard algorithm, by retaining partially matched
211substrings, it is more complicated. The
212<a href="pcre2partial.html"><b>pcre2partial</b></a>
213documentation gives details of partial matching and discusses multi-segment
214matching.
215</P>
216<br><a name="SEC6" href="#TOC1">DISADVANTAGES OF THE ALTERNATIVE ALGORITHM</a><br>
217<P>
218The alternative algorithm suffers from a number of disadvantages:
219</P>
220<P>
2211. It is substantially slower than the standard algorithm. This is partly
222because it has to search for all possible matches, but is also because it is
223less susceptible to optimization.
224</P>
225<P>
2262. Capturing parentheses, backreferences, script runs, and matching within
227invalid UTF string are not supported.
228</P>
229<P>
2303. Although atomic groups are supported, their use does not provide the
231performance advantage that it does for the standard algorithm.
232</P>
233<br><a name="SEC7" href="#TOC1">AUTHOR</a><br>
234<P>
235Philip Hazel
236<br>
237University Computing Service
238<br>
239Cambridge, England.
240<br>
241</P>
242<br><a name="SEC8" href="#TOC1">REVISION</a><br>
243<P>
244Last updated: 23 May 2019
245<br>
246Copyright &copy; 1997-2019 University of Cambridge.
247<br>
248<p>
249Return to the <a href="index.html">PCRE2 index page</a>.
250</p>
251