php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6740 * sometimes doesn't work like expected (doesn't match 0 characters)
Submitted: 2000-09-13 18:54 UTC Modified: 2000-09-13 23:45 UTC
From: thow at gmx dot net Assigned:
Status: Closed Package: PCRE related
PHP Version: 4.0.2 OS: Linux 2.2.14
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: thow at gmx dot net
New email:
PHP Version: OS:

 

 [2000-09-13 18:54 UTC] thow at gmx dot net
SCRIPTS:
=====

regexp.pl (works like expected):
======

#!/usr/bin/perl -w

sub process_hashmark {
	my $s = shift @_;

	if ($s eq '') {
		print "Matched string:     empty string\n";
	} else {
		print "Matched string:     $s\n";
	}

	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: $regexp\n";
print "String to process:  $s\n";

$s =~ s/$regexp/process_hashmark($1)/ge;

print "Processed String:   $s\n";
=====
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-13 21:23 UTC] thow at gmx dot 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 23:45 UTC] andrei@php.net
Fixed in CVS.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Mar 13 20:01:30 2025 UTC