pcre2compat.html 11 KB

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