pcre2compat.3 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. .TH PCRE2COMPAT 3 "06 October 2020" "PCRE2 10.36"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "DIFFERENCES BETWEEN PCRE2 AND PERL"
  5. .rs
  6. .sp
  7. This document describes some of the differences in the ways that PCRE2 and Perl
  8. handle regular expressions. The differences described here are with respect to
  9. Perl version 5.32.0, but as both Perl and PCRE2 are continually changing, the
  10. information may at times be out of date.
  11. .P
  12. 1. PCRE2 has only a subset of Perl's Unicode support. Details of what it does
  13. have are given in the
  14. .\" HREF
  15. \fBpcre2unicode\fP
  16. .\"
  17. page.
  18. .P
  19. 2. Like Perl, PCRE2 allows repeat quantifiers on parenthesized assertions, but
  20. they do not mean what you might think. For example, (?!a){3} does not assert
  21. that the next three characters are not "a". It just asserts that the next
  22. character is not "a" three times (in principle; PCRE2 optimizes this to run the
  23. assertion just once). Perl allows some repeat quantifiers on other assertions,
  24. for example, \eb* (but not \eb{3}, though oddly it does allow ^{3}), but these
  25. do not seem to have any use. PCRE2 does not allow any kind of quantifier on
  26. non-lookaround assertions.
  27. .P
  28. 3. Capture groups that occur inside negative lookaround assertions are counted,
  29. but their entries in the offsets vector are set only when a negative assertion
  30. is a condition that has a matching branch (that is, the condition is false).
  31. Perl may set such capture groups in other circumstances.
  32. .P
  33. 4. The following Perl escape sequences are not supported: \eF, \el, \eL, \eu,
  34. \eU, and \eN when followed by a character name. \eN on its own, matching a
  35. non-newline character, and \eN{U+dd..}, matching a Unicode code point, are
  36. supported. The escapes that modify the case of following letters are
  37. implemented by Perl's general string-handling and are not part of its pattern
  38. matching engine. If any of these are encountered by PCRE2, an error is
  39. generated by default. However, if either of the PCRE2_ALT_BSUX or
  40. PCRE2_EXTRA_ALT_BSUX options is set, \eU and \eu are interpreted as ECMAScript
  41. interprets them.
  42. .P
  43. 5. The Perl escape sequences \ep, \eP, and \eX are supported only if PCRE2 is
  44. built with Unicode support (the default). The properties that can be tested
  45. with \ep and \eP are limited to the general category properties such as Lu and
  46. Nd, script names such as Greek or Han, and the derived properties Any and L&.
  47. Both PCRE2 and Perl support the Cs (surrogate) property, but in PCRE2 its use
  48. is limited. See the
  49. .\" HREF
  50. \fBpcre2pattern\fP
  51. .\"
  52. documentation for details. The long synonyms for property names that Perl
  53. supports (such as \ep{Letter}) are not supported by PCRE2, nor is it permitted
  54. to prefix any of these properties with "Is".
  55. .P
  56. 6. PCRE2 supports the \eQ...\eE escape for quoting substrings. Characters
  57. in between are treated as literals. However, this is slightly different from
  58. Perl in that $ and @ are also handled as literals inside the quotes. In Perl,
  59. they cause variable interpolation (but of course PCRE2 does not have
  60. variables). Also, Perl does "double-quotish backslash interpolation" on any
  61. backslashes between \eQ and \eE which, its documentation says, "may lead to
  62. confusing results". PCRE2 treats a backslash between \eQ and \eE just like any
  63. other character. Note the following examples:
  64. .sp
  65. Pattern PCRE2 matches Perl matches
  66. .sp
  67. .\" JOIN
  68. \eQabc$xyz\eE abc$xyz abc followed by the
  69. contents of $xyz
  70. \eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
  71. \eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
  72. \eQA\eB\eE A\eB A\eB
  73. \eQ\e\eE \e \e\eE
  74. .sp
  75. The \eQ...\eE sequence is recognized both inside and outside character classes
  76. by both PCRE2 and Perl.
  77. .P
  78. 7. Fairly obviously, PCRE2 does not support the (?{code}) and (??{code})
  79. constructions. However, PCRE2 does have a "callout" feature, which allows an
  80. external function to be called during pattern matching. See the
  81. .\" HREF
  82. \fBpcre2callout\fP
  83. .\"
  84. documentation for details.
  85. .P
  86. 8. Subroutine calls (whether recursive or not) were treated as atomic groups up
  87. to PCRE2 release 10.23, but from release 10.30 this changed, and backtracking
  88. into subroutine calls is now supported, as in Perl.
  89. .P
  90. 9. In PCRE2, if any of the backtracking control verbs are used in a group that
  91. is called as a subroutine (whether or not recursively), their effect is
  92. confined to that group; it does not extend to the surrounding pattern. This is
  93. not always the case in Perl. In particular, if (*THEN) is present in a group
  94. that is called as a subroutine, its action is limited to that group, even if
  95. the group does not contain any | characters. Note that such groups are
  96. processed as anchored at the point where they are tested.
  97. .P
  98. 10. If a pattern contains more than one backtracking control verb, the first
  99. one that is backtracked onto acts. For example, in the pattern
  100. A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
  101. triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
  102. same as PCRE2, but there are cases where it differs.
  103. .P
  104. 11. There are some differences that are concerned with the settings of captured
  105. strings when part of a pattern is repeated. For example, matching "aba" against
  106. the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE2 it is set to
  107. "b".
  108. .P
  109. 12. PCRE2's handling of duplicate capture group numbers and names is not as
  110. general as Perl's. This is a consequence of the fact the PCRE2 works internally
  111. just with numbers, using an external table to translate between numbers and
  112. names. In particular, a pattern such as (?|(?<a>A)|(?<b>B)), where the two
  113. capture groups have the same number but different names, is not supported, and
  114. causes an error at compile time. If it were allowed, it would not be possible
  115. to distinguish which group matched, because both names map to capture group
  116. number 1. To avoid this confusing situation, an error is given at compile time.
  117. .P
  118. 13. Perl used to recognize comments in some places that PCRE2 does not, for
  119. example, between the ( and ? at the start of a group. If the /x modifier is
  120. set, Perl allowed white space between ( and ? though the latest Perls give an
  121. error (for a while it was just deprecated). There may still be some cases where
  122. Perl behaves differently.
  123. .P
  124. 14. Perl, when in warning mode, gives warnings for character classes such as
  125. [A-\ed] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE2 has no
  126. warning features, so it gives an error in these cases because they are almost
  127. certainly user mistakes.
  128. .P
  129. 15. In PCRE2, the upper/lower case character properties Lu and Ll are not
  130. affected when case-independent matching is specified. For example, \ep{Lu}
  131. always matches an upper case letter. I think Perl has changed in this respect;
  132. in the release at the time of writing (5.32), \ep{Lu} and \ep{Ll} match all
  133. letters, regardless of case, when case independence is specified.
  134. .P
  135. 16. From release 5.32.0, Perl locks out the use of \eK in lookaround
  136. assertions. In PCRE2, \eK is acted on when it occurs in positive assertions,
  137. but is ignored in negative assertions.
  138. .P
  139. 17. PCRE2 provides some extensions to the Perl regular expression facilities.
  140. Perl 5.10 included new features that were not in earlier versions of Perl, some
  141. of which (such as named parentheses) were in PCRE2 for some time before. This
  142. list is with respect to Perl 5.32:
  143. .sp
  144. (a) Although lookbehind assertions in PCRE2 must match fixed length strings,
  145. each alternative toplevel branch of a lookbehind assertion can match a
  146. different length of string. Perl requires them all to have the same length.
  147. .sp
  148. (b) From PCRE2 10.23, backreferences to groups of fixed length are supported
  149. in lookbehinds, provided that there is no possibility of referencing a
  150. non-unique number or name. Perl does not support backreferences in lookbehinds.
  151. .sp
  152. (c) If PCRE2_DOLLAR_ENDONLY is set and PCRE2_MULTILINE is not set, the $
  153. meta-character matches only at the very end of the string.
  154. .sp
  155. (d) A backslash followed by a letter with no special meaning is faulted. (Perl
  156. can be made to issue a warning.)
  157. .sp
  158. (e) If PCRE2_UNGREEDY is set, the greediness of the repetition quantifiers is
  159. inverted, that is, by default they are not greedy, but if followed by a
  160. question mark they are.
  161. .sp
  162. (f) PCRE2_ANCHORED can be used at matching time to force a pattern to be tried
  163. only at the first matching position in the subject string.
  164. .sp
  165. (g) The PCRE2_NOTBOL, PCRE2_NOTEOL, PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART
  166. options have no Perl equivalents.
  167. .sp
  168. (h) The \eR escape sequence can be restricted to match only CR, LF, or CRLF
  169. by the PCRE2_BSR_ANYCRLF option.
  170. .sp
  171. (i) The callout facility is PCRE2-specific. Perl supports codeblocks and
  172. variable interpolation, but not general hooks on every match.
  173. .sp
  174. (j) The partial matching facility is PCRE2-specific.
  175. .sp
  176. (k) The alternative matching function (\fBpcre2_dfa_match()\fP matches in a
  177. different way and is not Perl-compatible.
  178. .sp
  179. (l) PCRE2 recognizes some special sequences such as (*CR) or (*NO_JIT) at
  180. the start of a pattern. These set overall options that cannot be changed within
  181. the pattern.
  182. .sp
  183. (m) PCRE2 supports non-atomic positive lookaround assertions. This is an
  184. extension to the lookaround facilities. The default, Perl-compatible
  185. lookarounds are atomic.
  186. .P
  187. 18. The Perl /a modifier restricts /d numbers to pure ascii, and the /aa
  188. modifier restricts /i case-insensitive matching to pure ascii, ignoring Unicode
  189. rules. This separation cannot be represented with PCRE2_UCP.
  190. .P
  191. 19. Perl has different limits than PCRE2. See the
  192. .\" HREF
  193. \fBpcre2limit\fP
  194. .\"
  195. documentation for details. Perl went with 5.10 from recursion to iteration
  196. keeping the intermediate matches on the heap, which is ~10% slower but does not
  197. fall into any stack-overflow limit. PCRE2 made a similar change at release
  198. 10.30, and also has many build-time and run-time customizable limits.
  199. .
  200. .
  201. .SH AUTHOR
  202. .rs
  203. .sp
  204. .nf
  205. Philip Hazel
  206. University Computing Service
  207. Cambridge, England.
  208. .fi
  209. .
  210. .
  211. .SH REVISION
  212. .rs
  213. .sp
  214. .nf
  215. Last updated: 06 October 2020
  216. Copyright (c) 1997-2019 University of Cambridge.
  217. .fi