xref: /aosp_15_r20/external/pcre/doc/html/pcre2compat.html (revision 22dc650d8ae982c6770746019a6f94af92b0f024)
1*22dc650dSSadaf Ebrahimi<html>
2*22dc650dSSadaf Ebrahimi<head>
3*22dc650dSSadaf Ebrahimi<title>pcre2compat specification</title>
4*22dc650dSSadaf Ebrahimi</head>
5*22dc650dSSadaf Ebrahimi<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6*22dc650dSSadaf Ebrahimi<h1>pcre2compat man page</h1>
7*22dc650dSSadaf Ebrahimi<p>
8*22dc650dSSadaf EbrahimiReturn to the <a href="index.html">PCRE2 index page</a>.
9*22dc650dSSadaf Ebrahimi</p>
10*22dc650dSSadaf Ebrahimi<p>
11*22dc650dSSadaf EbrahimiThis page is part of the PCRE2 HTML documentation. It was generated
12*22dc650dSSadaf Ebrahimiautomatically from the original man page. If there is any nonsense in it,
13*22dc650dSSadaf Ebrahimiplease consult the man page, in case the conversion went wrong.
14*22dc650dSSadaf Ebrahimi<br>
15*22dc650dSSadaf Ebrahimi<br><b>
16*22dc650dSSadaf EbrahimiDIFFERENCES BETWEEN PCRE2 AND PERL
17*22dc650dSSadaf Ebrahimi</b><br>
18*22dc650dSSadaf Ebrahimi<P>
19*22dc650dSSadaf EbrahimiThis document describes some of the known differences in the ways that PCRE2
20*22dc650dSSadaf Ebrahimiand Perl handle regular expressions. The differences described here are with
21*22dc650dSSadaf Ebrahimirespect to Perl version 5.38.0, but as both Perl and PCRE2 are continually
22*22dc650dSSadaf Ebrahimichanging, the information may at times be out of date.
23*22dc650dSSadaf Ebrahimi</P>
24*22dc650dSSadaf Ebrahimi<P>
25*22dc650dSSadaf Ebrahimi1. When PCRE2_DOTALL (equivalent to Perl's /s qualifier) is not set, the
26*22dc650dSSadaf Ebrahimibehaviour of the '.' metacharacter differs from Perl. In PCRE2, '.' matches the
27*22dc650dSSadaf Ebrahiminext character unless it is the start of a newline sequence. This means that,
28*22dc650dSSadaf Ebrahimiif the newline setting is CR, CRLF, or NUL, '.' will match the code point LF
29*22dc650dSSadaf Ebrahimi(0x0A) in ASCII/Unicode environments, and NL (either 0x15 or 0x25) when using
30*22dc650dSSadaf EbrahimiEBCDIC. In Perl, '.' appears never to match LF, even when 0x0A is not a newline
31*22dc650dSSadaf Ebrahimiindicator.
32*22dc650dSSadaf Ebrahimi</P>
33*22dc650dSSadaf Ebrahimi<P>
34*22dc650dSSadaf Ebrahimi2. PCRE2 has only a subset of Perl's Unicode support. Details of what it does
35*22dc650dSSadaf Ebrahimihave are given in the
36*22dc650dSSadaf Ebrahimi<a href="pcre2unicode.html"><b>pcre2unicode</b></a>
37*22dc650dSSadaf Ebrahimipage.
38*22dc650dSSadaf Ebrahimi</P>
39*22dc650dSSadaf Ebrahimi<P>
40*22dc650dSSadaf Ebrahimi3. Like Perl, PCRE2 allows repeat quantifiers on parenthesized assertions, but
41*22dc650dSSadaf Ebrahimithey do not mean what you might think. For example, (?!a){3} does not assert
42*22dc650dSSadaf Ebrahimithat the next three characters are not "a". It just asserts that the next
43*22dc650dSSadaf Ebrahimicharacter is not "a" three times (in principle; PCRE2 optimizes this to run the
44*22dc650dSSadaf Ebrahimiassertion just once). Perl allows some repeat quantifiers on other assertions,
45*22dc650dSSadaf Ebrahimifor example, \b* , but these do not seem to have any use. PCRE2 does not allow
46*22dc650dSSadaf Ebrahimiany kind of quantifier on non-lookaround assertions.
47*22dc650dSSadaf Ebrahimi</P>
48*22dc650dSSadaf Ebrahimi<P>
49*22dc650dSSadaf Ebrahimi4. If a braced quantifier such as {1,2} appears where there is nothing to
50*22dc650dSSadaf Ebrahimirepeat (for example, at the start of a branch), PCRE2 raises an error whereas
51*22dc650dSSadaf EbrahimiPerl treats the quantifier characters as literal.
52*22dc650dSSadaf Ebrahimi</P>
53*22dc650dSSadaf Ebrahimi<P>
54*22dc650dSSadaf Ebrahimi5. Capture groups that occur inside negative lookaround assertions are counted,
55*22dc650dSSadaf Ebrahimibut their entries in the offsets vector are set only when a negative assertion
56*22dc650dSSadaf Ebrahimiis a condition that has a matching branch (that is, the condition is false).
57*22dc650dSSadaf EbrahimiPerl may set such capture groups in other circumstances.
58*22dc650dSSadaf Ebrahimi</P>
59*22dc650dSSadaf Ebrahimi<P>
60*22dc650dSSadaf Ebrahimi6. The following Perl escape sequences are not supported: \F, \l, \L, \u,
61*22dc650dSSadaf Ebrahimi\U, and \N when followed by a character name. \N on its own, matching a
62*22dc650dSSadaf Ebrahiminon-newline character, and \N{U+dd..}, matching a Unicode code point, are
63*22dc650dSSadaf Ebrahimisupported. The escapes that modify the case of following letters are
64*22dc650dSSadaf Ebrahimiimplemented by Perl's general string-handling and are not part of its pattern
65*22dc650dSSadaf Ebrahimimatching engine. If any of these are encountered by PCRE2, an error is
66*22dc650dSSadaf Ebrahimigenerated by default. However, if either of the PCRE2_ALT_BSUX or
67*22dc650dSSadaf EbrahimiPCRE2_EXTRA_ALT_BSUX options is set, \U and \u are interpreted as ECMAScript
68*22dc650dSSadaf Ebrahimiinterprets them.
69*22dc650dSSadaf Ebrahimi</P>
70*22dc650dSSadaf Ebrahimi<P>
71*22dc650dSSadaf Ebrahimi7. The Perl escape sequences \p, \P, and \X are supported only if PCRE2 is
72*22dc650dSSadaf Ebrahimibuilt with Unicode support (the default). The properties that can be tested
73*22dc650dSSadaf Ebrahimiwith \p and \P are limited to the general category properties such as Lu and
74*22dc650dSSadaf EbrahimiNd, the derived properties Any and LC (synonym L&), script names such as Greek
75*22dc650dSSadaf Ebrahimior Han, Bidi_Class, Bidi_Control, and a few binary properties. Both PCRE2 and
76*22dc650dSSadaf EbrahimiPerl support the Cs (surrogate) property, but in PCRE2 its use is limited. See
77*22dc650dSSadaf Ebrahimithe
78*22dc650dSSadaf Ebrahimi<a href="pcre2pattern.html"><b>pcre2pattern</b></a>
79*22dc650dSSadaf Ebrahimidocumentation for details. The long synonyms for property names that Perl
80*22dc650dSSadaf Ebrahimisupports (such as \p{Letter}) are not supported by PCRE2, nor is it permitted
81*22dc650dSSadaf Ebrahimito prefix any of these properties with "Is".
82*22dc650dSSadaf Ebrahimi</P>
83*22dc650dSSadaf Ebrahimi<P>
84*22dc650dSSadaf Ebrahimi8. PCRE2 supports the \Q...\E escape for quoting substrings. Characters
85*22dc650dSSadaf Ebrahimiin between are treated as literals. However, this is slightly different from
86*22dc650dSSadaf EbrahimiPerl in that $ and @ are also handled as literals inside the quotes. In Perl,
87*22dc650dSSadaf Ebrahimithey cause variable interpolation (PCRE2 does not have variables). Also, Perl
88*22dc650dSSadaf Ebrahimidoes "double-quotish backslash interpolation" on any backslashes between \Q
89*22dc650dSSadaf Ebrahimiand \E which, its documentation says, "may lead to confusing results". PCRE2
90*22dc650dSSadaf Ebrahimitreats a backslash between \Q and \E just like any other character. Note the
91*22dc650dSSadaf Ebrahimifollowing examples:
92*22dc650dSSadaf Ebrahimi<pre>
93*22dc650dSSadaf Ebrahimi    Pattern            PCRE2 matches     Perl matches
94*22dc650dSSadaf Ebrahimi
95*22dc650dSSadaf Ebrahimi    \Qabc$xyz\E        abc$xyz           abc followed by the contents of $xyz
96*22dc650dSSadaf Ebrahimi    \Qabc\$xyz\E       abc\$xyz          abc\$xyz
97*22dc650dSSadaf Ebrahimi    \Qabc\E\$\Qxyz\E   abc$xyz           abc$xyz
98*22dc650dSSadaf Ebrahimi    \QA\B\E            A\B               A\B
99*22dc650dSSadaf Ebrahimi    \Q\\E              \                 \\E
100*22dc650dSSadaf Ebrahimi</pre>
101*22dc650dSSadaf EbrahimiThe \Q...\E sequence is recognized both inside and outside character classes
102*22dc650dSSadaf Ebrahimiby both PCRE2 and Perl.
103*22dc650dSSadaf Ebrahimi</P>
104*22dc650dSSadaf Ebrahimi<P>
105*22dc650dSSadaf Ebrahimi9. Fairly obviously, PCRE2 does not support the (?{code}) and (??{code})
106*22dc650dSSadaf Ebrahimiconstructions. However, PCRE2 does have a "callout" feature, which allows an
107*22dc650dSSadaf Ebrahimiexternal function to be called during pattern matching. See the
108*22dc650dSSadaf Ebrahimi<a href="pcre2callout.html"><b>pcre2callout</b></a>
109*22dc650dSSadaf Ebrahimidocumentation for details.
110*22dc650dSSadaf Ebrahimi</P>
111*22dc650dSSadaf Ebrahimi<P>
112*22dc650dSSadaf Ebrahimi10. Subroutine calls (whether recursive or not) were treated as atomic groups
113*22dc650dSSadaf Ebrahimiup to PCRE2 release 10.23, but from release 10.30 this changed, and
114*22dc650dSSadaf Ebrahimibacktracking into subroutine calls is now supported, as in Perl.
115*22dc650dSSadaf Ebrahimi</P>
116*22dc650dSSadaf Ebrahimi<P>
117*22dc650dSSadaf Ebrahimi11. In PCRE2, if any of the backtracking control verbs are used in a group that
118*22dc650dSSadaf Ebrahimiis called as a subroutine (whether or not recursively), their effect is
119*22dc650dSSadaf Ebrahimiconfined to that group; it does not extend to the surrounding pattern. This is
120*22dc650dSSadaf Ebrahiminot always the case in Perl. In particular, if (*THEN) is present in a group
121*22dc650dSSadaf Ebrahimithat is called as a subroutine, its action is limited to that group, even if
122*22dc650dSSadaf Ebrahimithe group does not contain any | characters. Note that such groups are
123*22dc650dSSadaf Ebrahimiprocessed as anchored at the point where they are tested.
124*22dc650dSSadaf Ebrahimi</P>
125*22dc650dSSadaf Ebrahimi<P>
126*22dc650dSSadaf Ebrahimi12. If a pattern contains more than one backtracking control verb, the first
127*22dc650dSSadaf Ebrahimione that is backtracked onto acts. For example, in the pattern
128*22dc650dSSadaf EbrahimiA(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
129*22dc650dSSadaf Ebrahimitriggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
130*22dc650dSSadaf Ebrahimisame as PCRE2, but there are cases where it differs.
131*22dc650dSSadaf Ebrahimi</P>
132*22dc650dSSadaf Ebrahimi<P>
133*22dc650dSSadaf Ebrahimi13. There are some differences that are concerned with the settings of captured
134*22dc650dSSadaf Ebrahimistrings when part of a pattern is repeated. For example, matching "aba" against
135*22dc650dSSadaf Ebrahimithe pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE2 it is set to
136*22dc650dSSadaf Ebrahimi"b".
137*22dc650dSSadaf Ebrahimi</P>
138*22dc650dSSadaf Ebrahimi<P>
139*22dc650dSSadaf Ebrahimi14. PCRE2's handling of duplicate capture group numbers and names is not as
140*22dc650dSSadaf Ebrahimigeneral as Perl's. This is a consequence of the fact the PCRE2 works internally
141*22dc650dSSadaf Ebrahimijust with numbers, using an external table to translate between numbers and
142*22dc650dSSadaf Ebrahiminames. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b&#62;B)), where the two
143*22dc650dSSadaf Ebrahimicapture groups have the same number but different names, is not supported, and
144*22dc650dSSadaf Ebrahimicauses an error at compile time. If it were allowed, it would not be possible
145*22dc650dSSadaf Ebrahimito distinguish which group matched, because both names map to capture group
146*22dc650dSSadaf Ebrahiminumber 1. To avoid this confusing situation, an error is given at compile time.
147*22dc650dSSadaf Ebrahimi</P>
148*22dc650dSSadaf Ebrahimi<P>
149*22dc650dSSadaf Ebrahimi15. Perl used to recognize comments in some places that PCRE2 does not, for
150*22dc650dSSadaf Ebrahimiexample, between the ( and ? at the start of a group. If the /x modifier is
151*22dc650dSSadaf Ebrahimiset, Perl allowed white space between ( and ? though the latest Perls give an
152*22dc650dSSadaf Ebrahimierror (for a while it was just deprecated). There may still be some cases where
153*22dc650dSSadaf EbrahimiPerl behaves differently.
154*22dc650dSSadaf Ebrahimi</P>
155*22dc650dSSadaf Ebrahimi<P>
156*22dc650dSSadaf Ebrahimi16. Perl, when in warning mode, gives warnings for character classes such as
157*22dc650dSSadaf Ebrahimi[A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no
158*22dc650dSSadaf Ebrahimiwarning features, so it gives an error in these cases because they are almost
159*22dc650dSSadaf Ebrahimicertainly user mistakes.
160*22dc650dSSadaf Ebrahimi</P>
161*22dc650dSSadaf Ebrahimi<P>
162*22dc650dSSadaf Ebrahimi17. In PCRE2, the upper/lower case character properties Lu and Ll are not
163*22dc650dSSadaf Ebrahimiaffected when case-independent matching is specified. For example, \p{Lu}
164*22dc650dSSadaf Ebrahimialways matches an upper case letter. I think Perl has changed in this respect;
165*22dc650dSSadaf Ebrahimiin the release at the time of writing (5.38), \p{Lu} and \p{Ll} match all
166*22dc650dSSadaf Ebrahimiletters, regardless of case, when case independence is specified.
167*22dc650dSSadaf Ebrahimi</P>
168*22dc650dSSadaf Ebrahimi<P>
169*22dc650dSSadaf Ebrahimi18. From release 5.32.0, Perl locks out the use of \K in lookaround
170*22dc650dSSadaf Ebrahimiassertions. From release 10.38 PCRE2 does the same by default. However, there
171*22dc650dSSadaf Ebrahimiis an option for re-enabling the previous behaviour. When this option is set,
172*22dc650dSSadaf Ebrahimi\K is acted on when it occurs in positive assertions, but is ignored in
173*22dc650dSSadaf Ebrahiminegative assertions.
174*22dc650dSSadaf Ebrahimi</P>
175*22dc650dSSadaf Ebrahimi<P>
176*22dc650dSSadaf Ebrahimi19. PCRE2 provides some extensions to the Perl regular expression facilities.
177*22dc650dSSadaf EbrahimiPerl 5.10 included new features that were not in earlier versions of Perl, some
178*22dc650dSSadaf Ebrahimiof which (such as named parentheses) were in PCRE2 for some time before. This
179*22dc650dSSadaf Ebrahimilist is with respect to Perl 5.38:
180*22dc650dSSadaf Ebrahimi<br>
181*22dc650dSSadaf Ebrahimi<br>
182*22dc650dSSadaf Ebrahimi(a) If PCRE2_DOLLAR_ENDONLY is set and PCRE2_MULTILINE is not set, the $
183*22dc650dSSadaf Ebrahimimeta-character matches only at the very end of the string.
184*22dc650dSSadaf Ebrahimi<br>
185*22dc650dSSadaf Ebrahimi<br>
186*22dc650dSSadaf Ebrahimi(b) A backslash followed by a letter with no special meaning is faulted. (Perl
187*22dc650dSSadaf Ebrahimican be made to issue a warning.)
188*22dc650dSSadaf Ebrahimi<br>
189*22dc650dSSadaf Ebrahimi<br>
190*22dc650dSSadaf Ebrahimi(c) If PCRE2_UNGREEDY is set, the greediness of the repetition quantifiers is
191*22dc650dSSadaf Ebrahimiinverted, that is, by default they are not greedy, but if followed by a
192*22dc650dSSadaf Ebrahimiquestion mark they are.
193*22dc650dSSadaf Ebrahimi<br>
194*22dc650dSSadaf Ebrahimi<br>
195*22dc650dSSadaf Ebrahimi(d) PCRE2_ANCHORED can be used at matching time to force a pattern to be tried
196*22dc650dSSadaf Ebrahimionly at the first matching position in the subject string.
197*22dc650dSSadaf Ebrahimi<br>
198*22dc650dSSadaf Ebrahimi<br>
199*22dc650dSSadaf Ebrahimi(e) The PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART
200*22dc650dSSadaf Ebrahimioptions have no Perl equivalents.
201*22dc650dSSadaf Ebrahimi<br>
202*22dc650dSSadaf Ebrahimi<br>
203*22dc650dSSadaf Ebrahimi(f) The \R escape sequence can be restricted to match only CR, LF, or CRLF
204*22dc650dSSadaf Ebrahimiby the PCRE2_BSR_ANYCRLF option.
205*22dc650dSSadaf Ebrahimi<br>
206*22dc650dSSadaf Ebrahimi<br>
207*22dc650dSSadaf Ebrahimi(g) The callout facility is PCRE2-specific. Perl supports codeblocks and
208*22dc650dSSadaf Ebrahimivariable interpolation, but not general hooks on every match.
209*22dc650dSSadaf Ebrahimi<br>
210*22dc650dSSadaf Ebrahimi<br>
211*22dc650dSSadaf Ebrahimi(h) The partial matching facility is PCRE2-specific.
212*22dc650dSSadaf Ebrahimi<br>
213*22dc650dSSadaf Ebrahimi<br>
214*22dc650dSSadaf Ebrahimi(i) The alternative matching function (<b>pcre2_dfa_match()</b> matches in a
215*22dc650dSSadaf Ebrahimidifferent way and is not Perl-compatible.
216*22dc650dSSadaf Ebrahimi<br>
217*22dc650dSSadaf Ebrahimi<br>
218*22dc650dSSadaf Ebrahimi(j) PCRE2 recognizes some special sequences such as (*CR) or (*NO_JIT) at
219*22dc650dSSadaf Ebrahimithe start of a pattern. These set overall options that cannot be changed within
220*22dc650dSSadaf Ebrahimithe pattern.
221*22dc650dSSadaf Ebrahimi<br>
222*22dc650dSSadaf Ebrahimi<br>
223*22dc650dSSadaf Ebrahimi(k) PCRE2 supports non-atomic positive lookaround assertions. This is an
224*22dc650dSSadaf Ebrahimiextension to the lookaround facilities. The default, Perl-compatible
225*22dc650dSSadaf Ebrahimilookarounds are atomic.
226*22dc650dSSadaf Ebrahimi<br>
227*22dc650dSSadaf Ebrahimi<br>
228*22dc650dSSadaf Ebrahimi(l) There are three syntactical items in patterns that can refer to a capturing
229*22dc650dSSadaf Ebrahimigroup by number: back references such as \g{2}, subroutine calls such as (?3),
230*22dc650dSSadaf Ebrahimiand condition references such as (?(4)...). PCRE2 supports relative group
231*22dc650dSSadaf Ebrahiminumbers such as +2 and -4 in all three cases. Perl supports both plus and minus
232*22dc650dSSadaf Ebrahimifor subroutine calls, but only minus for back references, and no relative
233*22dc650dSSadaf Ebrahiminumbering at all for conditions.
234*22dc650dSSadaf Ebrahimi</P>
235*22dc650dSSadaf Ebrahimi<P>
236*22dc650dSSadaf Ebrahimi20. Perl has different limits than PCRE2. See the
237*22dc650dSSadaf Ebrahimi<a href="pcre2limit.html"><b>pcre2limit</b></a>
238*22dc650dSSadaf Ebrahimidocumentation for details. Perl went with 5.10 from recursion to iteration
239*22dc650dSSadaf Ebrahimikeeping the intermediate matches on the heap, which is ~10% slower but does not
240*22dc650dSSadaf Ebrahimifall into any stack-overflow limit. PCRE2 made a similar change at release
241*22dc650dSSadaf Ebrahimi10.30, and also has many build-time and run-time customizable limits.
242*22dc650dSSadaf Ebrahimi</P>
243*22dc650dSSadaf Ebrahimi<P>
244*22dc650dSSadaf Ebrahimi21. Unlike Perl, PCRE2 doesn't have character set modifiers and specially no way
245*22dc650dSSadaf Ebrahimito set characters by context just like Perl's "/d". A regular expression using
246*22dc650dSSadaf EbrahimiPCRE2_UTF and PCRE2_UCP will use similar rules to Perl's "/u"; something closer
247*22dc650dSSadaf Ebrahimito "/a" could be selected by adding other PCRE2_EXTRA_ASCII* options on top.
248*22dc650dSSadaf Ebrahimi</P>
249*22dc650dSSadaf Ebrahimi<P>
250*22dc650dSSadaf Ebrahimi22. Some recursive patterns that Perl diagnoses as infinite recursions can be
251*22dc650dSSadaf Ebrahimihandled by PCRE2, either by the interpreter or the JIT. An example is
252*22dc650dSSadaf Ebrahimi/(?:|(?0)abcd)(?(R)|\z)/, which matches a sequence of any number of repeated
253*22dc650dSSadaf Ebrahimi"abcd" substrings at the end of the subject.
254*22dc650dSSadaf Ebrahimi</P>
255*22dc650dSSadaf Ebrahimi<br><b>
256*22dc650dSSadaf EbrahimiAUTHOR
257*22dc650dSSadaf Ebrahimi</b><br>
258*22dc650dSSadaf Ebrahimi<P>
259*22dc650dSSadaf EbrahimiPhilip Hazel
260*22dc650dSSadaf Ebrahimi<br>
261*22dc650dSSadaf EbrahimiRetired from University Computing Service
262*22dc650dSSadaf Ebrahimi<br>
263*22dc650dSSadaf EbrahimiCambridge, England.
264*22dc650dSSadaf Ebrahimi<br>
265*22dc650dSSadaf Ebrahimi</P>
266*22dc650dSSadaf Ebrahimi<br><b>
267*22dc650dSSadaf EbrahimiREVISION
268*22dc650dSSadaf Ebrahimi</b><br>
269*22dc650dSSadaf Ebrahimi<P>
270*22dc650dSSadaf EbrahimiLast updated: 30 November 2023
271*22dc650dSSadaf Ebrahimi<br>
272*22dc650dSSadaf EbrahimiCopyright &copy; 1997-2023 University of Cambridge.
273*22dc650dSSadaf Ebrahimi<br>
274*22dc650dSSadaf Ebrahimi<p>
275*22dc650dSSadaf EbrahimiReturn to the <a href="index.html">PCRE2 index page</a>.
276*22dc650dSSadaf Ebrahimi</p>
277