php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38899 Backreference to optional item breaks preg_match
Submitted: 2006-09-20 13:24 UTC Modified: 2006-09-22 14:04 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: mike at we11er dot co dot uk Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 5.1.6 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
16 - 13 = ?
Subscribe to this entry?

 
 [2006-09-20 13:24 UTC] mike at we11er dot co dot uk
Description:
------------
echo preg_match("/(.)?a\\1/", "a");

returns "0"

But since the back reference is to an optional item, if that item does not exist, then the back reference should just be blank and thus the above should match "a", i.e. nothing + 'a' + nothing

This is actually stopping quite an important feature from being developed so I'd appreciate a speedy response. It might be something I'm doing wrong, or maybe this isn't supported (but i think it should be).

Thanks in advance.

Reproduce code:
---------------
echo preg_match("/(.)?a\\1/", "a");

Expected result:
----------------
Should return TRUE.

Actual result:
--------------
returns FALSE.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-20 13:34 UTC] mike at we11er dot co dot uk
I should also add that this happens with preg_split.
 [2006-09-20 13:40 UTC] mike at we11er dot co dot uk
I just realised the example code might be slightly ambiguous since the (.)? could match the 'a' itself.

echo preg_match("/(test)?a\\1/", "a");

is a better example and returns false.
 [2006-09-20 14:14 UTC] mike at we11er dot co dot uk
Ah! I got a solution, though I'm still not sure what the correct behaviour of the original regex should be.

preg_match("/((test)?)a\\1/", "a");

Wrapping another set of parentheses around the optional part works.
 [2006-09-20 14:23 UTC] mike at we11er dot co dot uk
OK, sorry for so many posts... but I'm still having problems. The actual regex i'm using is much more complicated, so I'm simpifying here:

preg_match("/(((test))?)a\\3/", "a") returns FALSE
preg_match("/(((test))?)a\\3/", "testatest") returns TRUE

The part I need to reference is nested within an outer part that is optional. Even adding extra parentheses doesn't solve the problem.
 [2006-09-22 14:04 UTC] edink@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC