| 1 |
.TH PCRE 3 |
.TH PCRE 3 |
| 2 |
.SH NAME |
.SH NAME |
| 3 |
PCRE - Perl-compatible regular expressions |
PCRE - Perl-compatible regular expressions |
| 4 |
.SH PCRE PERFORMANCE |
.SH "PCRE PERFORMANCE" |
| 5 |
.rs |
.rs |
| 6 |
.sp |
.sp |
| 7 |
Certain items that may appear in regular expression patterns are more efficient |
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 |
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 |
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 |
that provides the required behaviour is usually the most efficient. Jeffrey |
| 11 |
Friedl's book contains a lot of discussion about optimizing regular expressions |
Friedl's book contains a lot of useful general discussion about optimizing |
| 12 |
for efficient performance. |
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 |
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 |
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 |
pattern is implicitly anchored by PCRE, since it can match only at the start of |
| 26 |
the subject string contains newlines, the pattern may match from the character |
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, |
immediately following one of them instead of from the very start. For example, |
| 28 |
the pattern |
the pattern |
| 29 |
|
.sp |
| 30 |
.*second |
.*second |
| 31 |
|
.sp |
| 32 |
matches the subject "first\\nand second" (where \\n stands for a newline |
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 |
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. |
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 |
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 |
newlines, the best performance is obtained by setting PCRE_DOTALL, or starting |
| 38 |
the pattern with ^.* to indicate explicit anchoring. That saves PCRE from |
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. |
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 |
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 |
long time to run when applied to a string that does not match. Consider the |
| 43 |
pattern fragment |
pattern fragment |
| 44 |
|
.sp |
| 45 |
(a+)* |
(a+)* |
| 46 |
|
.sp |
| 47 |
This can match "aaaa" in 33 different ways, and this number increases very |
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 |
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 |
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 |
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 |
entire match is going to fail, PCRE has in principle to try every possible |
| 52 |
variation, and this can take an extremely long time. |
variation, and this can take an extremely long time. |
| 53 |
|
.P |
| 54 |
An optimization catches some of the more simple cases such as |
An optimization catches some of the more simple cases such as |
| 55 |
|
.sp |
| 56 |
(a+)*b |
(a+)*b |
| 57 |
|
.sp |
| 58 |
where a literal character follows. Before embarking on the standard matching |
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 |
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 |
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 |
following literal this optimization cannot be used. You can see the difference |
| 62 |
by comparing the behaviour of |
by comparing the behaviour of |
| 63 |
|
.sp |
| 64 |
(a+)*\\d |
(a+)*\ed |
| 65 |
|
.sp |
| 66 |
with the pattern above. The former gives a failure almost instantly when |
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 |
applied to a whole line of "a" characters, whereas the latter takes an |
| 68 |
appreciable time with strings longer than about 20 characters. |
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 |
.in 0 |
| 74 |
Last updated: 03 February 2003 |
Last updated: 09 September 2004 |
| 75 |
.br |
.br |
| 76 |
Copyright (c) 1997-2003 University of Cambridge. |
Copyright (c) 1997-2004 University of Cambridge. |