#41739 [Opn->Bgs]: Escaped backslash in PCRE regex documentation
| From: | vrana@php.net | Date: | Thu, 16 Aug 2007 14:21:11 +0000 |
| Subject: | #41739 [Opn->Bgs]: Escaped backslash in PCRE regex documentation | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969377437@lists.php.net to get a copy of this message | ||
ID: 41739
Updated by: vrana@php.net
Reported By: trex0003 at umn dot edu
-Status: Open
+Status: Bogus
Bug Type: Documentation problem
Operating System: CentOS
PHP Version: Irrelevant
New Comment:
\ character
\\ PCRE representation of this character
'\\\\' PHP string to write this representation.
Previous Comments:
------------------------------------------------------------------------
[2007-06-19 17:34:33] trex0003 at umn dot edu
Description:
------------
Documentation of how PCRE regex behaves with escaped backslashes is
confusing as written in current documentation:
http://php.planetmirror.com/manual/en/reference.pcre.pattern.syntax.php
Perhaps this just means an expansion of the note:
"Note: Single and double quoted PHP strings have special meaning of
backslash. Thus if \ has to be matched with a regular expression \\,
then "\\\\" or '\\\\' must be used in PHP code."
While trying to use preg_split to break a string at non-escaped string
quotes, I tried using a negative lookbehind assertion (see code below).
With all other escaped characters in the lookbehind assertion, you only
need one backwards slash before the escaped character (e.g., "(?<!\s)"
works); but if you want to escape a backslash, you need to escape it
twice (e.g., "(?<!\\\)" works but "(?<!\\)" does not).
Based on the documentation's statement 'In particular, if you want to
match a backslash, you write "\\".' it seems that (?<!\\) should work,
and the extra information in the Note is too terse to explain why.
Perhaps this is actually a bug, but I can't tell if it's normal
behavior that could be clarified more in documentation.
The closest I could find for bug reports to this issue is here:
http://bugs.php.net/bug.php?id=22315
...but this only implies the difference in behavior without explaining
why.
Reproduce code:
---------------
$string = "This sentence is not quoted. 'But this one is, and it
contains \'escaped quotes\' within!'";
$needle = "/(?<!\\)'/";
$results = preg_split($needle, $string);
print_r($results);
Expected result:
----------------
Array
(
[0] => This sentence is not quoted.
[1] => But this one is, and it contains \'escaped quotes\' within!
[2] =>
)
// Adding a third backslash to the $needle gives this result.
Actual result:
--------------
Warning: preg_split() [function.preg-split]: Compilation failed:
missing ) at offset 7 in
/data/domains/lawweb3.law.umn.edu/public/php/regex.php on line 16
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=41739&edit=1