chiark / gitweb /
pcre3 (2:8.35-7.4) unstable; urgency=medium
[pcre3.git] / doc / pcrecompat.3
1 .TH PCRECOMPAT 3 "10 November 2013" "PCRE 8.34"
2 .SH NAME
3 PCRE - Perl-compatible regular expressions
4 .SH "DIFFERENCES BETWEEN PCRE AND PERL"
5 .rs
6 .sp
7 This document describes the differences in the ways that PCRE and Perl handle
8 regular expressions. The differences described here are with respect to Perl
9 versions 5.10 and above.
10 .P
11 1. PCRE has only a subset of Perl's Unicode support. Details of what it does
12 have are given in the
13 .\" HREF
14 \fBpcreunicode\fP
15 .\"
16 page.
17 .P
18 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 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 these do not seem to have any use.
24 .P
25 3. Capturing subpatterns that occur inside negative lookahead assertions are
26 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 .P
29 4. Though binary zero characters are supported in the subject string, they are
30 not allowed in a pattern string because it is passed as a normal C string,
31 terminated by zero. The escape sequence \e0 can be used in the pattern to
32 represent a binary zero.
33 .P
34 5. The following Perl escape sequences are not supported: \el, \eu, \eL,
35 \eU, and \eN when followed by a character name or Unicode value. (\eN on its
36 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 generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
40 \eU and \eu are interpreted as JavaScript interprets them.
41 .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 Lu and Nd, script names such as Greek or Han, and the derived properties Any
46 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 .P
51 7. PCRE does support the \eQ...\eE escape for quoting substrings. Characters in
52 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 .sp
57     Pattern            PCRE matches      Perl matches
58 .sp
59 .\" JOIN
60     \eQabc$xyz\eE        abc$xyz           abc followed by the
61                                            contents of $xyz
62     \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 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
68 constructions. However, there is support for recursive patterns. This is not
69 available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout"
70 feature allows an external function to be called during pattern matching. See
71 the
72 .\" HREF
73 \fBpcrecallout\fP
74 .\"
75 documentation for details.
76 .P
77 9. Subpatterns that are called as subroutines (whether or not recursively) are
78 always treated as atomic groups in PCRE. This is like Python, but unlike Perl.
79 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 .\" HTML <a href="pcrepattern.html#recursiondifference">
83 .\" </a>
84 section on recursion differences from Perl
85 .\"
86 in the
87 .\" HREF
88 \fBpcrepattern\fP
89 .\"
90 page.
91 .P
92 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 .P
100 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 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 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 .P
113 14. PCRE's handling of duplicate subpattern numbers and duplicate subpattern
114 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 .P
123 15. Perl recognizes comments in some places that PCRE does not, for example,
124 between the ( and ? at the start of a subpattern. If the /x modifier is set,
125 Perl allows white space between ( and ? (though current Perls warn that this is
126 deprecated) but PCRE never does, even if the PCRE_EXTENDED option is set.
127 .P
128 16. Perl, when in warning mode, gives warnings for character classes such as
129 [A-\ed] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE has no
130 warning features, so it gives an error in these cases because they are almost
131 certainly user mistakes.
132 .P
133 17. In PCRE, the upper/lower case character properties Lu and Ll are not
134 affected when case-independent matching is specified. For example, \ep{Lu}
135 always matches an upper case letter. I think Perl has changed in this respect;
136 in the release at the time of writing (5.16), \ep{Lu} and \ep{Ll} match all
137 letters, regardless of case, when case independence is specified.
138 .P
139 18. PCRE provides some extensions to the Perl regular expression facilities.
140 Perl 5.10 includes new features that are not in earlier versions of Perl, some
141 of which (such as named parentheses) have been in PCRE for some time. This list
142 is with respect to Perl 5.10:
143 .sp
144 (a) Although lookbehind assertions in PCRE must match fixed length strings,
145 each alternative branch of a lookbehind assertion can match a different length
146 of string. Perl requires them all to have the same length.
147 .sp
148 (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
149 meta-character matches only at the very end of the string.
150 .sp
151 (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
152 meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored.
153 (Perl can be made to issue a warning.)
154 .sp
155 (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
156 inverted, that is, by default they are not greedy, but if followed by a
157 question mark they are.
158 .sp
159 (e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried
160 only at the first matching position in the subject string.
161 .sp
162 (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, and
163 PCRE_NO_AUTO_CAPTURE options for \fBpcre_exec()\fP have no Perl equivalents.
164 .sp
165 (g) The \eR escape sequence can be restricted to match only CR, LF, or CRLF
166 by the PCRE_BSR_ANYCRLF option.
167 .sp
168 (h) The callout facility is PCRE-specific.
169 .sp
170 (i) The partial matching facility is PCRE-specific.
171 .sp
172 (j) Patterns compiled by PCRE can be saved and re-used at a later time, even on
173 different hosts that have the other endianness. However, this does not apply to
174 optimized data created by the just-in-time compiler.
175 .sp
176 (k) The alternative matching functions (\fBpcre_dfa_exec()\fP,
177 \fBpcre16_dfa_exec()\fP and \fBpcre32_dfa_exec()\fP,) match in a different way
178 and are not Perl-compatible.
179 .sp
180 (l) PCRE recognizes some special sequences such as (*CR) at the start of
181 a pattern that set overall options that cannot be changed within the pattern.
182 .
183 .
184 .SH AUTHOR
185 .rs
186 .sp
187 .nf
188 Philip Hazel
189 University Computing Service
190 Cambridge CB2 3QH, England.
191 .fi
192 .
193 .
194 .SH REVISION
195 .rs
196 .sp
197 .nf
198 Last updated: 10 November 2013
199 Copyright (c) 1997-2013 University of Cambridge.
200 .fi