php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63218 Recursion and greedy
Submitted: 2012-10-04 11:38 UTC Modified: 2012-10-04 14:15 UTC
From: jille at hexon dot cx Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.4.7 OS: n/a
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: jille at hexon dot cx
New email:
PHP Version: OS:

 

 [2012-10-04 11:38 UTC] jille at hexon dot cx
Description:
------------
See test-script. When I make the recursive-backreference (?R) optional (with ?) it doesn't match $str to its full extent.

Test script:
---------------
<?php
  $str = "A{X|param:{Y}}B";
  $reWanted =   '/{[A-Z]+(\|param:[^|}]*(?R)?[^|}]*)*}/';
  var_dump(preg_match($reWanted, $str, $m), $m);
  $reWorking =  '/{[A-Z]+(\|param:[^|}]*(?R)[^|}]*)*}/';
  var_dump(preg_match($reWorking, $str, $m), $m);
  $reWorking2 = '/{[A-Z]+(\|param:([^|}]*(?R)[^|}]*|[^|}]*))*}/';
  var_dump(preg_match($reWorking2, $str, $m), $m);
?>

Expected result:
----------------
For all three cases:

int(1)
array(3) {
  [0]=>
  string(13) "{X|param:{Y}}"
  [1]=>
  string(10) "|param:{Y}"
  [2]=>
  string(3) "{Y}"
}


Actual result:
--------------
Using $reWanted gives this instead of the expected result. Note the missing }

int(1)
array(2) {
  [0]=>
  string(12) "{X|param:{Y}"
  [1]=>
  string(9) "|param:{Y"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-04 11:39 UTC] jille at hexon dot cx
5.3.10 has the same problem.
 [2012-10-04 13:58 UTC] rasmus@php.net
Any regex-related bug report should include the results from pcretest so we can 
see whether it is something that is specific to PHP or whether it is simply how 
the PCRE library works. eg.

  re> /{[A-Z]+(\|param:[^|}]*(?R)?[^|}]*)*}/
data> A{X|param:{Y}}B
 0: {X|param:{Y}
 1: |param:{Y

So as far as I can see this one has nothing to do with PHP. If you still feel it 
is a bug, you should file it against PCRE at http://www.pcre.org/ and get back 
to us with the results. As far as I can tell from a quick look at that regex, it 
is doing exactly what I would expect.
 [2012-10-04 13:58 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-10-04 14:15 UTC] jille at hexon dot cx
I've filed it as http://bugs.exim.org/show_bug.cgi?id=1303

(I disagree it works as expected, as it fails to find the longest match while it is in greedy-mode.)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC