• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.34.0, but as both Perl and PCRE2 are continually changing, the
22information may at times be out of date.
23</P>
24<P>
251. When PCRE2_DOTALL (equivalent to Perl's /s qualifier) is not set, the
26behaviour of the '.' metacharacter differs from Perl. In PCRE2, '.' matches the
27next character unless it is the start of a newline sequence. This means that,
28if the newline setting is CR, CRLF, or NUL, '.' will match the code point LF
29(0x0A) in ASCII/Unicode environments, and NL (either 0x15 or 0x25) when using
30EBCDIC. In Perl, '.' appears never to match LF, even when 0x0A is not a newline
31indicator.
32</P>
33<P>
342. PCRE2 has only a subset of Perl's Unicode support. Details of what it does
35have are given in the
36<a href="pcre2unicode.html"><b>pcre2unicode</b></a>
37page.
38</P>
39<P>
403. Like Perl, PCRE2 allows repeat quantifiers on parenthesized assertions, but
41they do not mean what you might think. For example, (?!a){3} does not assert
42that the next three characters are not "a". It just asserts that the next
43character is not "a" three times (in principle; PCRE2 optimizes this to run the
44assertion just once). Perl allows some repeat quantifiers on other assertions,
45for example, \b* , but these do not seem to have any use. PCRE2 does not allow
46any kind of quantifier on non-lookaround assertions.
47</P>
48<P>
494. Capture groups that occur inside negative lookaround assertions are counted,
50but their entries in the offsets vector are set only when a negative assertion
51is a condition that has a matching branch (that is, the condition is false).
52Perl may set such capture groups in other circumstances.
53</P>
54<P>
555. The following Perl escape sequences are not supported: \F, \l, \L, \u,
56\U, and \N when followed by a character name. \N on its own, matching a
57non-newline character, and \N{U+dd..}, matching a Unicode code point, are
58supported. The escapes that modify the case of following letters are
59implemented by Perl's general string-handling and are not part of its pattern
60matching engine. If any of these are encountered by PCRE2, an error is
61generated by default. However, if either of the PCRE2_ALT_BSUX or
62PCRE2_EXTRA_ALT_BSUX options is set, \U and \u are interpreted as ECMAScript
63interprets them.
64</P>
65<P>
666. The Perl escape sequences \p, \P, and \X are supported only if PCRE2 is
67built with Unicode support (the default). The properties that can be tested
68with \p and \P are limited to the general category properties such as Lu and
69Nd, script names such as Greek or Han, Bidi_Class, Bidi_Control, and the
70derived properties Any and LC (synonym L&). Both PCRE2 and Perl support the Cs
71(surrogate) property, but in PCRE2 its use is limited. See the
72<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
73documentation for details. The long synonyms for property names that Perl
74supports (such as \p{Letter}) are not supported by PCRE2, nor is it permitted
75to prefix any of these properties with "Is".
76</P>
77<P>
787. PCRE2 supports the \Q...\E escape for quoting substrings. Characters
79in between are treated as literals. However, this is slightly different from
80Perl in that $ and @ are also handled as literals inside the quotes. In Perl,
81they cause variable interpolation (PCRE2 does not have variables). Also, Perl
82does "double-quotish backslash interpolation" on any backslashes between \Q
83and \E which, its documentation says, "may lead to confusing results". PCRE2
84treats a backslash between \Q and \E just like any other character. Note the
85following examples:
86<pre>
87    Pattern            PCRE2 matches     Perl matches
88
89    \Qabc$xyz\E        abc$xyz           abc followed by the contents of $xyz
90    \Qabc\$xyz\E       abc\$xyz          abc\$xyz
91    \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz
92    \QA\B\E            A\B               A\B
93    \Q\\E              \                 \\E
94</pre>
95The \Q...\E sequence is recognized both inside and outside character classes
96by both PCRE2 and Perl.
97</P>
98<P>
998. Fairly obviously, PCRE2 does not support the (?{code}) and (??{code})
100constructions. However, PCRE2 does have a "callout" feature, which allows an
101external function to be called during pattern matching. See the
102<a href="pcre2callout.html"><b>pcre2callout</b></a>
103documentation for details.
104</P>
105<P>
1069. Subroutine calls (whether recursive or not) were treated as atomic groups up
107to PCRE2 release 10.23, but from release 10.30 this changed, and backtracking
108into subroutine calls is now supported, as in Perl.
109</P>
110<P>
11110. In PCRE2, if any of the backtracking control verbs are used in a group that
112is called as a subroutine (whether or not recursively), their effect is
113confined to that group; it does not extend to the surrounding pattern. This is
114not always the case in Perl. In particular, if (*THEN) is present in a group
115that is called as a subroutine, its action is limited to that group, even if
116the group does not contain any | characters. Note that such groups are
117processed as anchored at the point where they are tested.
118</P>
119<P>
12011. If a pattern contains more than one backtracking control verb, the first
121one that is backtracked onto acts. For example, in the pattern
122A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
123triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
124same as PCRE2, but there are cases where it differs.
125</P>
126<P>
12712. There are some differences that are concerned with the settings of captured
128strings when part of a pattern is repeated. For example, matching "aba" against
129the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE2 it is set to
130"b".
131</P>
132<P>
13313. PCRE2's handling of duplicate capture group numbers and names is not as
134general as Perl's. This is a consequence of the fact the PCRE2 works internally
135just with numbers, using an external table to translate between numbers and
136names. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b&#62;B)), where the two
137capture groups have the same number but different names, is not supported, and
138causes an error at compile time. If it were allowed, it would not be possible
139to distinguish which group matched, because both names map to capture group
140number 1. To avoid this confusing situation, an error is given at compile time.
141</P>
142<P>
14314. Perl used to recognize comments in some places that PCRE2 does not, for
144example, between the ( and ? at the start of a group. If the /x modifier is
145set, Perl allowed white space between ( and ? though the latest Perls give an
146error (for a while it was just deprecated). There may still be some cases where
147Perl behaves differently.
148</P>
149<P>
15015. Perl, when in warning mode, gives warnings for character classes such as
151[A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no
152warning features, so it gives an error in these cases because they are almost
153certainly user mistakes.
154</P>
155<P>
15616. In PCRE2, the upper/lower case character properties Lu and Ll are not
157affected when case-independent matching is specified. For example, \p{Lu}
158always matches an upper case letter. I think Perl has changed in this respect;
159in the release at the time of writing (5.34), \p{Lu} and \p{Ll} match all
160letters, regardless of case, when case independence is specified.
161</P>
162<P>
16317. From release 5.32.0, Perl locks out the use of \K in lookaround
164assertions. From release 10.38 PCRE2 does the same by default. However, there
165is an option for re-enabling the previous behaviour. When this option is set,
166\K is acted on when it occurs in positive assertions, but is ignored in
167negative assertions.
168</P>
169<P>
17018. PCRE2 provides some extensions to the Perl regular expression facilities.
171Perl 5.10 included new features that were not in earlier versions of Perl, some
172of which (such as named parentheses) were in PCRE2 for some time before. This
173list is with respect to Perl 5.34:
174<br>
175<br>
176(a) Although lookbehind assertions in PCRE2 must match fixed length strings,
177each alternative toplevel branch of a lookbehind assertion can match a
178different length of string. Perl used to require them all to have the same
179length, but the latest version has some variable length support.
180<br>
181<br>
182(b) From PCRE2 10.23, backreferences to groups of fixed length are supported
183in lookbehinds, provided that there is no possibility of referencing a
184non-unique number or name. Perl does not support backreferences in lookbehinds.
185<br>
186<br>
187(c) If PCRE2_DOLLAR_ENDONLY is set and PCRE2_MULTILINE is not set, the $
188meta-character matches only at the very end of the string.
189<br>
190<br>
191(d) A backslash followed by a letter with no special meaning is faulted. (Perl
192can be made to issue a warning.)
193<br>
194<br>
195(e) If PCRE2_UNGREEDY is set, the greediness of the repetition quantifiers is
196inverted, that is, by default they are not greedy, but if followed by a
197question mark they are.
198<br>
199<br>
200(f) PCRE2_ANCHORED can be used at matching time to force a pattern to be tried
201only at the first matching position in the subject string.
202<br>
203<br>
204(g) The PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART
205options have no Perl equivalents.
206<br>
207<br>
208(h) The \R escape sequence can be restricted to match only CR, LF, or CRLF
209by the PCRE2_BSR_ANYCRLF option.
210<br>
211<br>
212(i) The callout facility is PCRE2-specific. Perl supports codeblocks and
213variable interpolation, but not general hooks on every match.
214<br>
215<br>
216(j) The partial matching facility is PCRE2-specific.
217<br>
218<br>
219(k) The alternative matching function (<b>pcre2_dfa_match()</b> matches in a
220different way and is not Perl-compatible.
221<br>
222<br>
223(l) PCRE2 recognizes some special sequences such as (*CR) or (*NO_JIT) at
224the start of a pattern. These set overall options that cannot be changed within
225the pattern.
226<br>
227<br>
228(m) PCRE2 supports non-atomic positive lookaround assertions. This is an
229extension to the lookaround facilities. The default, Perl-compatible
230lookarounds are atomic.
231</P>
232<P>
23319. The Perl /a modifier restricts /d numbers to pure ascii, and the /aa
234modifier restricts /i case-insensitive matching to pure ascii, ignoring Unicode
235rules. This separation cannot be represented with PCRE2_UCP.
236</P>
237<P>
23820. Perl has different limits than PCRE2. See the
239<a href="pcre2limit.html"><b>pcre2limit</b></a>
240documentation for details. Perl went with 5.10 from recursion to iteration
241keeping the intermediate matches on the heap, which is ~10% slower but does not
242fall into any stack-overflow limit. PCRE2 made a similar change at release
24310.30, and also has many build-time and run-time customizable limits.
244</P>
245<br><b>
246AUTHOR
247</b><br>
248<P>
249Philip Hazel
250<br>
251Retired from University Computing Service
252<br>
253Cambridge, England.
254<br>
255</P>
256<br><b>
257REVISION
258</b><br>
259<P>
260Last updated: 08 December 2021
261<br>
262Copyright &copy; 1997-2021 University of Cambridge.
263<br>
264<p>
265Return to the <a href="index.html">PCRE2 index page</a>.
266</p>
267