php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54099 PCRE preg_match incorrectly matches negated character class
Submitted: 2011-02-25 10:24 UTC Modified: 2011-02-25 10:33 UTC
From: jeroen at asystance dot nl Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.3.5 OS: Linux, Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jeroen at asystance dot nl
New email:
PHP Version: OS:

 

 [2011-02-25 10:24 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-25 10:33 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-02-25 10:33 UTC] rasmus@php.net
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:43 UTC] jeroen at asystance dot nl
Argh, sorry, can't believe I didn't notice that!

Thanks for the swift reply!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC