pcre2compat
Name
pcre2compat - PCRE2 compatibility
Differences between PCRE2 and Perl
This document describes some of the known differences in the ways that PCRE2 and Perl handle regular expressions. The differences described here are with respect to Perl version 5.38.0, but as both Perl and PCRE2 are continually changing, the information may at times be out of date.
When
PCRE2_DOTALL(equivalent to Perl’s/squalifier) is not set, the behaviour of the.metacharacter differs from Perl. In PCRE2,.matches the next character unless it is the start of a newline sequence. This means that, if the newline setting is CR, CRLF, or NUL,.will match the code point LF (0x0A) in ASCII/Unicode environments, and NL (either0x15or0x25) when using EBCDIC. In Perl,.appears never to match LF, even when0x0Ais not a newline indicator.PCRE2 has only a subset of Perl’s Unicode support. Details of what it does have are given in the
pcre2unicodepage.Like Perl, PCRE2 allows repeat quantifiers on parenthesized assertions, but they do not mean what you might think. For example,
(?!a){3}does not assert that the next three characters are nota. It just asserts that the next character is notathree times (in principle; PCRE2 optimizes this to run the assertion just once). Perl allows some repeat quantifiers on other assertions, for example,\b*, but these do not seem to have any use. PCRE2 does not allow any kind of quantifier on non-lookaround assertions.If a braced quantifier such as
{1,2}appears where there is nothing to repeat (for example, at the start of a branch), PCRE2 raises an error whereas Perl treats the quantifier characters as literal. When a braced quantifier(...){min,max}has min > max, Perl treats it as an item which fails to match any portion of the subject (as no number of repetitions can meet the condition), and additionally issues a warning when in warning mode. PCRE2 has no warning features, so it gives an error in this case.Capture groups that occur inside negative lookaround assertions are counted, but their entries in the offsets vector are set only when a negative assertion is a condition that has a matching branch (that is, the condition is false). Perl may set such capture groups in other circumstances.
The following Perl escape sequences are not supported:
\F,\l,\L,\u,\U, and\Nwhen followed by a character name.\Non its own, matching a non-newline character, and\N{U+dd...}, matching a Unicode code point, are supported. The escapes that modify the case of following letters are implemented by Perl’s general string-handling and are not part of its pattern matching engine. If any of these are encountered by PCRE2, an error is generated by default. However, if either of thePCRE2_ALT_BSUXorPCRE2_EXTRA_ALT_BSUXoptions is set,\Uand\uare interpreted as ECMAScript interprets them.The Perl escape sequences
\p,\P, and\Xare supported only if PCRE2 is built with Unicode support (the default). The properties that can be tested with\pand\Pare limited to the general category properties such asLuandNd, the derived propertiesAnyandLc(synonymL&), script names such asGreekorHan,Bidi_Class,Bidi_Control, and a few binary properties. Both PCRE2 and Perl support theCs(surrogate) property, but in PCRE2 its use is limited. See thepcre2patterndocumentation for details. The long synonyms for property names that Perl supports (such as\p{Letter}) are not supported by PCRE2, nor is it permitted to prefix any of these properties withIs.PCRE2 supports the
\Q...\Eescape for quoting substrings. Characters in between are treated as literals. However, this is slightly different from Perl in that$and@are also handled as literals inside the quotes. In Perl, they cause variable interpolation (PCRE2 does not have variables). Also, Perl does "double-quotish backslash interpolation" on any backslashes between\Qand\Ewhich, its documentation says, "may lead to confusing results". PCRE2 treats a backslash between\Qand\Ejust like any other character. Note the following examples:Pattern PCRE2 matches Perl matches \Qabc$xyz\Eabc$xyzabcfollowed by the contents of$xyz\Qabc\$xyz\Eabc\$xyzabc\$xyz\Qabc\E\$\Qxyz\Eabc$xyzabc$xyz\QA\B\EA\BA\B\Q\\E\\\EThe
\Q...\Esequence is recognized both inside and outside character classes by both PCRE2 and Perl. Another difference from Perl is that any appearance of\Qor\Einside what might otherwise be a quantifier causes PCRE2 not to recognize the sequence as a quantifier. Perl recognizes a quantifier if (redundantly) either of the numbers is inside\Q...\E, but not if the separating comma is. When not recognized as a quantifier a sequence such as{\Q1\E,2}is treated as the literal string{1,2}.Fairly obviously, PCRE2 does not support the
(?{code})and(??{code})constructions. However, PCRE2 does have a "callout" feature, which allows an external function to be called during pattern matching. See thepcre2calloutdocumentation for details.Subroutine calls (whether recursive or not) were treated as atomic groups up to PCRE2 release 10.23, but from release 10.30 this changed, and backtracking into subroutine calls is now supported, as in Perl.
In PCRE2, if any of the backtracking control verbs are used in a group that is called as a subroutine (whether or not recursively), their effect is confined to that group; it does not extend to the surrounding pattern. This is not always the case in Perl. In particular, if
(*THEN)is present in a group that is called as a subroutine, its action is limited to that group, even if the group does not contain any|characters. Note that such groups are processed as anchored at the point where they are tested. PCRE2 also confines all control verbs within atomic assertions, again including(*THEN)in assertions with only one branch.If a pattern contains more than one backtracking control verb, the first one that is backtracked onto acts. For example, in the pattern
A(*COMMIT)B(*PRUNE)Ca failure inBtriggers(*COMMIT), but a failure inCtriggers(*PRUNE). Perl’s behaviour is more complex; in many cases it is the same as PCRE2, but there are cases where it differs.There are some differences that are concerned with the settings of captured strings when part of a pattern is repeated. For example, matching
abaagainst the pattern^(a(b)?)+$in Perl leaves$2unset, but in PCRE2 it is set tob.PCRE2’s handling of duplicate capture group numbers and names is not as general as Perl’s. This is a consequence of the fact the PCRE2 works internally just with numbers, using an external table to translate between numbers and names. In particular, a pattern such as
(?|(?<a>A)|(?<b>B)), where the two capture groups have the same number but different names, is not supported, and causes an error at compile time. If it were allowed, it would not be possible to distinguish which group matched, because both names map to capture group number 1. To avoid this confusing situation, an error is given at compile time.Perl used to recognize comments in some places that PCRE2 does not, for example, between the
(and?at the start of a group. If the/xmodifier is set, Perl allowed white space between(and?though the latest Perls give an error (for a while it was just deprecated). There may still be some cases where Perl behaves differently.Perl, when in warning mode, gives warnings for character classes such as
[A-\d]or[a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no warning features, so it gives an error in these cases because they are almost certainly user mistakes.In PCRE2, until release 10.45, the upper/lower case character properties
LuandLlwere not affected when case-independent matching was specified. Perl has changed in this respect, and PCRE2 has now changed to match. When caseless matching is in force,Lu,Ll, andLt(title case) are all treated asLc(cased letter).From release 5.32.0, Perl locks out the use of
\Kin lookaround assertions. From release 10.38 PCRE2 does the same by default. However, there is an option for re-enabling the previous behaviour. When this option is set,\Kis acted on when it occurs in positive assertions, but is ignored in negative assertions.PCRE2 provides some extensions to the Perl regular expression facilities. Perl 5.10 included new features that were not in earlier versions of Perl, some of which (such as named parentheses) were in PCRE2 for some time before. This list is with respect to Perl 5.38:
If
PCRE2_DOLLAR_ENDONLYis set andPCRE2_MULTILINEis not set, the$meta-character matches only at the very end of the string.A backslash followed by a letter with no special meaning is faulted. (Perl can be made to issue a warning.)
If
PCRE2_UNGREEDYis set, the greediness of the repetition quantifiers is inverted, that is, by default they are not greedy, but if followed by a question mark they are.PCRE2_ANCHOREDcan be used at matching time to force a pattern to be tried only at the first matching position in the subject string.The
PCRE2_NOTBOL,PCRE2_NOTEOL,PCRE2_NOTEMPTYandPCRE2_NOTEMPTY_ATSTARToptions have no Perl equivalents.The
\Rescape sequence can be restricted to match only CR, LF, or CRLF by thePCRE2_BSR_ANYCRLFoption.The callout facility is PCRE2-specific. Perl supports codeblocks and variable interpolation, but not general hooks on every match.
The partial matching facility is PCRE2-specific.
The alternative matching function (
pcre2_dfa_match()) matches in a different way and is not Perl-compatible.PCRE2 recognizes some special sequences such as
(*CR)or(*NO_JIT)at the start of a pattern. These set overall options that cannot be changed within the pattern.PCRE2 supports non-atomic positive lookaround assertions. This is an extension to the lookaround facilities. The default, Perl-compatible lookarounds are atomic.
There are three syntactical items in patterns that can refer to a capturing group by number: back references such as
\g{2}, subroutine calls such as(?3), and condition references such as(?(4)...). PCRE2 supports relative group numbers such as+2and-4in all three cases. Perl supports both plus and minus for subroutine calls, but only minus for back references, and no relative numbering at all for conditions.The scan substring assertion (syntax
(*scs:(n)...)) is a PCRE2 extension that is not available in Perl.
Perl has different limits than PCRE2. See the
pcre2limitsdocumentation for details. Perl went with 5.10 from recursion to iteration keeping the intermediate matches on the heap, which is ~10% slower but does not fall into any stack-overflow limit. PCRE2 made a similar change at release 10.30, and also has many build-time and run-time customizable limits.Unlike Perl, PCRE2 doesn’t have character set modifiers and specially no way to set characters by context just like Perl’s
/d. A regular expression usingPCRE2_UTFandPCRE2_UCPwill use similar rules to Perl’s/u; something closer to/acould be selected by adding otherPCRE2_EXTRA_ASCII*options on top.Some recursive patterns that Perl diagnoses as infinite recursions can be handled by PCRE2, either by the interpreter or the JIT. An example is
(?:|(?0)abcd)(?(R)|\z), which matches a sequence of any number of repeatedabcdsubstrings at the end of the subject.Both PCRE2 and Perl error when
\x{escapes are invalid, but Perl tries to recover and prints a warning if the problem was that an invalid hexadecimal digit was found. Since PCRE2 doesn’t have warnings it returns an error instead. Additionally, Perl accepts\x{}and matches NUL unlike PCRE2.From release 10.45, PCRE2 gives an error if
\xis not followed by a hexadecimal digit or a curly bracket. It used to interpret this as the NUL character. Perl still matches NUL, but warns when in warning mode in most cases.
Author
Philip Hazel
Retired from University Computing Service
Cambridge, England.
Revision
Last updated: 02 June 2025
Copyright © 1997-2024 University of Cambridge.