github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
12 KiB

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