1<html>
2<head>
3<title>pcre2compat specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcre2compat man page</h1>
7<p>
8Return to the <a href="index.html">PCRE2 index page</a>.
9</p>
10<p>
11This page is part of the PCRE2 HTML documentation. It was generated
12automatically from the original man page. If there is any nonsense in it,
13please consult the man page, in case the conversion went wrong.
14<br>
15<br><b>
16DIFFERENCES BETWEEN PCRE2 AND PERL
17</b><br>
18<P>
19This document describes the differences in the ways that PCRE2 and Perl handle
20regular expressions. The differences described here are with respect to Perl
21versions 5.26, but as both Perl and PCRE2 are continually changing, the
22information may sometimes be out of date.
23</P>
24<P>
251. PCRE2 has only a subset of Perl's Unicode support. Details of what it does
26have are given in the
27<a href="pcre2unicode.html"><b>pcre2unicode</b></a>
28page.
29</P>
30<P>
312. Like Perl, PCRE2 allows repeat quantifiers on parenthesized assertions, but
32they do not mean what you might think. For example, (?!a){3} does not assert
33that the next three characters are not "a". It just asserts that the next
34character is not "a" three times (in principle; PCRE2 optimizes this to run the
35assertion just once). Perl allows some repeat quantifiers on other assertions,
36for example, \b* (but not \b{3}), but these do not seem to have any use.
37</P>
38<P>
393. Capturing subpatterns that occur inside negative lookaround assertions are
40counted, but their entries in the offsets vector are set only when a negative
41assertion is a condition that has a matching branch (that is, the condition is
42false).
43</P>
44<P>
454. The following Perl escape sequences are not supported: \F, \l, \L, \u,
46\U, and \N when followed by a character name. \N on its own, matching a
47non-newline character, and \N{U+dd..}, matching a Unicode code point, are
48supported. The escapes that modify the case of following letters are
49implemented by Perl's general string-handling and are not part of its pattern
50matching engine. If any of these are encountered by PCRE2, an error is
51generated by default. However, if the PCRE2_ALT_BSUX option is set, \U and \u
52are interpreted as ECMAScript interprets them.
53</P>
54<P>
555. The Perl escape sequences \p, \P, and \X are supported only if PCRE2 is
56built with Unicode support (the default). The properties that can be tested
57with \p and \P are limited to the general category properties such as Lu and
58Nd, script names such as Greek or Han, and the derived properties Any and L&.
59PCRE2 does support the Cs (surrogate) property, which Perl does not; the Perl
60documentation says "Because Perl hides the need for the user to understand the
61internal representation of Unicode characters, there is no need to implement
62the somewhat messy concept of surrogates."
63</P>
64<P>
656. PCRE2 supports the \Q...\E escape for quoting substrings. Characters
66in between are treated as literals. However, this is slightly different from
67Perl in that $ and @ are also handled as literals inside the quotes. In Perl,
68they cause variable interpolation (but of course PCRE2 does not have
69variables). Also, Perl does "double-quotish backslash interpolation" on any
70backslashes between \Q and \E which, its documentation says, "may lead to
71confusing results". PCRE2 treats a backslash between \Q and \E just like any
72other character. Note the following examples:
73<pre>
74    Pattern            PCRE2 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    \QA\B\E            A\B               A\B
80    \Q\\E              \                 \\E
81</pre>
82The \Q...\E sequence is recognized both inside and outside character classes.
83</P>
84<P>
857. Fairly obviously, PCRE2 does not support the (?{code}) and (??{code})
86constructions. However, PCRE2 does have a "callout" feature, which allows an
87external function to be called during pattern matching. See the
88<a href="pcre2callout.html"><b>pcre2callout</b></a>
89documentation for details.
90</P>
91<P>
928. Subroutine calls (whether recursive or not) were treated as atomic groups up
93to PCRE2 release 10.23, but from release 10.30 this changed, and backtracking
94into subroutine calls is now supported, as in Perl.
95</P>
96<P>
979. If any of the backtracking control verbs are used in a subpattern that is
98called as a subroutine (whether or not recursively), their effect is confined
99to that subpattern; it does not extend to the surrounding pattern. This is not
100always the case in Perl. In particular, if (*THEN) is present in a group that
101is called as a subroutine, its action is limited to that group, even if the
102group does not contain any | characters. Note that such subpatterns are
103processed as anchored at the point where they are tested.
104</P>
105<P>
10610. If a pattern contains more than one backtracking control verb, the first
107one that is backtracked onto acts. For example, in the pattern
108A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
109triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
110same as PCRE2, but there are cases where it differs.
111</P>
112<P>
11311. Most backtracking verbs in assertions have their normal actions. They are
114not confined to the assertion.
115</P>
116<P>
11712. There are some differences that are concerned with the settings of captured
118strings when part of a pattern is repeated. For example, matching "aba" against
119the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE2 it is set to
120"b".
121</P>
122<P>
12313. PCRE2's handling of duplicate subpattern numbers and duplicate subpattern
124names is not as general as Perl's. This is a consequence of the fact the PCRE2
125works internally just with numbers, using an external table to translate
126between numbers and names. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b&#62;B),
127where the two capturing parentheses have the same number but different names,
128is not supported, and causes an error at compile time. If it were allowed, it
129would not be possible to distinguish which parentheses matched, because both
130names map to capturing subpattern number 1. To avoid this confusing situation,
131an error is given at compile time.
132</P>
133<P>
13414. Perl used to recognize comments in some places that PCRE2 does not, for
135example, between the ( and ? at the start of a subpattern. If the /x modifier
136is set, Perl allowed white space between ( and ? though the latest Perls give
137an error (for a while it was just deprecated). There may still be some cases
138where Perl behaves differently.
139</P>
140<P>
14115. Perl, when in warning mode, gives warnings for character classes such as
142[A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no
143warning features, so it gives an error in these cases because they are almost
144certainly user mistakes.
145</P>
146<P>
14716. In PCRE2, the upper/lower case character properties Lu and Ll are not
148affected when case-independent matching is specified. For example, \p{Lu}
149always matches an upper case letter. I think Perl has changed in this respect;
150in the release at the time of writing (5.24), \p{Lu} and \p{Ll} match all
151letters, regardless of case, when case independence is specified.
152</P>
153<P>
15417. PCRE2 provides some extensions to the Perl regular expression facilities.
155Perl 5.10 includes new features that are not in earlier versions of Perl, some
156of which (such as named parentheses) were in PCRE2 for some time before. This
157list is with respect to Perl 5.26:
158<br>
159<br>
160(a) Although lookbehind assertions in PCRE2 must match fixed length strings,
161each alternative branch of a lookbehind assertion can match a different length
162of string. Perl requires them all to have the same length.
163<br>
164<br>
165(b) From PCRE2 10.23, backreferences to groups of fixed length are supported
166in lookbehinds, provided that there is no possibility of referencing a
167non-unique number or name. Perl does not support backreferences in lookbehinds.
168<br>
169<br>
170(c) If PCRE2_DOLLAR_ENDONLY is set and PCRE2_MULTILINE is not set, the $
171meta-character matches only at the very end of the string.
172<br>
173<br>
174(d) A backslash followed by a letter with no special meaning is faulted. (Perl
175can be made to issue a warning.)
176<br>
177<br>
178(e) If PCRE2_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(f) PCRE2_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(g) The PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART
188options have no Perl equivalents.
189<br>
190<br>
191(h) The \R escape sequence can be restricted to match only CR, LF, or CRLF
192by the PCRE2_BSR_ANYCRLF option.
193<br>
194<br>
195(i) The callout facility is PCRE2-specific. Perl supports codeblocks and
196variable interpolation, but not general hooks on every match.
197<br>
198<br>
199(j) The partial matching facility is PCRE2-specific.
200<br>
201<br>
202(k) The alternative matching function (<b>pcre2_dfa_match()</b> matches in a
203different way and is not Perl-compatible.
204<br>
205<br>
206(l) PCRE2 recognizes some special sequences such as (*CR) or (*NO_JIT) at
207the start of a pattern that set overall options that cannot be changed within
208the pattern.
209</P>
210<P>
21118. The Perl /a modifier restricts /d numbers to pure ascii, and the /aa
212modifier restricts /i case-insensitive matching to pure ascii, ignoring Unicode
213rules. This separation cannot be represented with PCRE2_UCP.
214</P>
215<P>
21619. Perl has different limits than PCRE2. See the
217<a href="pcre2limit.html"><b>pcre2limit</b></a>
218documentation for details. Perl went with 5.10 from recursion to iteration
219keeping the intermediate matches on the heap, which is ~10% slower but does not
220fall into any stack-overflow limit. PCRE2 made a similar change at release
22110.30, and also has many build-time and run-time customizable limits.
222</P>
223<br><b>
224AUTHOR
225</b><br>
226<P>
227Philip Hazel
228<br>
229University Computing Service
230<br>
231Cambridge, England.
232<br>
233</P>
234<br><b>
235REVISION
236</b><br>
237<P>
238Last updated: 28 July 2018
239<br>
240Copyright &copy; 1997-2018 University of Cambridge.
241<br>
242<p>
243Return to the <a href="index.html">PCRE2 index page</a>.
244</p>
245