1 |
<html>
|
2 |
<head>
|
3 |
<title>pcreperform specification</title>
|
4 |
</head>
|
5 |
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
|
6 |
This HTML document has been generated automatically from the original man page.
|
7 |
If there is any nonsense in it, please consult the man page, in case the
|
8 |
conversion went wrong.<br>
|
9 |
<ul>
|
10 |
<li><a name="TOC1" href="#SEC1">PCRE PERFORMANCE</a>
|
11 |
</ul>
|
12 |
<br><a name="SEC1" href="#TOC1">PCRE PERFORMANCE</a><br>
|
13 |
<P>
|
14 |
Certain items that may appear in regular expression patterns are more efficient
|
15 |
than others. It is more efficient to use a character class like [aeiou] than a
|
16 |
set of alternatives such as (a|e|i|o|u). In general, the simplest construction
|
17 |
that provides the required behaviour is usually the most efficient. Jeffrey
|
18 |
Friedl's book contains a lot of discussion about optimizing regular expressions
|
19 |
for efficient performance.
|
20 |
</P>
|
21 |
<P>
|
22 |
When a pattern begins with .* not in parentheses, or in parentheses that are
|
23 |
not the subject of a backreference, and the PCRE_DOTALL option is set, the
|
24 |
pattern is implicitly anchored by PCRE, since it can match only at the start of
|
25 |
a subject string. However, if PCRE_DOTALL is not set, PCRE cannot make this
|
26 |
optimization, because the . metacharacter does not then match a newline, and if
|
27 |
the subject string contains newlines, the pattern may match from the character
|
28 |
immediately following one of them instead of from the very start. For example,
|
29 |
the pattern
|
30 |
</P>
|
31 |
<P>
|
32 |
<pre>
|
33 |
.*second
|
34 |
</PRE>
|
35 |
</P>
|
36 |
<P>
|
37 |
matches the subject "first\nand second" (where \n stands for a newline
|
38 |
character), with the match starting at the seventh character. In order to do
|
39 |
this, PCRE has to retry the match starting after every newline in the subject.
|
40 |
</P>
|
41 |
<P>
|
42 |
If you are using such a pattern with subject strings that do not contain
|
43 |
newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
|
44 |
the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
|
45 |
having to scan along the subject looking for a newline to restart at.
|
46 |
</P>
|
47 |
<P>
|
48 |
Beware of patterns that contain nested indefinite repeats. These can take a
|
49 |
long time to run when applied to a string that does not match. Consider the
|
50 |
pattern fragment
|
51 |
</P>
|
52 |
<P>
|
53 |
<pre>
|
54 |
(a+)*
|
55 |
</PRE>
|
56 |
</P>
|
57 |
<P>
|
58 |
This can match "aaaa" in 33 different ways, and this number increases very
|
59 |
rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
|
60 |
times, and for each of those cases other than 0, the + repeats can match
|
61 |
different numbers of times.) When the remainder of the pattern is such that the
|
62 |
entire match is going to fail, PCRE has in principle to try every possible
|
63 |
variation, and this can take an extremely long time.
|
64 |
</P>
|
65 |
<P>
|
66 |
An optimization catches some of the more simple cases such as
|
67 |
</P>
|
68 |
<P>
|
69 |
<pre>
|
70 |
(a+)*b
|
71 |
</PRE>
|
72 |
</P>
|
73 |
<P>
|
74 |
where a literal character follows. Before embarking on the standard matching
|
75 |
procedure, PCRE checks that there is a "b" later in the subject string, and if
|
76 |
there is not, it fails the match immediately. However, when there is no
|
77 |
following literal this optimization cannot be used. You can see the difference
|
78 |
by comparing the behaviour of
|
79 |
</P>
|
80 |
<P>
|
81 |
<pre>
|
82 |
(a+)*\d
|
83 |
</PRE>
|
84 |
</P>
|
85 |
<P>
|
86 |
with the pattern above. The former gives a failure almost instantly when
|
87 |
applied to a whole line of "a" characters, whereas the latter takes an
|
88 |
appreciable time with strings longer than about 20 characters.
|
89 |
</P>
|
90 |
<P>
|
91 |
Last updated: 03 February 2003
|
92 |
<br>
|
93 |
Copyright © 1997-2003 University of Cambridge.
|