ViewVC logotype

Contents of /code/trunk/doc/pcrecompat.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1404 - (hide annotations) (download)
Tue Nov 19 15:36:57 2013 UTC (17 months ago) by ph10
File size: 9178 byte(s)
Source tidies for 8.34-RC1.

1 ph10 1396 .TH PCRECOMPAT 3 "10 November 2013" "PCRE 8.34"
2 nigel 63 .SH NAME
3     PCRE - Perl-compatible regular expressions
5 nigel 63 .rs
6     .sp
7     This document describes the differences in the ways that PCRE and Perl handle
8 ph10 456 regular expressions. The differences described here are with respect to Perl
9 ph10 562 versions 5.10 and above.
10 nigel 75 .P
11 ph10 858 1. PCRE has only a subset of Perl's Unicode support. Details of what it does
12     have are given in the
13 nigel 73 .\" HREF
14 ph10 678 \fBpcreunicode\fP
15 nigel 73 .\"
16     page.
17 nigel 75 .P
18 ph10 637 2. PCRE allows repeat quantifiers only on parenthesized assertions, but they do
19     not mean what you might think. For example, (?!a){3} does not assert that the
20     next three characters are not "a". It just asserts that the next character is
21 ph10 654 not "a" three times (in principle: PCRE optimizes this to run the assertion
22     just once). Perl allows repeat quantifiers on other assertions such as \eb, but
23 ph10 637 these do not seem to have any use.
24 nigel 75 .P
25 nigel 73 3. Capturing subpatterns that occur inside negative lookahead assertions are
26 ph10 1292 counted, but their entries in the offsets vector are never set. Perl sometimes
27     (but not always) sets its numerical variables from inside negative assertions.
28 nigel 75 .P
29 nigel 73 4. Though binary zero characters are supported in the subject string, they are
30 nigel 63 not allowed in a pattern string because it is passed as a normal C string,
31 nigel 75 terminated by zero. The escape sequence \e0 can be used in the pattern to
32 nigel 63 represent a binary zero.
33 nigel 75 .P
34     5. The following Perl escape sequences are not supported: \el, \eu, \eL,
35 ph10 654 \eU, and \eN when followed by a character name or Unicode value. (\eN on its
36 ph10 634 own, matching a non-newline character, is supported.) In fact these are
37     implemented by Perl's general string-handling and are not part of its pattern
38     matching engine. If any of these are encountered by PCRE, an error is
39 ph10 836 generated by default. However, if the PCRE_JAVASCRIPT_COMPAT option is set,
40 ph10 745 \eU and \eu are interpreted as JavaScript interprets them.
41 nigel 75 .P
42     6. The Perl escape sequences \ep, \eP, and \eX are supported only if PCRE is
43     built with Unicode character property support. The properties that can be
44     tested with \ep and \eP are limited to the general category properties such as
45 nigel 87 Lu and Nd, script names such as Greek or Han, and the derived properties Any
46 ph10 450 and L&. PCRE does support the Cs (surrogate) property, which Perl does not; the
47     Perl documentation says "Because Perl hides the need for the user to understand
48     the internal representation of Unicode characters, there is no need to
49     implement the somewhat messy concept of surrogates."
50 nigel 75 .P
51 ph10 1011 7. PCRE does support the \eQ...\eE escape for quoting substrings. Characters in
52 nigel 63 between are treated as literals. This is slightly different from Perl in that $
53     and @ are also handled as literals inside the quotes. In Perl, they cause
54     variable interpolation (but of course PCRE does not have variables). Note the
55     following examples:
56 nigel 75 .sp
57 nigel 63 Pattern PCRE matches Perl matches
58 nigel 75 .sp
59     .\" JOIN
60     \eQabc$xyz\eE abc$xyz abc followed by the
61 nigel 63 contents of $xyz
62 nigel 75 \eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
63     \eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
64     .sp
65     The \eQ...\eE sequence is recognized both inside and outside character classes.
66     .P
67 ph10 1011 8. Fairly obviously, PCRE does not support the (?{code}) and (??{code})
68 nigel 93 constructions. However, there is support for recursive patterns. This is not
69 ph10 453 available in Perl 5.8, but it is in Perl 5.10. Also, the PCRE "callout"
70 nigel 93 feature allows an external function to be called during pattern matching. See
71     the
72 nigel 75 .\" HREF
73     \fBpcrecallout\fP
74     .\"
75     documentation for details.
76     .P
77 ph10 1011 9. Subpatterns that are called as subroutines (whether or not recursively) are
78 ph10 716 always treated as atomic groups in PCRE. This is like Python, but unlike Perl.
79 ph10 724 Captured values that are set outside a subroutine call can be reference from
80     inside in PCRE, but not in Perl. There is a discussion that explains these
81     differences in more detail in the
82 ph10 453 .\" HTML <a href="pcrepattern.html#recursiondifference">
83     .\" </a>
84     section on recursion differences from Perl
85     .\"
86     in the
87     .\" HREF
88 ph10 456 \fBpcrepattern\fP
89 ph10 453 .\"
90     page.
91 nigel 93 .P
92 ph10 1297 10. If any of the backtracking control verbs are used in a subpattern that is
93     called as a subroutine (whether or not recursively), their effect is confined
94     to that subpattern; it does not extend to the surrounding pattern. This is not
95     always the case in Perl. In particular, if (*THEN) is present in a group that
96     is called as a subroutine, its action is limited to that group, even if the
97     group does not contain any | characters. Note that such subpatterns are
98     processed as anchored at the point where they are tested.
99 ph10 716 .P
100 ph10 1293 11. If a pattern contains more than one backtracking control verb, the first
101     one that is backtracked onto acts. For example, in the pattern
102     A(*COMMIT)B(*PRUNE)C a failure in B triggers (*COMMIT), but a failure in C
103     triggers (*PRUNE). Perl's behaviour is more complex; in many cases it is the
104     same as PCRE, but there are examples where it differs.
105     .P
106 ph10 1335 12. Most backtracking verbs in assertions have their normal actions. They are
107 ph10 1297 not confined to the assertion.
108     .P
109     13. There are some differences that are concerned with the settings of captured
110 nigel 63 strings when part of a pattern is repeated. For example, matching "aba" against
111     the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b".
112 nigel 75 .P
113 ph10 1297 14. PCRE's handling of duplicate subpattern numbers and duplicate subpattern
114 ph10 459 names is not as general as Perl's. This is a consequence of the fact the PCRE
115     works internally just with numbers, using an external table to translate
116     between numbers and names. In particular, a pattern such as (?|(?<a>A)|(?<b)B),
117     where the two capturing parentheses have the same number but different names,
118     is not supported, and causes an error at compile time. If it were allowed, it
119     would not be possible to distinguish which parentheses matched, because both
120     names map to capturing subpattern number 1. To avoid this confusing situation,
121     an error is given at compile time.
122 ph10 456 .P
123 ph10 1297 15. Perl recognizes comments in some places that PCRE does not, for example,
124 ph10 654 between the ( and ? at the start of a subpattern. If the /x modifier is set,
125 ph10 1404 Perl allows white space between ( and ? (though current Perls warn that this is
126 ph10 1396 deprecated) but PCRE never does, even if the PCRE_EXTENDED option is set.
127 ph10 562 .P
128 ph10 1392 16. Perl, when in warning mode, gives warnings for character classes such as
129 ph10 1404 [A-\ed] or [a-[:digit:]]. It then treats the hyphens as literals. PCRE has no
130     warning features, so it gives an error in these cases because they are almost
131 ph10 1392 certainly user mistakes.
132     .P
133     17. In PCRE, the upper/lower case character properties Lu and Ll are not
134 ph10 1258 affected when case-independent matching is specified. For example, \ep{Lu}
135     always matches an upper case letter. I think Perl has changed in this respect;
136     in the release at the time of writing (5.16), \ep{Lu} and \ep{Ll} match all
137     letters, regardless of case, when case independence is specified.
138     .P
139 ph10 1392 18. PCRE provides some extensions to the Perl regular expression facilities.
140 ph10 456 Perl 5.10 includes new features that are not in earlier versions of Perl, some
141     of which (such as named parentheses) have been in PCRE for some time. This list
142     is with respect to Perl 5.10:
143     .sp
144     (a) Although lookbehind assertions in PCRE must match fixed length strings,
145     each alternative branch of a lookbehind assertion can match a different length
146     of string. Perl requires them all to have the same length.
147     .sp
148 nigel 63 (b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $
149     meta-character matches only at the very end of the string.
150 nigel 75 .sp
151 nigel 63 (c) If PCRE_EXTRA is set, a backslash followed by a letter with no special
152 ph10 181 meaning is faulted. Otherwise, like Perl, the backslash is quietly ignored.
153     (Perl can be made to issue a warning.)
154 nigel 75 .sp
155 nigel 63 (d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is
156     inverted, that is, by default they are not greedy, but if followed by a
157     question mark they are.
158 nigel 75 .sp
159     (e) PCRE_ANCHORED can be used at matching time to force a pattern to be tried
160     only at the first matching position in the subject string.
161     .sp
163     PCRE_NO_AUTO_CAPTURE options for \fBpcre_exec()\fP have no Perl equivalents.
164 nigel 75 .sp
165 ph10 231 (g) The \eR escape sequence can be restricted to match only CR, LF, or CRLF
166     by the PCRE_BSR_ANYCRLF option.
167 nigel 75 .sp
168 ph10 231 (h) The callout facility is PCRE-specific.
169 nigel 75 .sp
170 ph10 231 (i) The partial matching facility is PCRE-specific.
171     .sp
172     (j) Patterns compiled by PCRE can be saved and re-used at a later time, even on
173 ph10 733 different hosts that have the other endianness. However, this does not apply to
174 ph10 716 optimized data created by the just-in-time compiler.
175 nigel 77 .sp
176 chpe 1055 (k) The alternative matching functions (\fBpcre_dfa_exec()\fP,
177     \fBpcre16_dfa_exec()\fP and \fBpcre32_dfa_exec()\fP,) match in a different way
178     and are not Perl-compatible.
179 ph10 231 .sp
180     (l) PCRE recognizes some special sequences such as (*CR) at the start of
181     a pattern that set overall options that cannot be changed within the pattern.
182 ph10 99 .
183     .
184     .SH AUTHOR
185     .rs
186     .sp
187     .nf
188     Philip Hazel
189     University Computing Service
190     Cambridge CB2 3QH, England.
191     .fi
192     .
193     .
194     .SH REVISION
195     .rs
196     .sp
197     .nf
198 ph10 1396 Last updated: 10 November 2013
199 ph10 1258 Copyright (c) 1997-2013 University of Cambridge.
200 ph10 99 .fi


Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

ViewVC Help
Powered by ViewVC 1.1.12