1PCRE2MATCHING(3)           Library Functions Manual           PCRE2MATCHING(3)
2
3
4

NAME

6       PCRE2 - Perl-compatible regular expressions (revised API)
7

PCRE2 MATCHING ALGORITHMS

9
10       This document describes the two different algorithms that are available
11       in PCRE2 for matching a compiled regular  expression  against  a  given
12       subject  string.  The  "standard"  algorithm is the one provided by the
13       pcre2_match() function. This works in the same as  as  Perl's  matching
14       function,  and  provide a Perl-compatible matching operation. The just-
15       in-time (JIT) optimization that is described in the pcre2jit documenta‐
16       tion is compatible with this function.
17
18       An alternative algorithm is provided by the pcre2_dfa_match() function;
19       it operates in a different way, and is not Perl-compatible. This alter‐
20       native  has advantages and disadvantages compared with the standard al‐
21       gorithm, and these are described below.
22
23       When there is only one possible way in which a given subject string can
24       match  a pattern, the two algorithms give the same answer. A difference
25       arises, however, when there are multiple possibilities. For example, if
26       the pattern
27
28         ^<.*>
29
30       is matched against the string
31
32         <something> <something else> <something further>
33
34       there are three possible answers. The standard algorithm finds only one
35       of them, whereas the alternative algorithm finds all three.
36

REGULAR EXPRESSIONS AS TREES

38
39       The set of strings that are matched by a regular expression can be rep‐
40       resented  as  a  tree structure. An unlimited repetition in the pattern
41       makes the tree of infinite size, but it is still a tree.  Matching  the
42       pattern  to a given subject string (from a given starting point) can be
43       thought of as a search of the tree.  There are two  ways  to  search  a
44       tree:  depth-first  and  breadth-first, and these correspond to the two
45       matching algorithms provided by PCRE2.
46

THE STANDARD MATCHING ALGORITHM

48
49       In the terminology of Jeffrey Friedl's book "Mastering Regular  Expres‐
50       sions",  the  standard  algorithm  is an "NFA algorithm". It conducts a
51       depth-first search of the pattern tree. That is, it  proceeds  along  a
52       single path through the tree, checking that the subject matches what is
53       required. When there is a mismatch, the algorithm  tries  any  alterna‐
54       tives  at  the  current point, and if they all fail, it backs up to the
55       previous branch point in the  tree,  and  tries  the  next  alternative
56       branch  at  that  level.  This often involves backing up (moving to the
57       left) in the subject string as well.  The  order  in  which  repetition
58       branches  are  tried  is controlled by the greedy or ungreedy nature of
59       the quantifier.
60
61       If a leaf node is reached, a matching string has  been  found,  and  at
62       that  point the algorithm stops. Thus, if there is more than one possi‐
63       ble match, this algorithm returns the first one that it finds.  Whether
64       this  is the shortest, the longest, or some intermediate length depends
65       on the way the alternations and the greedy or ungreedy repetition quan‐
66       tifiers are specified in the pattern.
67
68       Because  it  ends  up  with a single path through the tree, it is rela‐
69       tively straightforward for this algorithm to keep  track  of  the  sub‐
70       strings  that  are  matched  by portions of the pattern in parentheses.
71       This provides support for capturing parentheses and backreferences.
72

THE ALTERNATIVE MATCHING ALGORITHM

74
75       This algorithm conducts a breadth-first search of  the  tree.  Starting
76       from  the  first  matching  point  in the subject, it scans the subject
77       string from left to right, once, character by character, and as it does
78       this,  it remembers all the paths through the tree that represent valid
79       matches. In Friedl's terminology, this is a kind  of  "DFA  algorithm",
80       though  it is not implemented as a traditional finite state machine (it
81       keeps multiple states active simultaneously).
82
83       Although the general principle of this matching algorithm  is  that  it
84       scans  the subject string only once, without backtracking, there is one
85       exception: when a lookaround assertion is encountered,  the  characters
86       following  or  preceding the current point have to be independently in‐
87       spected.
88
89       The scan continues until either the end of the subject is  reached,  or
90       there  are  no more unterminated paths. At this point, terminated paths
91       represent the different matching possibilities (if there are none,  the
92       match  has  failed).   Thus,  if there is more than one possible match,
93       this algorithm finds all of them, and in particular, it finds the long‐
94       est.  The matches are returned in the output vector in decreasing order
95       of length. There is an option to stop the  algorithm  after  the  first
96       match (which is necessarily the shortest) is found.
97
98       Note  that the size of vector needed to contain all the results depends
99       on the number of simultaneous matches, not on the number of parentheses
100       in  the pattern. Using pcre2_match_data_create_from_pattern() to create
101       the match data block is therefore not advisable when doing  DFA  match‐
102       ing.
103
104       Note  also  that all the matches that are found start at the same point
105       in the subject. If the pattern
106
107         cat(er(pillar)?)?
108
109       is matched against the string "the caterpillar catchment",  the  result
110       is  the  three  strings "caterpillar", "cater", and "cat" that start at
111       the fifth character of the subject. The algorithm  does  not  automati‐
112       cally move on to find matches that start at later positions.
113
114       PCRE2's "auto-possessification" optimization usually applies to charac‐
115       ter repeats at the end of a pattern (as well as internally). For  exam‐
116       ple, the pattern "a\d+" is compiled as if it were "a\d++" because there
117       is no point even considering the possibility of backtracking  into  the
118       repeated  digits.  For  DFA matching, this means that only one possible
119       match is found. If you really do want multiple matches in  such  cases,
120       either  use  an ungreedy repeat ("a\d+?") or set the PCRE2_NO_AUTO_POS‐
121       SESS option when compiling.
122
123       There are a number of features of PCRE2 regular  expressions  that  are
124       not  supported  or behave differently in the alternative matching func‐
125       tion. Those that are not supported cause an error if encountered.
126
127       1. Because the algorithm finds all possible matches, the greedy or  un‐
128       greedy  nature of repetition quantifiers is not relevant (though it may
129       affect auto-possessification,  as  just  described).  During  matching,
130       greedy  and  ungreedy  quantifiers are treated in exactly the same way.
131       However, possessive quantifiers can make a difference when what follows
132       could  also  match  what  is  quantified, for example in a pattern like
133       this:
134
135         ^a++\w!
136
137       This pattern matches "aaab!" but not "aaa!", which would be matched  by
138       a  non-possessive quantifier. Similarly, if an atomic group is present,
139       it is matched as if it were a standalone pattern at the current  point,
140       and  the  longest match is then "locked in" for the rest of the overall
141       pattern.
142
143       2. When dealing with multiple paths through the tree simultaneously, it
144       is  not  straightforward  to  keep track of captured substrings for the
145       different matching possibilities, and PCRE2's  implementation  of  this
146       algorithm does not attempt to do this. This means that no captured sub‐
147       strings are available.
148
149       3. Because no substrings are captured, backreferences within  the  pat‐
150       tern are not supported.
151
152       4.  For  the same reason, conditional expressions that use a backrefer‐
153       ence as the condition or test for a specific group  recursion  are  not
154       supported.
155
156       5. Again for the same reason, script runs are not supported.
157
158       6. Because many paths through the tree may be active, the \K escape se‐
159       quence, which resets the start of the match when encountered  (but  may
160       be on some paths and not on others), is not supported.
161
162       7.  Callouts  are  supported, but the value of the capture_top field is
163       always 1, and the value of the capture_last field is always 0.
164
165       8. The \C escape sequence, which (in  the  standard  algorithm)  always
166       matches  a  single  code  unit, even in a UTF mode, is not supported in
167       these modes, because the alternative algorithm moves through  the  sub‐
168       ject  string  one  character  (not code unit) at a time, for all active
169       paths through the tree.
170
171       9. Except for (*FAIL), the backtracking control verbs such as  (*PRUNE)
172       are  not  supported.  (*FAIL)  is supported, and behaves like a failing
173       negative assertion.
174
175       10. The PCRE2_MATCH_INVALID_UTF option for pcre2_compile() is not  sup‐
176       ported by pcre2_dfa_match().
177

ADVANTAGES OF THE ALTERNATIVE ALGORITHM

179
180       The  main  advantage  of the alternative algorithm is that all possible
181       matches (at a single point in the subject) are automatically found, and
182       in  particular, the longest match is found. To find more than one match
183       at the same point using the standard algorithm, you have to  do  kludgy
184       things with callouts.
185
186       Partial  matching  is  possible with this algorithm, though it has some
187       limitations. The pcre2partial documentation gives  details  of  partial
188       matching and discusses multi-segment matching.
189

DISADVANTAGES OF THE ALTERNATIVE ALGORITHM

191
192       The alternative algorithm suffers from a number of disadvantages:
193
194       1.  It  is  substantially  slower  than the standard algorithm. This is
195       partly because it has to search for all possible matches, but  is  also
196       because it is less susceptible to optimization.
197
198       2.  Capturing  parentheses,  backreferences,  script runs, and matching
199       within invalid UTF string are not supported.
200
201       3. Although atomic groups are supported, their use does not provide the
202       performance advantage that it does for the standard algorithm.
203
204       4. JIT optimization is not supported.
205

AUTHOR

207
208       Philip Hazel
209       Retired from University Computing Service
210       Cambridge, England.
211

REVISION

213
214       Last updated: 28 August 2021
215       Copyright (c) 1997-2021 University of Cambridge.
216
217
218
219PCRE2 10.38                     28 August 2021                PCRE2MATCHING(3)
Impressum