pcrecompat.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <html>
  2. <head>
  3. <title>pcrecompat specification</title>
  4. </head>
  5. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  6. <h1>pcrecompat man page</h1>
  7. <p>
  8. Return to the <a href="index.html">PCRE index page</a>.
  9. </p>
  10. <p>
  11. This page is part of the PCRE HTML documentation. It was generated automatically
  12. from the original man page. If there is any nonsense in it, please consult the
  13. man page, in case the conversion went wrong.
  14. <br>
  15. <br><b>
  16. DIFFERENCES BETWEEN PCRE AND PERL
  17. </b><br>
  18. <P>
  19. This document describes the differences in the ways that PCRE and Perl handle
  20. regular expressions. The differences described here are with respect to Perl
  21. versions 5.10 and above.
  22. </P>
  23. <P>
  24. 1. PCRE has only a subset of Perl's Unicode support. Details of what it does
  25. have are given in the
  26. <a href="pcreunicode.html"><b>pcreunicode</b></a>
  27. page.
  28. </P>
  29. <P>
  30. 2. PCRE allows repeat quantifiers only on parenthesized assertions, but they do
  31. not mean what you might think. For example, (?!a){3} does not assert that the
  32. next three characters are not "a". It just asserts that the next character is
  33. not "a" three times (in principle: PCRE optimizes this to run the assertion
  34. just once). Perl allows repeat quantifiers on other assertions such as \b, but
  35. these do not seem to have any use.
  36. </P>
  37. <P>
  38. 3. Capturing subpatterns that occur inside negative lookahead assertions are
  39. counted, but their entries in the offsets vector are never set. Perl sometimes
  40. (but not always) sets its numerical variables from inside negative assertions.
  41. </P>
  42. <P>
  43. 4. Though binary zero characters are supported in the subject string, they are
  44. not allowed in a pattern string because it is passed as a normal C string,
  45. terminated by zero. The escape sequence \0 can be used in the pattern to
  46. represent a binary zero.
  47. </P>
  48. <P>
  49. 5. The following Perl escape sequences are not supported: \l, \u, \L,
  50. \U, and \N when followed by a character name or Unicode value. (\N on its
  51. own, matching a non-newline character, is supported.) In fact these are
  52. implemented by Perl's general string-handling and are not part of its pattern
  53. matching engine. If any of these are encountered by PCRE, an error is
  54. generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
  55. \U and \u are interpreted as JavaScript interprets them.
  56. </P>
  57. <P>
  58. 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE is
  59. built with Unicode character property support. The properties that can be
  60. tested with \p and \P are limited to the general category properties such as
  61. Lu and Nd, script names such as Greek or Han, and the derived properties Any
  62. and L&. PCRE does support the Cs (surrogate) property, which Perl does not; the
  63. Perl documentation says "Because Perl hides the need for the user to understand
  64. the internal representation of Unicode characters, there is no need to
  65. implement the somewhat messy concept of surrogates."
  66. </P>
  67. <P>
  68. 7. PCRE does support the \Q...\E escape for quoting substrings. Characters in
  69. between are treated as literals. This is slightly different from Perl in that $
  70. and @ are also handled as literals inside the quotes. In Perl, they cause
  71. variable interpolation (but of course PCRE does not have variables). Note the
  72. following examples:
  73. <pre>
  74. Pattern PCRE matches Perl matches
  75. \Qabc$xyz\E abc$xyz abc followed by the contents of $xyz
  76. \Qabc\$xyz\E abc\$xyz abc\$xyz
  77. \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
  78. </pre>
  79. The \Q...\E sequence is recognized both inside and outside character classes.
  80. </P>
  81. <P>
  82. 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
  83. constructions. However, there is support for recursive patterns. This is not
  84. available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout"
  85. feature allows an external function to be called during pattern matching. See
  86. the
  87. <a href="pcrecallout.html"><b>pcrecallout</b></a>
  88. documentation for details.
  89. </P>
  90. <P>
  91. 9. Subpatterns that are called as subroutines (whether or not recursively) are
  92. always treated as atomic groups in PCRE. This is like Python, but unlike Perl.
  93. Captured values that are set outside a subroutine call can be reference from
  94. inside in PCRE, but not in Perl. There is a discussion that explains these
  95. differences in more detail in the
  96. <a href="pcrepattern.html#recursiondifference">section on recursion differences from Perl</a>
  97. in the
  98. <a href="pcrepattern.html"><b>pcrepattern</b></a>
  99. page.
  100. </P>
  101. <P>
  102. 10. If any of the backtracking control verbs are used in a subpattern that is
  103. called as a subroutine (whether or not recursively), their effect is confined
  104. to that subpattern; it does not extend to the surrounding pattern. This is not
  105. always the case in Perl. In particular, if (*THEN) is present in a group that
  106. is called as a subroutine, its action is limited to that group, even if the
  107. group does not contain any | characters. Note that such subpatterns are
  108. processed as anchored at the point where they are tested.
  109. </P>
  110. <P>
  111. 11. 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 PCRE, but there are examples where it differs.
  116. </P>
  117. <P>
  118. 12. Most backtracking verbs in assertions have their normal actions. They are
  119. not confined to the assertion.
  120. </P>
  121. <P>
  122. 13. There are some differences that are concerned with the settings of captured
  123. strings when part of a pattern is repeated. For example, matching "aba" against
  124. the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b".
  125. </P>
  126. <P>
  127. 14. PCRE's handling of duplicate subpattern numbers and duplicate subpattern
  128. names is not as general as Perl's. This is a consequence of the fact the PCRE
  129. works internally just with numbers, using an external table to translate
  130. between numbers and names. In particular, a pattern such as (?|(?&#60;a&#62;A)|(?&#60;b&#62;B),
  131. where the two capturing parentheses have the same number but different names,
  132. is not supported, and causes an error at compile time. If it were allowed, it
  133. would not be possible to distinguish which parentheses matched, because both
  134. names map to capturing subpattern number 1. To avoid this confusing situation,
  135. an error is given at compile time.
  136. </P>
  137. <P>
  138. 15. Perl recognizes comments in some places that PCRE does not, for example,
  139. between the ( and ? at the start of a subpattern. If the /x modifier is set,
  140. Perl allows white space between ( and ? (though current Perls warn that this is
  141. deprecated) but PCRE never does, even if the PCRE_EXTENDED option is set.
  142. </P>
  143. <P>
  144. 16. Perl, when in warning mode, gives warnings for character classes such as
  145. [A-\d] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE has no
  146. warning features, so it gives an error in these cases because they are almost
  147. certainly user mistakes.
  148. </P>
  149. <P>
  150. 17. In PCRE, the upper/lower case character properties Lu and Ll are not
  151. affected when case-independent matching is specified. For example, \p{Lu}
  152. always matches an upper case letter. I think Perl has changed in this respect;
  153. in the release at the time of writing (5.16), \p{Lu} and \p{Ll} match all
  154. letters, regardless of case, when case independence is specified.
  155. </P>
  156. <P>
  157. 18. PCRE provides some extensions to the Perl regular expression facilities.
  158. Perl 5.10 includes new features that are not in earlier versions of Perl, some
  159. of which (such as named parentheses) have been in PCRE for some time. This list
  160. is with respect to Perl 5.10:
  161. <br>
  162. <br>
  163. (a) Although lookbehind assertions in PCRE must match fixed length strings,
  164. each alternative branch of a lookbehind assertion can match a different length
  165. of string. Perl requires them all to have the same length.
  166. <br>
  167. <br>
  168. (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
  169. meta-character matches only at the very end of the string.
  170. <br>
  171. <br>
  172. (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
  173. meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored.
  174. (Perl can be made to issue a warning.)
  175. <br>
  176. <br>
  177. (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
  178. inverted, that is, by default they are not greedy, but if followed by a
  179. question mark they are.
  180. <br>
  181. <br>
  182. (e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried
  183. only at the first matching position in the subject string.
  184. <br>
  185. <br>
  186. (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, and
  187. PCRE_NO_AUTO_CAPTURE options for <b>pcre_exec()</b> have no Perl equivalents.
  188. <br>
  189. <br>
  190. (g) The \R escape sequence can be restricted to match only CR, LF, or CRLF
  191. by the PCRE_BSR_ANYCRLF option.
  192. <br>
  193. <br>
  194. (h) The callout facility is PCRE-specific.
  195. <br>
  196. <br>
  197. (i) The partial matching facility is PCRE-specific.
  198. <br>
  199. <br>
  200. (j) Patterns compiled by PCRE can be saved and re-used at a later time, even on
  201. different hosts that have the other endianness. However, this does not apply to
  202. optimized data created by the just-in-time compiler.
  203. <br>
  204. <br>
  205. (k) The alternative matching functions (<b>pcre_dfa_exec()</b>,
  206. <b>pcre16_dfa_exec()</b> and <b>pcre32_dfa_exec()</b>,) match in a different way
  207. and are not Perl-compatible.
  208. <br>
  209. <br>
  210. (l) PCRE recognizes some special sequences such as (*CR) at the start of
  211. a pattern that set overall options that cannot be changed within the pattern.
  212. </P>
  213. <br><b>
  214. AUTHOR
  215. </b><br>
  216. <P>
  217. Philip Hazel
  218. <br>
  219. University Computing Service
  220. <br>
  221. Cambridge CB2 3QH, England.
  222. <br>
  223. </P>
  224. <br><b>
  225. REVISION
  226. </b><br>
  227. <P>
  228. Last updated: 10 November 2013
  229. <br>
  230. Copyright &copy; 1997-2013 University of Cambridge.
  231. <br>
  232. <p>
  233. Return to the <a href="index.html">PCRE index page</a>.
  234. </p>