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