|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20856 preg_match backreference fails
Submitted: 2002-12-06 06:35 UTC Modified: 2002-12-06 09:11 UTC
From: earth_php at erowid dot org Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 4.2.2 OS: freebsd 4.7 stable
Private report: No CVE-ID: None
 [2002-12-06 06:35 UTC] earth_php at erowid dot org
Perhaps I am misunderstanding something about the differences between perl regex and php perl regex, but this looks like a bug to me.  The back references are behaving strangely.. I think the following code demonstrates it fairly well.  Removing the back reference from the preg_match code results in a successful match as expected.

The following code fails in php 4.2.2:

$string = "blah/blah/other/stuff/here.html";
if (preg_match("/^\/?([^\/]+\/)(\1)/",$string,$matches) )  
{  print "found match: $matches[1] <BR>\n";
{       print "failed to match.. <BR>\n";

in perl 5.x, it works fine:

my $string = "blah/blah/other/stuff/here.html";
if ($string =~ /^\/?([^\/]+\/)(\1)/)
{      print "found match: $1 <BR>\n";
{       print "failed to match.. <BR>\n";

configure command:

'./configure' '--with-apxs=/usr/local/sbin/apxs' '--with-config-file-path=/usr/local/etc' '--enable-versioning' '--with-regex=system' '--without-gd' '--without-mysql' '--with-gd=/usr/local' '--enable-gd-native-ttf' '--with-freetype-dir=/usr/local' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr/local' '--with-zlib' '--with-bz2=/usr' '--with-mcrypt=/usr/local' '--with-mhash=/usr/local' '--with-mysql=/usr/local' '--with-openssl=/usr' '--with-expat-dir=/usr/local' '--with-xmlrpc' '--enable-xslt' '--with-xslt-sablot' '--enable-ftp' '--with-curl=/usr/local' '--with-gettext=/usr/local' '--with-iconv=/usr/local' '--enable-trans-sid' '--prefix=/usr/local' 'i386-portbld-freebsd4.6'

Thanks for looking at this,



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-06 09:11 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Since you're using double quotes, the \1 is the ascii character \1. You need to provide \\1 to get the backreference.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Nov 29 00:01:26 2023 UTC