| 1 |
ph10 |
1297 |
.TH PCRECOMPAT 3 "19 March 2013" "PCRE 8.33" |
| 2 |
nigel |
63 |
.SH NAME |
| 3 |
|
|
PCRE - Perl-compatible regular expressions |
| 4 |
nigel |
75 |
.SH "DIFFERENCES BETWEEN PCRE AND PERL" |
| 5 |
nigel |
63 |
.rs |
| 6 |
|
|
.sp |
| 7 |
|
|
This document describes the differences in the ways that PCRE and Perl handle |
| 8 |
ph10 |
456 |
regular expressions. The differences described here are with respect to Perl |
| 9 |
ph10 |
562 |
versions 5.10 and above. |
| 10 |
nigel |
75 |
.P |
| 11 |
ph10 |
858 |
1. PCRE has only a subset of Perl's Unicode support. Details of what it does |
| 12 |
|
|
have are given in the |
| 13 |
nigel |
73 |
.\" HREF |
| 14 |
ph10 |
678 |
\fBpcreunicode\fP |
| 15 |
nigel |
73 |
.\" |
| 16 |
|
|
page. |
| 17 |
nigel |
75 |
.P |
| 18 |
ph10 |
637 |
2. PCRE allows repeat quantifiers only on parenthesized assertions, but they do |
| 19 |
|
|
not mean what you might think. For example, (?!a){3} does not assert that the |
| 20 |
|
|
next three characters are not "a". It just asserts that the next character is |
| 21 |
ph10 |
654 |
not "a" three times (in principle: PCRE optimizes this to run the assertion |
| 22 |
|
|
just once). Perl allows repeat quantifiers on other assertions such as \eb, but |
| 23 |
ph10 |
637 |
these do not seem to have any use. |
| 24 |
nigel |
75 |
.P |
| 25 |
nigel |
73 |
3. Capturing subpatterns that occur inside negative lookahead assertions are |
| 26 |
ph10 |
1292 |
counted, but their entries in the offsets vector are never set. Perl sometimes |
| 27 |
|
|
(but not always) sets its numerical variables from inside negative assertions. |
| 28 |
nigel |
75 |
.P |
| 29 |
nigel |
73 |
4. Though binary zero characters are supported in the subject string, they are |
| 30 |
nigel |
63 |
not allowed in a pattern string because it is passed as a normal C string, |
| 31 |
nigel |
75 |
terminated by zero. The escape sequence \e0 can be used in the pattern to |
| 32 |
nigel |
63 |
represent a binary zero. |
| 33 |
nigel |
75 |
.P |
| 34 |
|
|
5. The following Perl escape sequences are not supported: \el, \eu, \eL, |
| 35 |
ph10 |
654 |
\eU, and \eN when followed by a character name or Unicode value. (\eN on its |
| 36 |
ph10 |
634 |
own, matching a non-newline character, is supported.) In fact these are |
| 37 |
|
|
implemented by Perl's general string-handling and are not part of its pattern |
| 38 |
|
|
matching engine. If any of these are encountered by PCRE, an error is |
| 39 |
ph10 |
836 |
generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set, |
| 40 |
ph10 |
745 |
\eU and \eu are interpreted as JavaScript interprets them. |
| 41 |
nigel |
75 |
.P |
| 42 |
|
|
6. The Perl escape sequences \ep, \eP, and \eX are supported only if PCRE is |
| 43 |
|
|
built with Unicode character property support. The properties that can be |
| 44 |
|
|
tested with \ep and \eP are limited to the general category properties such as |
| 45 |
nigel |
87 |
Lu and Nd, script names such as Greek or Han, and the derived properties Any |
| 46 |
ph10 |
450 |
and L&. PCRE does support the Cs (surrogate) property, which Perl does not; the |
| 47 |
|
|
Perl documentation says "Because Perl hides the need for the user to understand |
| 48 |
|
|
the internal representation of Unicode characters, there is no need to |
| 49 |
|
|
implement the somewhat messy concept of surrogates." |
| 50 |
nigel |
75 |
.P |
| 51 |
ph10 |
1011 |
7. PCRE does support the \eQ...\eE escape for quoting substrings. Characters in |
| 52 |
nigel |
63 |
between are treated as literals. This is slightly different from Perl in that $ |
| 53 |
|
|
and @ are also handled as literals inside the quotes. In Perl, they cause |
| 54 |
|
|
variable interpolation (but of course PCRE does not have variables). Note the |
| 55 |
|
|
following examples: |
| 56 |
nigel |
75 |
.sp |
| 57 |
nigel |
63 |
Pattern PCRE matches Perl matches |
| 58 |
nigel |
75 |
.sp |
| 59 |
|
|
.\" JOIN |
| 60 |
|
|
\eQabc$xyz\eE abc$xyz abc followed by the |
| 61 |
nigel |
63 |
contents of $xyz |
| 62 |
nigel |
75 |
\eQabc\e$xyz\eE abc\e$xyz abc\e$xyz |
| 63 |
|
|
\eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz |
| 64 |
|
|
.sp |
| 65 |
|
|
The \eQ...\eE sequence is recognized both inside and outside character classes. |
| 66 |
|
|
.P |
| 67 |
ph10 |
1011 |
8. Fairly obviously, PCRE does not support the (?{code}) and (??{code}) |
| 68 |
nigel |
93 |
constructions. However, there is support for recursive patterns. This is not |
| 69 |
ph10 |
453 |
available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout" |
| 70 |
nigel |
93 |
feature allows an external function to be called during pattern matching. See |
| 71 |
|
|
the |
| 72 |
nigel |
75 |
.\" HREF |
| 73 |
|
|
\fBpcrecallout\fP |
| 74 |
|
|
.\" |
| 75 |
|
|
documentation for details. |
| 76 |
|
|
.P |
| 77 |
ph10 |
1011 |
9. Subpatterns that are called as subroutines (whether or not recursively) are |
| 78 |
ph10 |
716 |
always treated as atomic groups in PCRE. This is like Python, but unlike Perl. |
| 79 |
ph10 |
724 |
Captured values that are set outside a subroutine call can be reference from |
| 80 |
|
|
inside in PCRE, but not in Perl. There is a discussion that explains these |
| 81 |
|
|
differences in more detail in the |
| 82 |
ph10 |
453 |
.\" HTML <a href="pcrepattern.html#recursiondifference"> |
| 83 |
|
|
.\" </a> |
| 84 |
|
|
section on recursion differences from Perl |
| 85 |
|
|
.\" |
| 86 |
|
|
in the |
| 87 |
|
|
.\" HREF |
| 88 |
ph10 |
456 |
\fBpcrepattern\fP |
| 89 |
ph10 |
453 |
.\" |
| 90 |
|
|
page. |
| 91 |
nigel |
93 |
.P |
| 92 |
ph10 |
1297 |
10. If any of the backtracking control verbs are used in a subpattern that is |
| 93 |
|
|
called as a subroutine (whether or not recursively), their effect is confined |
| 94 |
|
|
to that subpattern; it does not extend to the surrounding pattern. This is not |
| 95 |
|
|
always the case in Perl. In particular, if (*THEN) is present in a group that |
| 96 |
|
|
is called as a subroutine, its action is limited to that group, even if the |
| 97 |
|
|
group does not contain any | characters. Note that such subpatterns are |
| 98 |
|
|
processed as anchored at the point where they are tested. |
| 99 |
ph10 |
716 |
.P |
| 100 |
ph10 |
1293 |
11. If a pattern contains more than one backtracking control verb, the first |
| 101 |
|
|
one that is backtracked onto acts. For example, in the pattern |
| 102 |
|
|
A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C |
| 103 |
|
|
triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the |
| 104 |
|
|
same as PCRE, but there are examples where it differs. |
| 105 |
|
|
.P |
| 106 |
ph10 |
1297 |
12. Most backtracking verbs in assertions have their normal actions. They are |
| 107 |
|
|
not confined to the assertion. |
| 108 |
|
|
.P |
| 109 |
|
|
13. There are some differences that are concerned with the settings of captured |
| 110 |
nigel |
63 |
strings when part of a pattern is repeated. For example, matching "aba" against |
| 111 |
|
|
the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b". |
| 112 |
nigel |
75 |
.P |
| 113 |
ph10 |
1297 |
14. PCRE's handling of duplicate subpattern numbers and duplicate subpattern |
| 114 |
ph10 |
459 |
names is not as general as Perl's. This is a consequence of the fact the PCRE |
| 115 |
|
|
works internally just with numbers, using an external table to translate |
| 116 |
|
|
between numbers and names. In particular, a pattern such as (?|(?<a>A)|(?<b)B), |
| 117 |
|
|
where the two capturing parentheses have the same number but different names, |
| 118 |
|
|
is not supported, and causes an error at compile time. If it were allowed, it |
| 119 |
|
|
would not be possible to distinguish which parentheses matched, because both |
| 120 |
|
|
names map to capturing subpattern number 1. To avoid this confusing situation, |
| 121 |
|
|
an error is given at compile time. |
| 122 |
ph10 |
456 |
.P |
| 123 |
ph10 |
1297 |
15. Perl recognizes comments in some places that PCRE does not, for example, |
| 124 |
ph10 |
654 |
between the ( and ? at the start of a subpattern. If the /x modifier is set, |
| 125 |
ph10 |
968 |
Perl allows white space between ( and ? but PCRE never does, even if the |
| 126 |
ph10 |
635 |
PCRE_EXTENDED option is set. |
| 127 |
ph10 |
562 |
.P |
| 128 |
ph10 |
1297 |
16. In PCRE, the upper/lower case character properties Lu and Ll are not |
| 129 |
ph10 |
1258 |
affected when case-independent matching is specified. For example, \ep{Lu} |
| 130 |
|
|
always matches an upper case letter. I think Perl has changed in this respect; |
| 131 |
|
|
in the release at the time of writing (5.16), \ep{Lu} and \ep{Ll} match all |
| 132 |
|
|
letters, regardless of case, when case independence is specified. |
| 133 |
|
|
.P |
| 134 |
ph10 |
1297 |
17. PCRE provides some extensions to the Perl regular expression facilities. |
| 135 |
ph10 |
456 |
Perl 5.10 includes new features that are not in earlier versions of Perl, some |
| 136 |
|
|
of which (such as named parentheses) have been in PCRE for some time. This list |
| 137 |
|
|
is with respect to Perl 5.10: |
| 138 |
|
|
.sp |
| 139 |
|
|
(a) Although lookbehind assertions in PCRE must match fixed length strings, |
| 140 |
|
|
each alternative branch of a lookbehind assertion can match a different length |
| 141 |
|
|
of string. Perl requires them all to have the same length. |
| 142 |
|
|
.sp |
| 143 |
nigel |
63 |
(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ |
| 144 |
|
|
meta-character matches only at the very end of the string. |
| 145 |
nigel |
75 |
.sp |
| 146 |
nigel |
63 |
(c) If PCRE_EXTRA is set, a backslash followed by a letter with no special |
| 147 |
ph10 |
181 |
meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored. |
| 148 |
|
|
(Perl can be made to issue a warning.) |
| 149 |
nigel |
75 |
.sp |
| 150 |
nigel |
63 |
(d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is |
| 151 |
|
|
inverted, that is, by default they are not greedy, but if followed by a |
| 152 |
|
|
question mark they are. |
| 153 |
nigel |
75 |
.sp |
| 154 |
|
|
(e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried |
| 155 |
|
|
only at the first matching position in the subject string. |
| 156 |
|
|
.sp |
| 157 |
ph10 |
442 |
(f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, and |
| 158 |
|
|
PCRE_NO_AUTO_CAPTURE options for \fBpcre_exec()\fP have no Perl equivalents. |
| 159 |
nigel |
75 |
.sp |
| 160 |
ph10 |
231 |
(g) The \eR escape sequence can be restricted to match only CR, LF, or CRLF |
| 161 |
|
|
by the PCRE_BSR_ANYCRLF option. |
| 162 |
nigel |
75 |
.sp |
| 163 |
ph10 |
231 |
(h) The callout facility is PCRE-specific. |
| 164 |
nigel |
75 |
.sp |
| 165 |
ph10 |
231 |
(i) The partial matching facility is PCRE-specific. |
| 166 |
|
|
.sp |
| 167 |
|
|
(j) Patterns compiled by PCRE can be saved and re-used at a later time, even on |
| 168 |
ph10 |
733 |
different hosts that have the other endianness. However, this does not apply to |
| 169 |
ph10 |
716 |
optimized data created by the just-in-time compiler. |
| 170 |
nigel |
77 |
.sp |
| 171 |
chpe |
1055 |
(k) The alternative matching functions (\fBpcre_dfa_exec()\fP, |
| 172 |
|
|
\fBpcre16_dfa_exec()\fP and \fBpcre32_dfa_exec()\fP,) match in a different way |
| 173 |
|
|
and are not Perl-compatible. |
| 174 |
ph10 |
231 |
.sp |
| 175 |
|
|
(l) PCRE recognizes some special sequences such as (*CR) at the start of |
| 176 |
|
|
a pattern that set overall options that cannot be changed within the pattern. |
| 177 |
ph10 |
99 |
. |
| 178 |
|
|
. |
| 179 |
|
|
.SH AUTHOR |
| 180 |
|
|
.rs |
| 181 |
|
|
.sp |
| 182 |
|
|
.nf |
| 183 |
|
|
Philip Hazel |
| 184 |
|
|
University Computing Service |
| 185 |
|
|
Cambridge CB2 3QH, England. |
| 186 |
|
|
.fi |
| 187 |
|
|
. |
| 188 |
|
|
. |
| 189 |
|
|
.SH REVISION |
| 190 |
|
|
.rs |
| 191 |
|
|
.sp |
| 192 |
|
|
.nf |
| 193 |
ph10 |
1297 |
Last updated: 19 March 2013 |
| 194 |
ph10 |
1258 |
Copyright (c) 1997-2013 University of Cambridge. |
| 195 |
ph10 |
99 |
.fi |