php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45260 Recursive Capturing Subpattern
Submitted: 2008-06-13 13:29 UTC Modified: 2008-06-13 17:41 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ralph at deboom dot biz Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.2.6 OS: Debian Etch 4.0+
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: ralph at deboom dot biz
New email:
PHP Version: OS:

 

 [2008-06-13 13:29 UTC] ralph at deboom dot biz
Description:
------------
A recursive subpattern turns out to overwrite data of the previous matched call... Thus it will only show the last recursive match it made in the pattern...



Reproduce code:
---------------
$value = 'sub.sub2.sub3.sub4.domain.ext';
if (preg_match("/^(([a-z0-9]+)(([\.|\-]{1}[a-z0-9]+)|(?R))*\.([a-z]{2,6}))$/i", $value, $match)) {	
 	print_R($match);
}


Expected result:
----------------
Array
(
    [0] => sub.sub2.sub3.sub4.domain.ext
    [1] => sub.sub2.sub3.sub4.domain.ext
    [2] => sub
    [3] => .sub2.sub3.sub4.domain
    [4] => .sub2.sub3.sub4.domain
    [5] => ext
)

Actual result:
--------------
Array
(
    [0] => sub.sub2.sub3.sub4.domain.ext
    [1] => sub.sub2.sub3.sub4.domain.ext
    [2] => sub
    [3] => .domain
    [4] => .domain
    [5] => ext
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-13 13:32 UTC] ralph at deboom dot biz
$value = 'sub.sub2.sub3.sub4.domain.ext';
if (preg_match("/^(([a-z0-9]+)(([\.|\-]{1}[a-z0-9]+)|(?R))*\.([a-z]{2,6}))$/i",$value,$match)) {	
 	print_R($match);
}

Thanks to a crappy textarea form I cant properly outline the code.. just indent it yourself ^^
 [2008-06-13 16:46 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

"If a capturing subpattern is matched repeatedly, it is the last portion
of the string that it matched that is returned."
- http://pcre.org/pcre.txt
 [2008-06-13 17:19 UTC] ralph at deboom dot biz
Ok, I've got quite the knowledge with PCRE regexp's but is it possible with PCRE to generate this result I wanted?

Because quite frankly I've tried on various ways. As in the whole day...
 [2008-06-13 17:41 UTC] felipe@php.net
The (?R) doesn't make sense to me in your regexp.
I think that ((?:[.-][a-z0-9]+)*) would be ok. :)

PS: Any other question, not reporting bug send to my email.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 13:01:30 2024 UTC