#41739 [NEW]: Escaped backslash in PCRE regex documentation
| From: | trex0003 at umn dot edu | Date: | Tue, 19 Jun 2007 17:34:33 +0000 |
| Subject: | #41739 [NEW]: Escaped backslash in PCRE regex documentation | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969376777@lists.php.net to get a copy of this message | ||
From: trex0003 at umn dot edu
Operating system: CentOS
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: Escaped backslash in PCRE regex documentation
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 bug report at http://bugs.php.net/?id=41739&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=41739&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=41739&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=41739&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=41739&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=41739&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=41739&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=41739&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=41739&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=41739&r=support
Expected behavior: http://bugs.php.net/fix.php?id=41739&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=41739&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=41739&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=41739&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=41739&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=41739&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=41739&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=41739&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=41739&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=41739&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=41739&r=mysqlcfg