1<html>
2<head>
3<title>pcrecompat specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcrecompat man page</h1>
7<p>
8Return to the <a href="index.html">PCRE index page</a>.
9</p>
10<p>
11This page is part of the PCRE HTML documentation. It was generated automatically
12from the original man page. If there is any nonsense in it, please consult the
13man page, in case the conversion went wrong.
14<br>
15<br><b>
16DIFFERENCES BETWEEN PCRE AND PERL
17</b><br>
18<P>
19This document describes the differences in the ways that PCRE and Perl handle
20regular expressions. The differences described here are with respect to Perl
21versions 5.10 and above.
22</P>
23<P>
241. PCRE has only a subset of Perl's Unicode support. Details of what it does
25have are given in the
26<a href="pcreunicode.html"><b>pcreunicode</b></a>
27page.
28</P>
29<P>
302. PCRE allows repeat quantifiers only on parenthesized assertions, but they do
31not mean what you might think. For example, (?!a){3} does not assert that the
32next three characters are not "a". It just asserts that the next character is
33not "a" three times (in principle: PCRE optimizes this to run the assertion
34just once). Perl allows repeat quantifiers on other assertions such as \b, but
35these do not seem to have any use.
36</P>
37<P>
383. Capturing subpatterns that occur inside negative lookahead assertions are
39counted, but their entries in the offsets vector are never set. Perl sometimes
40(but not always) sets its numerical variables from inside negative assertions.
41</P>
42<P>
434. Though binary zero characters are supported in the subject string, they are
44not allowed in a pattern string because it is passed as a normal C string,
45terminated by zero. The escape sequence \0 can be used in the pattern to
46represent a binary zero.
47</P>
48<P>
495. The following Perl escape sequences are not supported: \l, \u, \L,
50\U, and \N when followed by a character name or Unicode value. (\N on its
51own, matching a non-newline character, is supported.) In fact these are
52implemented by Perl's general string-handling and are not part of its pattern
53matching engine. If any of these are encountered by PCRE, an error is
54generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
55\U and \u are interpreted as JavaScript interprets them.
56</P>
57<P>
586. The Perl escape sequences \p, \P, and \X are supported only if PCRE is
59built with Unicode character property support. The properties that can be
60tested with \p and \P are limited to the general category properties such as
61Lu and Nd, script names such as Greek or Han, and the derived properties Any
62and L&. PCRE does support the Cs (surrogate) property, which Perl does not; the
63Perl documentation says "Because Perl hides the need for the user to understand
64the internal representation of Unicode characters, there is no need to
65implement the somewhat messy concept of surrogates."
66</P>
67<P>
687. PCRE does support the \Q...\E escape for quoting substrings. Characters in
69between are treated as literals. This is slightly different from Perl in that $
70and @ are also handled as literals inside the quotes. In Perl, they cause
71variable interpolation (but of course PCRE does not have variables). Note the
72following examples:
73<pre>
74    Pattern            PCRE matches      Perl matches
75
76    \Qabc$xyz\E        abc$xyz           abc followed by the contents of $xyz
77    \Qabc\$xyz\E       abc\$xyz          abc\$xyz
78    \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz
79</pre>
80The \Q...\E sequence is recognized both inside and outside character classes.
81</P>
82<P>
838. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
84constructions. However, there is support for recursive patterns. This is not
85available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout"
86feature allows an external function to be called during pattern matching. See
87the
88<a href="pcrecallout.html"><b>pcrecallout</b></a>
89documentation for details.
90</P>
91<P>
929. Subpatterns that are called as subroutines (whether or not recursively) are
93always treated as atomic groups in PCRE. This is like Python, but unlike Perl.
94Captured values that are set outside a subroutine call can be reference from
95inside in PCRE, but not in Perl. There is a discussion that explains these
96differences in more detail in the
97<a href="pcrepattern.html#recursiondifference">section on recursion differences from Perl</a>
98in the
99<a href="pcrepattern.html"><b>pcrepattern</b></a>
100page.
101</P>
102<P>
10310. If any of the backtracking control verbs are used in a subpattern that is
104called as a subroutine (whether or not recursively), their effect is confined
105to that subpattern; it does not extend to the surrounding pattern. This is not
106always the case in Perl. In particular, if (*THEN) is present in a group that
107is called as a subroutine, its action is limited to that group, even if the
108group does not contain any | characters. Note that such subpatterns are
109processed as anchored at the point where they are tested.
110</P>
111<P>
11211. If a pattern contains more than one backtracking control verb, the first
113one that is backtracked onto acts. For example, in the pattern
114A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
115triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
116same as PCRE, but there are examples where it differs.
117</P>
118<P>
11912. Most backtracking verbs in assertions have their normal actions. They are
120not confined to the assertion.
121</P>
122<P>
12313. There are some differences that are concerned with the settings of captured
124strings when part of a pattern is repeated. For example, matching "aba" against
125the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b".
126</P>
127<P>
12814. PCRE's handling of duplicate subpattern numbers and duplicate subpattern
129names is not as general as Perl's. This is a consequence of the fact the PCRE
130works internally just with numbers, using an external table to translate
131between numbers and names. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b)B),
132where the two capturing parentheses have the same number but different names,
133is not supported, and causes an error at compile time. If it were allowed, it
134would not be possible to distinguish which parentheses matched, because both
135names map to capturing subpattern number 1. To avoid this confusing situation,
136an error is given at compile time.
137</P>
138<P>
13915. Perl recognizes comments in some places that PCRE does not, for example,
140between the ( and ? at the start of a subpattern. If the /x modifier is set,
141Perl allows white space between ( and ? (though current Perls warn that this is
142deprecated) but PCRE never does, even if the PCRE_EXTENDED option is set.
143</P>
144<P>
14516. Perl, when in warning mode, gives warnings for character classes such as
146[A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE has no
147warning features, so it gives an error in these cases because they are almost
148certainly user mistakes.
149</P>
150<P>
15117. In PCRE, the upper/lower case character properties Lu and Ll are not
152affected when case-independent matching is specified. For example, \p{Lu}
153always matches an upper case letter. I think Perl has changed in this respect;
154in the release at the time of writing (5.16), \p{Lu} and \p{Ll} match all
155letters, regardless of case, when case independence is specified.
156</P>
157<P>
15818. PCRE provides some extensions to the Perl regular expression facilities.
159Perl 5.10 includes new features that are not in earlier versions of Perl, some
160of which (such as named parentheses) have been in PCRE for some time. This list
161is with respect to Perl 5.10:
162<br>
163<br>
164(a) Although lookbehind assertions in PCRE must match fixed length strings,
165each alternative branch of a lookbehind assertion can match a different length
166of string. Perl requires them all to have the same length.
167<br>
168<br>
169(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
170meta-character matches only at the very end of the string.
171<br>
172<br>
173(c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
174meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored.
175(Perl can be made to issue a warning.)
176<br>
177<br>
178(d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
179inverted, that is, by default they are not greedy, but if followed by a
180question mark they are.
181<br>
182<br>
183(e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried
184only at the first matching position in the subject string.
185<br>
186<br>
187(f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, and
188PCRE_NO_AUTO_CAPTURE options for <b>pcre_exec()</b> have no Perl equivalents.
189<br>
190<br>
191(g) The \R escape sequence can be restricted to match only CR, LF, or CRLF
192by the PCRE_BSR_ANYCRLF option.
193<br>
194<br>
195(h) The callout facility is PCRE-specific.
196<br>
197<br>
198(i) The partial matching facility is PCRE-specific.
199<br>
200<br>
201(j) Patterns compiled by PCRE can be saved and re-used at a later time, even on
202different hosts that have the other endianness. However, this does not apply to
203optimized data created by the just-in-time compiler.
204<br>
205<br>
206(k) The alternative matching functions (<b>pcre_dfa_exec()</b>,
207<b>pcre16_dfa_exec()</b> and <b>pcre32_dfa_exec()</b>,) match in a different way
208and are not Perl-compatible.
209<br>
210<br>
211(l) PCRE recognizes some special sequences such as (*CR) at the start of
212a pattern that set overall options that cannot be changed within the pattern.
213</P>
214<br><b>
215AUTHOR
216</b><br>
217<P>
218Philip Hazel
219<br>
220University Computing Service
221<br>
222Cambridge CB2 3QH, England.
223<br>
224</P>
225<br><b>
226REVISION
227</b><br>
228<P>
229Last updated: 10 November 2013
230<br>
231Copyright &copy; 1997-2013 University of Cambridge.
232<br>
233<p>
234Return to the <a href="index.html">PCRE index page</a>.
235</p>
236