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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC