1PCRECOMPAT(3) Library Functions Manual PCRECOMPAT(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
9
10 This document describes the differences in the ways that PCRE and Perl
11 handle regular expressions. The differences described here are mainly
12 with respect to Perl 5.8, though PCRE versions 7.0 and later contain
13 some features that are expected to be in the forthcoming Perl 5.10.
14
15 1. PCRE has only a subset of Perl's UTF-8 and Unicode support. Details
16 of what it does have are given in the section on UTF-8 support in the
17 main pcre page.
18
19 2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl
20 permits them, but they do not mean what you might think. For example,
21 (?!a){3} does not assert that the next three characters are not "a". It
22 just asserts that the next character is not "a" three times.
23
24 3. Capturing subpatterns that occur inside negative lookahead asser‐
25 tions are counted, but their entries in the offsets vector are never
26 set. Perl sets its numerical variables from any such patterns that are
27 matched before the assertion fails to match something (thereby succeed‐
28 ing), but only if the negative lookahead assertion contains just one
29 branch.
30
31 4. Though binary zero characters are supported in the subject string,
32 they are not allowed in a pattern string because it is passed as a nor‐
33 mal C string, terminated by zero. The escape sequence \0 can be used in
34 the pattern to represent a binary zero.
35
36 5. The following Perl escape sequences are not supported: \l, \u, \L,
37 \U, and \N. In fact these are implemented by Perl's general string-han‐
38 dling and are not part of its pattern matching engine. If any of these
39 are encountered by PCRE, an error is generated.
40
41 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE
42 is built with Unicode character property support. The properties that
43 can be tested with \p and \P are limited to the general category prop‐
44 erties such as Lu and Nd, script names such as Greek or Han, and the
45 derived properties Any and L&.
46
47 7. PCRE does support the \Q...\E escape for quoting substrings. Charac‐
48 ters in between are treated as literals. This is slightly different
49 from Perl in that $ and @ are also handled as literals inside the
50 quotes. In Perl, they cause variable interpolation (but of course PCRE
51 does not have variables). Note the following examples:
52
53 Pattern PCRE matches Perl matches
54
55 \Qabc$xyz\E abc$xyz abc followed by the
56 contents of $xyz
57 \Qabc\$xyz\E abc\$xyz abc\$xyz
58 \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
59
60 The \Q...\E sequence is recognized both inside and outside character
61 classes.
62
63 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
64 constructions. However, there is support for recursive patterns. This
65 is not available in Perl 5.8, but will be in Perl 5.10. Also, the PCRE
66 "callout" feature allows an external function to be called during pat‐
67 tern matching. See the pcrecallout documentation for details.
68
69 9. Subpatterns that are called recursively or as "subroutines" are
70 always treated as atomic groups in PCRE. This is like Python, but
71 unlike Perl.
72
73 10. There are some differences that are concerned with the settings of
74 captured strings when part of a pattern is repeated. For example,
75 matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2
76 unset, but in PCRE it is set to "b".
77
78 11. PCRE does support Perl 5.10's backtracking verbs (*ACCEPT),
79 (*FAIL), (*F), (*COMMIT), (*PRUNE), (*SKIP), and (*THEN), but only in
80 the forms without an argument. PCRE does not support (*MARK). If
81 (*ACCEPT) is within capturing parentheses, PCRE does not set that cap‐
82 ture group; this is different to Perl.
83
84 12. PCRE provides some extensions to the Perl regular expression facil‐
85 ities. Perl 5.10 will include new features that are not in earlier
86 versions, some of which (such as named parentheses) have been in PCRE
87 for some time. This list is with respect to Perl 5.10:
88
89 (a) Although lookbehind assertions must match fixed length strings,
90 each alternative branch of a lookbehind assertion can match a different
91 length of string. Perl requires them all to have the same length.
92
93 (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
94 meta-character matches only at the very end of the string.
95
96 (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe‐
97 cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
98 ignored. (Perl can be made to issue a warning.)
99
100 (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti‐
101 fiers is inverted, that is, by default they are not greedy, but if fol‐
102 lowed by a question mark they are.
103
104 (e) PCRE_ANCHORED can be used at matching time to force a pattern to be
105 tried only at the first matching position in the subject string.
106
107 (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAP‐
108 TURE options for pcre_exec() have no Perl equivalents.
109
110 (g) The \R escape sequence can be restricted to match only CR, LF, or
111 CRLF by the PCRE_BSR_ANYCRLF option.
112
113 (h) The callout facility is PCRE-specific.
114
115 (i) The partial matching facility is PCRE-specific.
116
117 (j) Patterns compiled by PCRE can be saved and re-used at a later time,
118 even on different hosts that have the other endianness.
119
120 (k) The alternative matching function (pcre_dfa_exec()) matches in a
121 different way and is not Perl-compatible.
122
123 (l) PCRE recognizes some special sequences such as (*CR) at the start
124 of a pattern that set overall options that cannot be changed within the
125 pattern.
126
128
129 Philip Hazel
130 University Computing Service
131 Cambridge CB2 3QH, England.
132
134
135 Last updated: 11 September 2007
136 Copyright (c) 1997-2007 University of Cambridge.
137
138
139
140 PCRECOMPAT(3)