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 some of the differences in the ways that PCRE2 and Perl
20handle regular expressions. The differences described here are with respect to
21Perl version 5.32.0, but as both Perl and PCRE2 are continually changing, the
22information may at times 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}, though oddly it does allow ^{3}), but these
37do not seem to have any use. PCRE2 does not allow any kind of quantifier on
38non-lookaround assertions.
39</P>
40<P>
413. Capture groups that occur inside negative lookaround assertions are counted,
42but their entries in the offsets vector are set only when a negative assertion
43is a condition that has a matching branch (that is, the condition is false).
44Perl may set such capture groups in other circumstances.
45</P>
46<P>
474. The following Perl escape sequences are not supported: \F, \l, \L, \u,
48\U, and \N when followed by a character name. \N on its own, matching a
49non-newline character, and \N{U+dd..}, matching a Unicode code point, are
50supported. The escapes that modify the case of following letters are
51implemented by Perl's general string-handling and are not part of its pattern
52matching engine. If any of these are encountered by PCRE2, an error is
53generated by default. However, if either of the PCRE2_ALT_BSUX or
54PCRE2_EXTRA_ALT_BSUX options is set, \U and \u are interpreted as ECMAScript
55interprets them.
56</P>
57<P>
585. The Perl escape sequences \p, \P, and \X are supported only if PCRE2 is
59built with Unicode support (the default). The properties that can be tested
60with \p and \P are limited to the general category properties such as Lu and
61Nd, script names such as Greek or Han, and the derived properties Any and L&.
62Both PCRE2 and Perl support the Cs (surrogate) property, but in PCRE2 its use
63is limited. See the
64<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
65documentation for details. The long synonyms for property names that Perl
66supports (such as \p{Letter}) are not supported by PCRE2, nor is it permitted
67to prefix any of these properties with "Is".
68</P>
69<P>
706. PCRE2 supports the \Q...\E escape for quoting substrings. Characters
71in between are treated as literals. However, this is slightly different from
72Perl in that $ and @ are also handled as literals inside the quotes. In Perl,
73they cause variable interpolation (but of course PCRE2 does not have
74variables). Also, Perl does "double-quotish backslash interpolation" on any
75backslashes between \Q and \E which, its documentation says, "may lead to
76confusing results". PCRE2 treats a backslash between \Q and \E just like any
77other character. Note the following examples:
78<pre>
79    Pattern            PCRE2 matches     Perl matches
80
81    \Qabc$xyz\E        abc$xyz           abc followed by the contents of $xyz
82    \Qabc\$xyz\E       abc\$xyz          abc\$xyz
83    \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz
84    \QA\B\E            A\B               A\B
85    \Q\\E              \                 \\E
86</pre>
87The \Q...\E sequence is recognized both inside and outside character classes
88by both PCRE2 and Perl.
89</P>
90<P>
917. Fairly obviously, PCRE2 does not support the (?{code}) and (??{code})
92constructions. However, PCRE2 does have a "callout" feature, which allows an
93external function to be called during pattern matching. See the
94<a href="pcre2callout.html"><b>pcre2callout</b></a>
95documentation for details.
96</P>
97<P>
988. Subroutine calls (whether recursive or not) were treated as atomic groups up
99to PCRE2 release 10.23, but from release 10.30 this changed, and backtracking
100into subroutine calls is now supported, as in Perl.
101</P>
102<P>
1039. In PCRE2, if any of the backtracking control verbs are used in a group that
104is called as a subroutine (whether or not recursively), their effect is
105confined to that group; it does not extend to the surrounding pattern. This is
106not always the case in Perl. In particular, if (*THEN) is present in a group
107that is called as a subroutine, its action is limited to that group, even if
108the group does not contain any | characters. Note that such groups are
109processed as anchored at the point where they are tested.
110</P>
111<P>
11210. 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 PCRE2, but there are cases where it differs.
117</P>
118<P>
11911. There are some differences that are concerned with the settings of captured
120strings when part of a pattern is repeated. For example, matching "aba" against
121the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE2 it is set to
122"b".
123</P>
124<P>
12512. PCRE2's handling of duplicate capture group numbers and names is not as
126general as Perl's. This is a consequence of the fact the PCRE2 works internally
127just with numbers, using an external table to translate between numbers and
128names. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b&#62;B)), where the two
129capture groups have the same number but different names, is not supported, and
130causes an error at compile time. If it were allowed, it would not be possible
131to distinguish which group matched, because both names map to capture group
132number 1. To avoid this confusing situation, an error is given at compile time.
133</P>
134<P>
13513. Perl used to recognize comments in some places that PCRE2 does not, for
136example, between the ( and ? at the start of a group. If the /x modifier is
137set, Perl allowed white space between ( and ? though the latest Perls give an
138error (for a while it was just deprecated). There may still be some cases where
139Perl behaves differently.
140</P>
141<P>
14214. Perl, when in warning mode, gives warnings for character classes such as
143[A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no
144warning features, so it gives an error in these cases because they are almost
145certainly user mistakes.
146</P>
147<P>
14815. In PCRE2, the upper/lower case character properties Lu and Ll are not
149affected when case-independent matching is specified. For example, \p{Lu}
150always matches an upper case letter. I think Perl has changed in this respect;
151in the release at the time of writing (5.32), \p{Lu} and \p{Ll} match all
152letters, regardless of case, when case independence is specified.
153</P>
154<P>
15516. From release 5.32.0, Perl locks out the use of \K in lookaround
156assertions. In PCRE2, \K is acted on when it occurs in positive assertions,
157but is ignored in negative assertions.
158</P>
159<P>
16017. PCRE2 provides some extensions to the Perl regular expression facilities.
161Perl 5.10 included new features that were not in earlier versions of Perl, some
162of which (such as named parentheses) were in PCRE2 for some time before. This
163list is with respect to Perl 5.32:
164<br>
165<br>
166(a) Although lookbehind assertions in PCRE2 must match fixed length strings,
167each alternative toplevel branch of a lookbehind assertion can match a
168different length of string. Perl requires them all to have the same length.
169<br>
170<br>
171(b) From PCRE2 10.23, backreferences to groups of fixed length are supported
172in lookbehinds, provided that there is no possibility of referencing a
173non-unique number or name. Perl does not support backreferences in lookbehinds.
174<br>
175<br>
176(c) If PCRE2_DOLLAR_ENDONLY is set and PCRE2_MULTILINE is not set, the $
177meta-character matches only at the very end of the string.
178<br>
179<br>
180(d) A backslash followed by a letter with no special meaning is faulted. (Perl
181can be made to issue a warning.)
182<br>
183<br>
184(e) If PCRE2_UNGREEDY is set, the greediness of the repetition quantifiers is
185inverted, that is, by default they are not greedy, but if followed by a
186question mark they are.
187<br>
188<br>
189(f) PCRE2_ANCHORED can be used at matching time to force a pattern to be tried
190only at the first matching position in the subject string.
191<br>
192<br>
193(g) The PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART
194options have no Perl equivalents.
195<br>
196<br>
197(h) The \R escape sequence can be restricted to match only CR, LF, or CRLF
198by the PCRE2_BSR_ANYCRLF option.
199<br>
200<br>
201(i) The callout facility is PCRE2-specific. Perl supports codeblocks and
202variable interpolation, but not general hooks on every match.
203<br>
204<br>
205(j) The partial matching facility is PCRE2-specific.
206<br>
207<br>
208(k) The alternative matching function (<b>pcre2_dfa_match()</b> matches in a
209different way and is not Perl-compatible.
210<br>
211<br>
212(l) PCRE2 recognizes some special sequences such as (*CR) or (*NO_JIT) at
213the start of a pattern. These set overall options that cannot be changed within
214the pattern.
215<br>
216<br>
217(m) PCRE2 supports non-atomic positive lookaround assertions. This is an
218extension to the lookaround facilities. The default, Perl-compatible
219lookarounds are atomic.
220</P>
221<P>
22218. The Perl /a modifier restricts /d numbers to pure ascii, and the /aa
223modifier restricts /i case-insensitive matching to pure ascii, ignoring Unicode
224rules. This separation cannot be represented with PCRE2_UCP.
225</P>
226<P>
22719. Perl has different limits than PCRE2. See the
228<a href="pcre2limit.html"><b>pcre2limit</b></a>
229documentation for details. Perl went with 5.10 from recursion to iteration
230keeping the intermediate matches on the heap, which is ~10% slower but does not
231fall into any stack-overflow limit. PCRE2 made a similar change at release
23210.30, and also has many build-time and run-time customizable limits.
233</P>
234<br><b>
235AUTHOR
236</b><br>
237<P>
238Philip Hazel
239<br>
240University Computing Service
241<br>
242Cambridge, England.
243<br>
244</P>
245<br><b>
246REVISION
247</b><br>
248<P>
249Last updated: 06 October 2020
250<br>
251Copyright &copy; 1997-2019 University of Cambridge.
252<br>
253<p>
254Return to the <a href="index.html">PCRE2 index page</a>.
255</p>
256