PHP 4.0 Bug #6740 Updated: * sometimes doesn't work like expected (doesn't match 0 characters)
| From: | Bug Database | Date: | Thu, 14 Sep 2000 03:45:06 +0000 |
| Subject: | PHP 4.0 Bug #6740 Updated: * sometimes doesn't work like expected (doesn't match 0 characters) | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-33249@lists.php.net to get a copy of this message | ||
ID: 6740
Updated by: andrei
Reported By: thow@gmx.net
Status: Closed
Bug Type: PCRE related
Assigned To:
Comments:
Fixed in CVS.
Previous Comments:
---------------------------------------------------------------------------
[2000-09-13 21:23:24] thow@gmx.net
Update: this seems to be a problem with the backreferences, because when you have a string
"abc##def#ghi"
and you want to match everything between the hashmarks, the regexp "/#([^#]*?)#/" fails
when there would be an empty backreference as it it would be at the 2 hashmarks after
"abc" but "/(#[^#]*?#)/" works without any problem because there cannot be an
empty backreference.
Counclusion: empty backreferences don't work :-(
---------------------------------------------------------------------------
[2000-09-13 18:54:13] thow@gmx.net
SCRIPTS:
=====
regexp.pl (works like expected):
======
#!/usr/bin/perl -w
sub process_hashmark {
my $s = shift @_;
if ($s eq '') {
print "Matched string: empty stringn";
} else {
print "Matched string: $sn";
}
return 'XX' if ($s eq '');
return 'ZZZ';
}
my $s;
my $regexp = 'X([^X]*?)X';
if (defined $ARGV[0]) {
$s = $ARGV[0];
} else {
$s = 'abcXdefXghi';
}
print "Regular Expression: $regexpn";
print "String to process: $sn";
$s =~ s/$regexp/process_hashmark($1)/ge;
print "Processed String: $sn";
=====
regexp.pl (end)
regexp.php (works not like expected):
=====
function process_hashmark($string) {
if ($string == "") {
print "Matched string: empty string<br>n";
} else {
print "Matched string: $string<br>n";
}
if ($string == "") return "XX";
return "ZZZ";
}
$regexp = "X([^X]*?)X";
if (empty($s)) $s = "abcXdefXghi";
echo "Regular Expression: $regexp<br>n";
echo "String to process: $s<br>n";
$s = preg_replace("/$regexp/e", "process_hashmark(\1);", $s);
echo "Processed string: $s<br>n";
=====
regexp.php (end)
OUTPUT:
=====
regexp.pl (case 1):
Regular Expression: X([^X]*?)X
String to process: abcXdefXghi
Matched string: def
Processed String: abcZZZghi
regexp.pl (case 2):
Regular Expression: X([^X]*?)X
String to process: abcXXdefXghi
Matched string: empty string
Processed String: abcXXdefXghi
regexp.php (case 1):
Regular Expression: X([^X]*?)X
String to process: abcXdefXghi
Matched string: def
Processed string: abcZZZghi
regexp.php (case 2):
Regular Expression: X([^X]*?)X
String to process: abcXXdefXghi
Matched string: XdefXghi
Processed string: abcZZZdefXghi
CONFIGURE LINE:
=====
./configure --with-apxs --with-pgsql=/usr/lib/pgsql --with-mysql=/usr
php.ini should be irrelevant and there is nothing special in our setup
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=6740