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

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE MATCHING ALGORITHMS

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

REGULAR EXPRESSIONS AS TREES

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

THE STANDARD MATCHING ALGORITHM

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

THE ALTERNATIVE MATCHING ALGORITHM

72
73       This algorithm conducts a breadth-first search of  the  tree.  Starting
74       from  the  first  matching  point  in the subject, it scans the subject
75       string from left to right, once, character by character, and as it does
76       this,  it remembers all the paths through the tree that represent valid
77       matches. In Friedl's terminology, this is a kind  of  "DFA  algorithm",
78       though  it is not implemented as a traditional finite state machine (it
79       keeps multiple states active simultaneously).
80
81       The scan continues until either the end of the subject is  reached,  or
82       there  are  no more unterminated paths. At this point, terminated paths
83       represent the different matching possibilities (if there are none,  the
84       match  has  failed).   Thus,  if there is more than one possible match,
85       this algorithm finds all of them, and in particular, it finds the long‐
86       est.  In PCRE, there is an option to stop the algorithm after the first
87       match (which is necessarily the shortest) has been found.
88
89       Note that all the matches that are found start at the same point in the
90       subject. If the pattern
91
92         cat(er(pillar)?)
93
94       is  matched  against the string "the caterpillar catchment", the result
95       will be the three strings "cat", "cater", and "caterpillar" that  start
96       at the fourth character of the subject. The algorithm does not automat‐
97       ically move on to find matches that start at later positions.
98
99       There are a number of features of PCRE regular expressions that are not
100       supported by the alternative matching algorithm. They are as follows:
101
102       1.  Because  the  algorithm  finds  all possible matches, the greedy or
103       ungreedy nature of repetition quantifiers is not relevant.  Greedy  and
104       ungreedy quantifiers are treated in exactly the same way. However, pos‐
105       sessive quantifiers can make a difference when what follows could  also
106       match what is quantified, for example in a pattern like this:
107
108         ^a++\w!
109
110       This  pattern matches "aaab!" but not "aaa!", which would be matched by
111       a non-possessive quantifier. Similarly, if an atomic group is  present,
112       it  is matched as if it were a standalone pattern at the current point,
113       and the longest match is then "locked in" for the rest of  the  overall
114       pattern.
115
116       2. When dealing with multiple paths through the tree simultaneously, it
117       is not straightforward to keep track of  captured  substrings  for  the
118       different  matching  possibilities,  and  PCRE's implementation of this
119       algorithm does not attempt to do this. This means that no captured sub‐
120       strings are available.
121
122       3.  Because no substrings are captured, back references within the pat‐
123       tern are not supported, and cause errors if encountered.
124
125       4. For the same reason, conditional expressions that use  a  backrefer‐
126       ence  as  the  condition or test for a specific group recursion are not
127       supported.
128
129       5. Because many paths through the tree may be  active,  the  \K  escape
130       sequence, which resets the start of the match when encountered (but may
131       be on some paths and not on others), is not  supported.  It  causes  an
132       error if encountered.
133
134       6.  Callouts  are  supported, but the value of the capture_top field is
135       always 1, and the value of the capture_last field is always -1.
136
137       7. The \C escape sequence, which (in the standard algorithm) matches  a
138       single  byte, even in UTF-8 mode, is not supported because the alterna‐
139       tive algorithm moves through the subject  string  one  character  at  a
140       time, for all active paths through the tree.
141
142       8.  Except for (*FAIL), the backtracking control verbs such as (*PRUNE)
143       are not supported. (*FAIL) is supported, and  behaves  like  a  failing
144       negative assertion.
145

ADVANTAGES OF THE ALTERNATIVE ALGORITHM

147
148       Using  the alternative matching algorithm provides the following advan‐
149       tages:
150
151       1. All possible matches (at a single point in the subject) are automat‐
152       ically  found,  and  in particular, the longest match is found. To find
153       more than one match using the standard algorithm, you have to do kludgy
154       things with callouts.
155
156       2.  There is much better support for partial matching. The restrictions
157       on the content of the pattern that apply when using the standard  algo‐
158       rithm  for  partial matching do not apply to the alternative algorithm.
159       For non-anchored patterns, the starting position of a partial match  is
160       available.
161
162       3.  Because  the  alternative  algorithm  scans the subject string just
163       once, and never needs to backtrack, it is possible to  pass  very  long
164       subject  strings  to  the matching function in several pieces, checking
165       for partial matching each time.
166

DISADVANTAGES OF THE ALTERNATIVE ALGORITHM

168
169       The alternative algorithm suffers from a number of disadvantages:
170
171       1. It is substantially slower than  the  standard  algorithm.  This  is
172       partly  because  it has to search for all possible matches, but is also
173       because it is less susceptible to optimization.
174
175       2. Capturing parentheses and back references are not supported.
176
177       3. Although atomic groups are supported, their use does not provide the
178       performance advantage that it does for the standard algorithm.
179

AUTHOR

181
182       Philip Hazel
183       University Computing Service
184       Cambridge CB2 3QH, England.
185

REVISION

187
188       Last updated: 19 April 2008
189       Copyright (c) 1997-2008 University of Cambridge.
190
191
192
193                                                               PCREMATCHING(3)
Impressum