/[pcre]/code/trunk/doc/html/pcreperform.html
ViewVC logotype

Contents of /code/trunk/doc/html/pcreperform.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (show annotations) (download) (as text)
Sat Feb 24 21:40:45 2007 UTC (7 years, 6 months ago) by nigel
File MIME type: text/html
File size: 4020 byte(s)
Load pcre-6.0 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12