ViewVC logotype

Contents of /code/tags/pcre-5.0/doc/pcreperform.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 76 - (show annotations)
Sat Feb 24 21:40:39 2007 UTC (14 years, 8 months ago) by nigel
File size: 3491 byte(s)
Tag code/trunk as code/tags/pcre-5.0.
1 .TH PCRE 3
3 PCRE - Perl-compatible regular expressions
5 .rs
6 .sp
7 Certain items that may appear in regular expression patterns are more efficient
8 than others. It is more efficient to use a character class like [aeiou] than a
9 set of alternatives such as (a|e|i|o|u). In general, the simplest construction
10 that provides the required behaviour is usually the most efficient. Jeffrey
11 Friedl's book contains a lot of useful general discussion about optimizing
12 regular expressions for efficient performance. This document contains a few
13 observations about PCRE.
14 .P
15 Using Unicode character properties (the \ep, \eP, and \eX escapes) is slow,
16 because PCRE has to scan a structure that contains data for over fifteen
17 thousand characters whenever it needs a character's property. If you can find
18 an alternative pattern that does not use character properties, it will probably
19 be faster.
20 .P
21 When a pattern begins with .* not in parentheses, or in parentheses that are
22 not the subject of a backreference, and the PCRE_DOTALL option is set, the
23 pattern is implicitly anchored by PCRE, since it can match only at the start of
24 a subject string. However, if PCRE_DOTALL is not set, PCRE cannot make this
25 optimization, because the . metacharacter does not then match a newline, and if
26 the subject string contains newlines, the pattern may match from the character
27 immediately following one of them instead of from the very start. For example,
28 the pattern
29 .sp
30 .*second
31 .sp
32 matches the subject "first\enand second" (where \en stands for a newline
33 character), with the match starting at the seventh character. In order to do
34 this, PCRE has to retry the match starting after every newline in the subject.
35 .P
36 If you are using such a pattern with subject strings that do not contain
37 newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
38 the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
39 having to scan along the subject looking for a newline to restart at.
40 .P
41 Beware of patterns that contain nested indefinite repeats. These can take a
42 long time to run when applied to a string that does not match. Consider the
43 pattern fragment
44 .sp
45 (a+)*
46 .sp
47 This can match "aaaa" in 33 different ways, and this number increases very
48 rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
49 times, and for each of those cases other than 0, the + repeats can match
50 different numbers of times.) When the remainder of the pattern is such that the
51 entire match is going to fail, PCRE has in principle to try every possible
52 variation, and this can take an extremely long time.
53 .P
54 An optimization catches some of the more simple cases such as
55 .sp
56 (a+)*b
57 .sp
58 where a literal character follows. Before embarking on the standard matching
59 procedure, PCRE checks that there is a "b" later in the subject string, and if
60 there is not, it fails the match immediately. However, when there is no
61 following literal this optimization cannot be used. You can see the difference
62 by comparing the behaviour of
63 .sp
64 (a+)*\ed
65 .sp
66 with the pattern above. The former gives a failure almost instantly when
67 applied to a whole line of "a" characters, whereas the latter takes an
68 appreciable time with strings longer than about 20 characters.
69 .P
70 In many cases, the solution to this kind of performance issue is to use an
71 atomic group or a possessive quantifier.
72 .P
73 .in 0
74 Last updated: 09 September 2004
75 .br
76 Copyright (c) 1997-2004 University of Cambridge.

  ViewVC Help
Powered by ViewVC 1.1.5