| 1 |
nigel |
63 |
<html> |
| 2 |
|
|
<head> |
| 3 |
|
|
<title>pcrecompat specification</title> |
| 4 |
|
|
</head> |
| 5 |
|
|
<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> |
| 6 |
|
|
This HTML document has been generated automatically from the original man page. |
| 7 |
|
|
If there is any nonsense in it, please consult the man page, in case the |
| 8 |
|
|
conversion went wrong.<br> |
| 9 |
|
|
<ul> |
| 10 |
|
|
<li><a name="TOC1" href="#SEC1">DIFFERENCES FROM PERL</a> |
| 11 |
|
|
</ul> |
| 12 |
|
|
<br><a name="SEC1" href="#TOC1">DIFFERENCES FROM PERL</a><br> |
| 13 |
|
|
<P> |
| 14 |
|
|
This document describes the differences in the ways that PCRE and Perl handle |
| 15 |
|
|
regular expressions. The differences described here are with respect to Perl |
| 16 |
|
|
5.8. |
| 17 |
|
|
</P> |
| 18 |
|
|
<P> |
| 19 |
nigel |
73 |
1. PCRE does not have full UTF-8 support. Details of what it does have are |
| 20 |
|
|
given in the |
| 21 |
|
|
<a href="pcre.html#utf8support">section on UTF-8 support</a> |
| 22 |
|
|
in the main |
| 23 |
|
|
<a href="pcre.html"><b>pcre</b></a> |
| 24 |
|
|
page. |
| 25 |
|
|
</P> |
| 26 |
|
|
<P> |
| 27 |
|
|
2. PCRE does not allow repeat quantifiers on lookahead assertions. Perl permits |
| 28 |
nigel |
63 |
them, but they do not mean what you might think. For example, (?!a){3} does |
| 29 |
|
|
not assert that the next three characters are not "a". It just asserts that the |
| 30 |
|
|
next character is not "a" three times. |
| 31 |
|
|
</P> |
| 32 |
|
|
<P> |
| 33 |
nigel |
73 |
3. Capturing subpatterns that occur inside negative lookahead assertions are |
| 34 |
nigel |
63 |
counted, but their entries in the offsets vector are never set. Perl sets its |
| 35 |
|
|
numerical variables from any such patterns that are matched before the |
| 36 |
|
|
assertion fails to match something (thereby succeeding), but only if the |
| 37 |
|
|
negative lookahead assertion contains just one branch. |
| 38 |
|
|
</P> |
| 39 |
|
|
<P> |
| 40 |
nigel |
73 |
4. Though binary zero characters are supported in the subject string, they are |
| 41 |
nigel |
63 |
not allowed in a pattern string because it is passed as a normal C string, |
| 42 |
|
|
terminated by zero. The escape sequence "\0" can be used in the pattern to |
| 43 |
|
|
represent a binary zero. |
| 44 |
|
|
</P> |
| 45 |
|
|
<P> |
| 46 |
nigel |
73 |
5. The following Perl escape sequences are not supported: \l, \u, \L, |
| 47 |
nigel |
63 |
\U, \P, \p, \N, and \X. In fact these are implemented by Perl's general |
| 48 |
|
|
string-handling and are not part of its pattern matching engine. If any of |
| 49 |
|
|
these are encountered by PCRE, an error is generated. |
| 50 |
|
|
</P> |
| 51 |
|
|
<P> |
| 52 |
nigel |
73 |
6. PCRE does support the \Q...\E escape for quoting substrings. Characters in |
| 53 |
nigel |
63 |
between are treated as literals. This is slightly different from Perl in that $ |
| 54 |
|
|
and @ are also handled as literals inside the quotes. In Perl, they cause |
| 55 |
|
|
variable interpolation (but of course PCRE does not have variables). Note the |
| 56 |
|
|
following examples: |
| 57 |
|
|
</P> |
| 58 |
|
|
<P> |
| 59 |
|
|
<pre> |
| 60 |
|
|
Pattern PCRE matches Perl matches |
| 61 |
|
|
</PRE> |
| 62 |
|
|
</P> |
| 63 |
|
|
<P> |
| 64 |
|
|
<pre> |
| 65 |
|
|
\Qabc$xyz\E abc$xyz abc followed by the |
| 66 |
|
|
contents of $xyz |
| 67 |
|
|
\Qabc\$xyz\E abc\$xyz abc\$xyz |
| 68 |
|
|
\Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
| 69 |
|
|
</PRE> |
| 70 |
|
|
</P> |
| 71 |
|
|
<P> |
| 72 |
nigel |
73 |
The \Q...\E sequence is recognized both inside and outside character classes. |
| 73 |
nigel |
63 |
</P> |
| 74 |
|
|
<P> |
| 75 |
nigel |
73 |
7. Fairly obviously, PCRE does not support the (?{code}) and (?p{code}) |
| 76 |
nigel |
63 |
constructions. However, there is some experimental support for recursive |
| 77 |
|
|
patterns using the non-Perl items (?R), (?number) and (?P>name). Also, the PCRE |
| 78 |
|
|
"callout" feature allows an external function to be called during pattern |
| 79 |
|
|
matching. |
| 80 |
|
|
</P> |
| 81 |
|
|
<P> |
| 82 |
nigel |
73 |
8. There are some differences that are concerned with the settings of captured |
| 83 |
nigel |
63 |
strings when part of a pattern is repeated. For example, matching "aba" against |
| 84 |
|
|
the pattern /^(a(b)?)+$/ in Perl leaves $2 unset, but in PCRE it is set to "b". |
| 85 |
|
|
</P> |
| 86 |
|
|
<P> |
| 87 |
nigel |
73 |
9. PCRE provides some extensions to the Perl regular expression facilities: |
| 88 |
nigel |
63 |
</P> |
| 89 |
|
|
<P> |
| 90 |
|
|
(a) Although lookbehind assertions must match fixed length strings, each |
| 91 |
|
|
alternative branch of a lookbehind assertion can match a different length of |
| 92 |
|
|
string. Perl requires them all to have the same length. |
| 93 |
|
|
</P> |
| 94 |
|
|
<P> |
| 95 |
|
|
(b) If PCRE_DOLLAR_ENDONLY is set and PCRE_MULTILINE is not set, the $ |
| 96 |
|
|
meta-character matches only at the very end of the string. |
| 97 |
|
|
</P> |
| 98 |
|
|
<P> |
| 99 |
|
|
© If PCRE_EXTRA is set, a backslash followed by a letter with no special |
| 100 |
|
|
meaning is faulted. |
| 101 |
|
|
</P> |
| 102 |
|
|
<P> |
| 103 |
|
|
(d) If PCRE_UNGREEDY is set, the greediness of the repetition quantifiers is |
| 104 |
|
|
inverted, that is, by default they are not greedy, but if followed by a |
| 105 |
|
|
question mark they are. |
| 106 |
|
|
</P> |
| 107 |
|
|
<P> |
| 108 |
|
|
(e) PCRE_ANCHORED can be used to force a pattern to be tried only at the first |
| 109 |
|
|
matching position in the subject string. |
| 110 |
|
|
</P> |
| 111 |
|
|
<P> |
| 112 |
|
|
(f) The PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and PCRE_NO_AUTO_CAPTURE |
| 113 |
|
|
options for <b>pcre_exec()</b> have no Perl equivalents. |
| 114 |
|
|
</P> |
| 115 |
|
|
<P> |
| 116 |
|
|
(g) The (?R), (?number), and (?P>name) constructs allows for recursive pattern |
| 117 |
|
|
matching (Perl can do this using the (?p{code}) construct, which PCRE cannot |
| 118 |
|
|
support.) |
| 119 |
|
|
</P> |
| 120 |
|
|
<P> |
| 121 |
|
|
(h) PCRE supports named capturing substrings, using the Python syntax. |
| 122 |
|
|
</P> |
| 123 |
|
|
<P> |
| 124 |
|
|
(i) PCRE supports the possessive quantifier "++" syntax, taken from Sun's Java |
| 125 |
|
|
package. |
| 126 |
|
|
</P> |
| 127 |
|
|
<P> |
| 128 |
|
|
(j) The (R) condition, for testing recursion, is a PCRE extension. |
| 129 |
|
|
</P> |
| 130 |
|
|
<P> |
| 131 |
|
|
(k) The callout facility is PCRE-specific. |
| 132 |
|
|
</P> |
| 133 |
|
|
<P> |
| 134 |
nigel |
73 |
Last updated: 09 December 2003 |
| 135 |
nigel |
63 |
<br> |
| 136 |
|
|
Copyright © 1997-2003 University of Cambridge. |