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

ADVANTAGES OF THE ALTERNATIVE ALGORITHM

153
154       Using  the alternative matching algorithm provides the following advan‐
155       tages:
156
157       1. All possible matches (at a single point in the subject) are automat‐
158       ically  found,  and  in particular, the longest match is found. To find
159       more than one match using the standard algorithm, you have to do kludgy
160       things with callouts.
161
162       2.  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.  The  pcrepartial  documentation  gives
166       details of partial matching.
167

DISADVANTAGES OF THE ALTERNATIVE ALGORITHM

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

AUTHOR

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

REVISION

188
189       Last updated: 29 September 2009
190       Copyright (c) 1997-2009 University of Cambridge.
191
192
193
194                                                               PCREMATCHING(3)
Impressum