blob: 64d3dbc67edc86ae44fb7ed7938be7bf9ccfac7d [file] [log] [blame]
Tristan Matthews04616462013-11-14 16:09:34 -05001.TH PCRECOMPAT 3
2.SH NAME
3PCRE - Perl-compatible regular expressions
4.SH "DIFFERENCES BETWEEN PCRE AND PERL"
5.rs
6.sp
7This document describes the differences in the ways that PCRE and Perl handle
8regular expressions. The differences described here are with respect to Perl
9versions 5.10 and above.
10.P
111. PCRE has only a subset of Perl's UTF-8 and Unicode support. Details of what
12it does have are given in the
13.\" HREF
14\fBpcreunicode\fP
15.\"
16page.
17.P
182. PCRE allows repeat quantifiers only on parenthesized assertions, but they do
19not mean what you might think. For example, (?!a){3} does not assert that the
20next three characters are not "a". It just asserts that the next character is
21not "a" three times (in principle: PCRE optimizes this to run the assertion
22just once). Perl allows repeat quantifiers on other assertions such as \eb, but
23these do not seem to have any use.
24.P
253. Capturing subpatterns that occur inside negative lookahead assertions are
26counted, but their entries in the offsets vector are never set. Perl sets its
27numerical variables from any such patterns that are matched before the
28assertion fails to match something (thereby succeeding), but only if the
29negative lookahead assertion contains just one branch.
30.P
314. Though binary zero characters are supported in the subject string, they are
32not allowed in a pattern string because it is passed as a normal C string,
33terminated by zero. The escape sequence \e0 can be used in the pattern to
34represent a binary zero.
35.P
365. The following Perl escape sequences are not supported: \el, \eu, \eL,
37\eU, and \eN when followed by a character name or Unicode value. (\eN on its
38own, matching a non-newline character, is supported.) In fact these are
39implemented by Perl's general string-handling and are not part of its pattern
40matching engine. If any of these are encountered by PCRE, an error is
41generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
42\eU and \eu are interpreted as JavaScript interprets them.
43.P
446. The Perl escape sequences \ep, \eP, and \eX are supported only if PCRE is
45built with Unicode character property support. The properties that can be
46tested with \ep and \eP are limited to the general category properties such as
47Lu and Nd, script names such as Greek or Han, and the derived properties Any
48and L&. PCRE does support the Cs (surrogate) property, which Perl does not; the
49Perl documentation says "Because Perl hides the need for the user to understand
50the internal representation of Unicode characters, there is no need to
51implement the somewhat messy concept of surrogates."
52.P
537. PCRE implements a simpler version of \eX than Perl, which changed to make
54\eX match what Unicode calls an "extended grapheme cluster". This is more
55complicated than an extended Unicode sequence, which is what PCRE matches.
56.P
578. PCRE does support the \eQ...\eE escape for quoting substrings. Characters in
58between are treated as literals. This is slightly different from Perl in that $
59and @ are also handled as literals inside the quotes. In Perl, they cause
60variable interpolation (but of course PCRE does not have variables). Note the
61following examples:
62.sp
63 Pattern PCRE matches Perl matches
64.sp
65.\" JOIN
66 \eQabc$xyz\eE abc$xyz abc followed by the
67 contents of $xyz
68 \eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
69 \eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
70.sp
71The \eQ...\eE sequence is recognized both inside and outside character classes.
72.P
739. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
74constructions. However, there is support for recursive patterns. This is not
75available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout"
76feature allows an external function to be called during pattern matching. See
77the
78.\" HREF
79\fBpcrecallout\fP
80.\"
81documentation for details.
82.P
8310. Subpatterns that are called as subroutines (whether or not recursively) are
84always treated as atomic groups in PCRE. This is like Python, but unlike Perl.
85Captured values that are set outside a subroutine call can be reference from
86inside in PCRE, but not in Perl. There is a discussion that explains these
87differences in more detail in the
88.\" HTML <a href="pcrepattern.html#recursiondifference">
89.\" </a>
90section on recursion differences from Perl
91.\"
92in the
93.\" HREF
94\fBpcrepattern\fP
95.\"
96page.
97.P
9811. If (*THEN) is present in a group that is called as a subroutine, its action
99is limited to that group, even if the group does not contain any | characters.
100.P
10112. There are some differences that are concerned with the settings of captured
102strings when part of a pattern is repeated. For example, matching "aba" against
103the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b".
104.P
10513. PCRE's handling of duplicate subpattern numbers and duplicate subpattern
106names is not as general as Perl's. This is a consequence of the fact the PCRE
107works internally just with numbers, using an external table to translate
108between numbers and names. In particular, a pattern such as (?|(?<a>A)|(?<b)B),
109where the two capturing parentheses have the same number but different names,
110is not supported, and causes an error at compile time. If it were allowed, it
111would not be possible to distinguish which parentheses matched, because both
112names map to capturing subpattern number 1. To avoid this confusing situation,
113an error is given at compile time.
114.P
11514. Perl recognizes comments in some places that PCRE does not, for example,
116between the ( and ? at the start of a subpattern. If the /x modifier is set,
117Perl allows whitespace between ( and ? but PCRE never does, even if the
118PCRE_EXTENDED option is set.
119.P
12015. PCRE provides some extensions to the Perl regular expression facilities.
121Perl 5.10 includes new features that are not in earlier versions of Perl, some
122of which (such as named parentheses) have been in PCRE for some time. This list
123is with respect to Perl 5.10:
124.sp
125(a) Although lookbehind assertions in PCRE must match fixed length strings,
126each alternative branch of a lookbehind assertion can match a different length
127of string. Perl requires them all to have the same length.
128.sp
129(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
130meta-character matches only at the very end of the string.
131.sp
132(c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
133meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored.
134(Perl can be made to issue a warning.)
135.sp
136(d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
137inverted, that is, by default they are not greedy, but if followed by a
138question mark they are.
139.sp
140(e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried
141only at the first matching position in the subject string.
142.sp
143(f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, and
144PCRE_NO_AUTO_CAPTURE options for \fBpcre_exec()\fP have no Perl equivalents.
145.sp
146(g) The \eR escape sequence can be restricted to match only CR, LF, or CRLF
147by the PCRE_BSR_ANYCRLF option.
148.sp
149(h) The callout facility is PCRE-specific.
150.sp
151(i) The partial matching facility is PCRE-specific.
152.sp
153(j) Patterns compiled by PCRE can be saved and re-used at a later time, even on
154different hosts that have the other endianness. However, this does not apply to
155optimized data created by the just-in-time compiler.
156.sp
157(k) The alternative matching function (\fBpcre_dfa_exec()\fP) matches in a
158different way and is not Perl-compatible.
159.sp
160(l) PCRE recognizes some special sequences such as (*CR) at the start of
161a pattern that set overall options that cannot be changed within the pattern.
162.
163.
164.SH AUTHOR
165.rs
166.sp
167.nf
168Philip Hazel
169University Computing Service
170Cambridge CB2 3QH, England.
171.fi
172.
173.
174.SH REVISION
175.rs
176.sp
177.nf
178Last updated: 14 November 2011
179Copyright (c) 1997-2011 University of Cambridge.
180.fi