Edit report at http://bugs.php.net/bug.php?id=54099&edit=1
ID: 54099
Comment by: jeroen at asystance dot nl
Reported by: jeroen at asystance dot nl
Summary: PCRE preg_match incorrectly matches negated
character class
Status: Bogus
Type: Bug
Package: PCRE related
Operating System: Linux, Windows
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
Argh, sorry, can't believe I didn't notice that!
Thanks for the swift reply!
Previous Comments:
------------------------------------------------------------------------
[2011-02-25 10:33:54] [email protected]
You forgot your delimiters.
eg.
echo preg_match( '/[^ab]/', 'ab' ) . "\n";
You didn't forget them in the Perl version which is why it worked there.
------------------------------------------------------------------------
[2011-02-25 10:24:58] jeroen at asystance dot nl
Description:
------------
The perl-compatible regex matcher somehow matches a negated character
class if the string to match contains the same characters, and in the
same order, as the character class.
I don't see why /[^ab]/ should match "ab" but not "ba". Perl agrees:
$ perl -e 'for("ab","ba") { print "$_: "; if( /[^ab]/ ) {print
"match\n"} else {print "no match\n"}}'
ab: no match
ba: no match
Test script:
---------------
<?php
echo "expecting 0, 0, 0, 0, 0\n";
echo preg_match( '[^ab]', 'ba' ) . "\n"; // expected no match,
passes
echo preg_match( '[^ab]', 'ab' ) . "\n"; // expected no match,
fails
echo preg_match( '([^ab])', 'ab' ) . "\n"; // expected no match,
passes
echo preg_match( '[^ab]', 'aba' ) . "\n"; // expected no match,
fails
echo preg_match( '[^ab]', 'abb' ) . "\n"; // expected no match,
fails
echo preg_match( '([^ab])', 'abb' ) . "\n"; // expected no match,
passes
?>
Expected result:
----------------
expecting 0, 0, 0, 0, 0, 0
0
0
0
0
0
0
Actual result:
--------------
expecting 0, 0, 0, 0, 0, 0
0
1
0
1
1
0
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54099&edit=1