Bug #51663 [Fbk]: PHP preg_match doesn't match string which should match
| From: | pajoye@php.net | Date: | Wed, 28 Apr 2010 09:45:56 +0000 |
| Subject: | Bug #51663 [Fbk]: PHP preg_match doesn't match string which should match | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-149985@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51663&edit=1
ID: 51663
Updated by: pajoye@php.net
Reported by: jordi dot salvat dot i dot alabart at gmail dot com
Summary: PHP preg_match doesn't match string which should match
Status: Feedback
Type: Bug
Package: PCRE related
Operating System: Ubuntu
PHP Version: 5.3.2
New Comment:
Only to be sure:
did you actually try using 5.3.2 and the bundled PCRE? You can test it
without having to install PHP on your system. You only have to compile
php. The same applies for php 5.2.13.
Previous Comments:
------------------------------------------------------------------------
[2010-04-28 11:31:07] adconrad at ubuntu dot com
Given that Ubuntu builds PHP against the system libpcre, and pcretest
(by definition, but also a quick visual check with ldd) also uses the
system libpcre, it seems unlikely this is a problem with libpcre itself,
since pcretest works and PHP doesn't.
------------------------------------------------------------------------
[2010-04-26 12:18:53] johannes@php.net
Are you using the bundled PCRE lib or your system's one? - Default is
the bundled one. currently bundled is 8.02. i assume this is a PCRE
and no PHP issue.
------------------------------------------------------------------------
[2010-04-26 00:52:55] jordi dot salvat dot i dot alabart at gmail dot
com
I've been able to simplify the example to:
<?= preg_match("/(.+)+:/", "a:bbbbbbbbbbbbb") ? "pass" :
"fail" ?>
(I've checked this simplified form fails in PHP 5.2.10-2ubuntu6.4;
checking it in 5.3.2 too is left as an exercise for the reader).
------------------------------------------------------------------------
[2010-04-26 00:37:02] jordi dot salvat dot i dot alabart at gmail dot
com
Description:
------------
This regular expression:
/^(?:[^\[\]{}']+|'[^']*')+:(?:[^\[\]{}']+|'[^']*')+$/
matches this string: a:bbbbbbbbbbbbbbb
in Perl (5.10.0-24ubuntu4):
perl <<__END__
print 'a:bbbbbbbbbbbbb' =~
q/^(?:[^\[\]{}']+|'[^']*')+:(?:[^\[\]{}']+|'[^']*')+$/;
print "\n";
__END__
1
and pcretest (libpcre3 7.8-3):
pcretest <<__END__
/^(?:[^\[\]{}']+|'[^']*')+:(?:[^\[\]{}']+|'[^']*')+$/
a:bbbbbbbbbbbbbbb
__END__
PCRE version 7.8 2008-09-05
re> data> 0: a:bbbbbbbbbbbbbbb
data>
Not, however, in PHP (5.3.2):
$ ./php --version
PHP 5.3.2 (cli) (built: Apr 25 2010 23:58:33)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
$ ./php <<__END__
<?=
preg_match("/^(?:[^\[\]{}']+|'[^']*')+:(?:[^\[\]{}']+|'[^']*')+$/",
"a:bbbbbbbbbbbbb") ?>
__END__
0
The bug is pretty sensible to changes in the input. Removing a couple of
"b"s makes it match. I don't know which aspects of the regexp cause it
to fail.
For confirmation that this is indeed a bug without having to decypher
the regexp, here's proof:
<?php
$A='(?:[^\[\]{}\']+|\'[^\']*\')+';
$a= 'a';
$B=":$A";
$b= ':bbbbbbbbbbbbbbb';
print_r(preg_match("/^$A$/", "$a"));
print_r(preg_match("/^$B$/", "$b"));
print_r(preg_match("/^$A$B$/", "$a$b"));
print_r("\n");
This outputs "110", which is impossible since if /^$A$/ matches "$a" and
/^$B$/ matches "$b", /^$A$B$/ should definitely match "$a$b".
Test script:
---------------
<?=
preg_match("/^(?:[^\[\]{}']+|'[^']*')+:(?:[^\[\]{}']+|'[^']*')+$/",
"a:bbbbbbbbbbbbb") ? "pass" : "fail" ?>
Expected result:
----------------
pass
Actual result:
--------------
fail
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51663&edit=1