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 with
12 respect to Perl versions 5.10 and above.
13
14 1. PCRE has only a subset of Perl's Unicode support. Details of what it
15 does have are given in the pcreunicode page.
16
17 2. PCRE allows repeat quantifiers only on parenthesized assertions, but
18 they do not mean what you might think. For example, (?!a){3} does not
19 assert that the next three characters are not "a". It just asserts that
20 the next character is not "a" three times (in principle: PCRE optimizes
21 this to run the assertion just once). Perl allows repeat quantifiers on
22 other assertions such as \b, but these do not seem to have any use.
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 when followed by a character name or Unicode value. (\N on
38 its own, matching a non-newline character, is supported.) In fact these
39 are implemented by Perl's general string-handling and are not part of
40 its pattern matching engine. If any of these are encountered by PCRE,
41 an error is generated by default. However, if the PCRE_JAVASCRIPT_COM‐
42 PAT option is set, \U and \u are interpreted as JavaScript interprets
43 them.
44
45 6. The Perl escape sequences \p, \P, and \X are supported only if PCRE
46 is built with Unicode character property support. The properties that
47 can be tested with \p and \P are limited to the general category prop‐
48 erties such as Lu and Nd, script names such as Greek or Han, and the
49 derived properties Any and L&. PCRE does support the Cs (surrogate)
50 property, which Perl does not; the Perl documentation says "Because
51 Perl hides the need for the user to understand the internal representa‐
52 tion of Unicode characters, there is no need to implement the somewhat
53 messy concept of surrogates."
54
55 7. PCRE does support the \Q...\E escape for quoting substrings. Charac‐
56 ters in between are treated as literals. This is slightly different
57 from Perl in that $ and @ are also handled as literals inside the
58 quotes. In Perl, they cause variable interpolation (but of course PCRE
59 does not have variables). Note the following examples:
60
61 Pattern PCRE matches Perl matches
62
63 \Qabc$xyz\E abc$xyz abc followed by the
64 contents of $xyz
65 \Qabc\$xyz\E abc\$xyz abc\$xyz
66 \Qabc\E\$\Qxyz\E abc$xyz abc$xyz
67
68 The \Q...\E sequence is recognized both inside and outside character
69 classes.
70
71 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
72 constructions. However, there is support for recursive patterns. This
73 is not available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE
74 "callout" feature allows an external function to be called during pat‐
75 tern matching. See the pcrecallout documentation for details.
76
77 9. Subpatterns that are called as subroutines (whether or not recur‐
78 sively) are always treated as atomic groups in PCRE. This is like
79 Python, but unlike Perl. Captured values that are set outside a sub‐
80 routine call can be reference from inside in PCRE, but not in Perl.
81 There is a discussion that explains these differences in more detail in
82 the section on recursion differences from Perl in the pcrepattern page.
83
84 10. If any of the backtracking control verbs are used in an assertion
85 or in a subpattern that is called as a subroutine (whether or not
86 recursively), their effect is confined to that subpattern; it does not
87 extend to the surrounding pattern. This is not always the case in Perl.
88 In particular, if (*THEN) is present in a group that is called as a
89 subroutine, its action is limited to that group, even if the group does
90 not contain any | characters. There is one exception to this: the name
91 from a *(MARK), (*PRUNE), or (*THEN) that is encountered in a success‐
92 ful positive assertion is passed back when a match succeeds (compare
93 capturing parentheses in assertions). Note that such subpatterns are
94 processed as anchored at the point where they are tested.
95
96 11. There are some differences that are concerned with the settings of
97 captured strings when part of a pattern is repeated. For example,
98 matching "aba" against the pattern /^(a(b)?)+$/ in Perl leaves $2
99 unset, but in PCRE it is set to "b".
100
101 12. PCRE's handling of duplicate subpattern numbers and duplicate sub‐
102 pattern names is not as general as Perl's. This is a consequence of the
103 fact the PCRE works internally just with numbers, using an external ta‐
104 ble to translate between numbers and names. In particular, a pattern
105 such as (?|(?<a>A)|(?<b)B), where the two capturing parentheses have
106 the same number but different names, is not supported, and causes an
107 error at compile time. If it were allowed, it would not be possible to
108 distinguish which parentheses matched, because both names map to cap‐
109 turing subpattern number 1. To avoid this confusing situation, an error
110 is given at compile time.
111
112 13. Perl recognizes comments in some places that PCRE does not, for
113 example, between the ( and ? at the start of a subpattern. If the /x
114 modifier is set, Perl allows white space between ( and ? but PCRE never
115 does, even if the PCRE_EXTENDED option is set.
116
117 14. PCRE provides some extensions to the Perl regular expression facil‐
118 ities. Perl 5.10 includes new features that are not in earlier ver‐
119 sions of Perl, some of which (such as named parentheses) have been in
120 PCRE for some time. This list is with respect to Perl 5.10:
121
122 (a) Although lookbehind assertions in PCRE must match fixed length
123 strings, each alternative branch of a lookbehind assertion can match a
124 different length of string. Perl requires them all to have the same
125 length.
126
127 (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
128 meta-character matches only at the very end of the string.
129
130 (c) If PCRE_EXTRA is set, a backslash followed by a letter with no spe‐
131 cial meaning is faulted. Otherwise, like Perl, the backslash is quietly
132 ignored. (Perl can be made to issue a warning.)
133
134 (d) If PCRE_UNGREEDY is set, the greediness of the repetition quanti‐
135 fiers is inverted, that is, by default they are not greedy, but if fol‐
136 lowed by a question mark they are.
137
138 (e) PCRE_ANCHORED can be used at matching time to force a pattern to be
139 tried only at the first matching position in the subject string.
140
141 (f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART,
142 and PCRE_NO_AUTO_CAPTURE options for pcre_exec() have no Perl equiva‐
143 lents.
144
145 (g) The \R escape sequence can be restricted to match only CR, LF, or
146 CRLF by the PCRE_BSR_ANYCRLF option.
147
148 (h) The callout facility is PCRE-specific.
149
150 (i) The partial matching facility is PCRE-specific.
151
152 (j) Patterns compiled by PCRE can be saved and re-used at a later time,
153 even on different hosts that have the other endianness. However, this
154 does not apply to optimized data created by the just-in-time compiler.
155
156 (k) The alternative matching functions (pcre_dfa_exec(),
157 pcre16_dfa_exec() and pcre32_dfa_exec(),) match in a different way and
158 are not Perl-compatible.
159
160 (l) PCRE recognizes some special sequences such as (*CR) at the start
161 of a pattern that set overall options that cannot be changed within the
162 pattern.
163
165
166 Philip Hazel
167 University Computing Service
168 Cambridge CB2 3QH, England.
169
171
172 Last updated: 25 August 2012
173 Copyright (c) 1997-2012 University of Cambridge.
174
175
176
177PCRE 8.30 24 June 2012 PCRECOMPAT(3)