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
21       algorithm, 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 greedy and ungreedy repetition quantifiers are specified
66       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
87       inspected.
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 decreasing order of length. There is
95       an option to stop the algorithm after the first match (which is  neces‐
96       sarily the shortest) is found.
97
98       Note that all the matches that are found start at the same point in the
99       subject. If the pattern
100
101         cat(er(pillar)?)?
102
103       is matched against the string "the caterpillar catchment",  the  result
104       is  the  three  strings "caterpillar", "cater", and "cat" that start at
105       the fifth character of the subject. The algorithm  does  not  automati‐
106       cally move on to find matches that start at later positions.
107
108       PCRE2's "auto-possessification" optimization usually applies to charac‐
109       ter repeats at the end of a pattern (as well as internally). For  exam‐
110       ple, the pattern "a\d+" is compiled as if it were "a\d++" because there
111       is no point even considering the possibility of backtracking  into  the
112       repeated  digits.  For  DFA matching, this means that only one possible
113       match is found. If you really do want multiple matches in  such  cases,
114       either  use  an ungreedy repeat ("a\d+?") or set the PCRE2_NO_AUTO_POS‐
115       SESS option when compiling.
116
117       There are a number of features of PCRE2 regular  expressions  that  are
118       not  supported  or behave differently in the alternative matching func‐
119       tion. Those that are not supported cause an error if encountered.
120
121       1. Because the algorithm finds all  possible  matches,  the  greedy  or
122       ungreedy  nature  of  repetition quantifiers is not relevant (though it
123       may affect auto-possessification, as just described). During  matching,
124       greedy  and  ungreedy  quantifiers are treated in exactly the same way.
125       However, possessive quantifiers can make a difference when what follows
126       could  also  match  what  is  quantified, for example in a pattern like
127       this:
128
129         ^a++\w!
130
131       This pattern matches "aaab!" but not "aaa!", which would be matched  by
132       a  non-possessive quantifier. Similarly, if an atomic group is present,
133       it is matched as if it were a standalone pattern at the current  point,
134       and  the  longest match is then "locked in" for the rest of the overall
135       pattern.
136
137       2. When dealing with multiple paths through the tree simultaneously, it
138       is  not  straightforward  to  keep track of captured substrings for the
139       different matching possibilities, and PCRE2's  implementation  of  this
140       algorithm does not attempt to do this. This means that no captured sub‐
141       strings are available.
142
143       3. Because no substrings are captured, backreferences within  the  pat‐
144       tern are not supported.
145
146       4.  For  the same reason, conditional expressions that use a backrefer‐
147       ence as the condition or test for a specific group  recursion  are  not
148       supported.
149
150       5. Again for the same reason, script runs are not supported.
151
152       6.  Because  many  paths  through the tree may be active, the \K escape
153       sequence, which resets the start of the match when encountered (but may
154       be on some paths and not on others), is not supported.
155
156       7.  Callouts  are  supported, but the value of the capture_top field is
157       always 1, and the value of the capture_last field is always 0.
158
159       8. The \C escape sequence, which (in  the  standard  algorithm)  always
160       matches  a  single  code  unit, even in a UTF mode, is not supported in
161       these modes, because the alternative algorithm moves through  the  sub‐
162       ject  string  one  character  (not code unit) at a time, for all active
163       paths through the tree.
164
165       9. Except for (*FAIL), the backtracking control verbs such as  (*PRUNE)
166       are  not  supported.  (*FAIL)  is supported, and behaves like a failing
167       negative assertion.
168

ADVANTAGES OF THE ALTERNATIVE ALGORITHM

170
171       Using the alternative matching algorithm provides the following  advan‐
172       tages:
173
174       1. All possible matches (at a single point in the subject) are automat‐
175       ically found, and in particular, the longest match is  found.  To  find
176       more than one match using the standard algorithm, you have to do kludgy
177       things with callouts.
178
179       2. Because the alternative algorithm  scans  the  subject  string  just
180       once, and never needs to backtrack (except for lookbehinds), it is pos‐
181       sible to pass very long subject strings to  the  matching  function  in
182       several pieces, checking for partial matching each time. Although it is
183       also possible to do multi-segment matching  using  the  standard  algo‐
184       rithm,  by  retaining  partially matched substrings, it is more compli‐
185       cated. The pcre2partial documentation gives details of partial matching
186       and discusses multi-segment matching.
187

DISADVANTAGES OF THE ALTERNATIVE ALGORITHM

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

AUTHOR

203
204       Philip Hazel
205       University Computing Service
206       Cambridge, England.
207

REVISION

209
210       Last updated: 10 October 2018
211       Copyright (c) 1997-2018 University of Cambridge.
212
213
214
215PCRE2 10.33                     10 October 2018               PCRE2MATCHING(3)
Impressum