php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55811 preg_match recursive does not work with start of subject ^ option
Submitted: 2011-09-29 09:45 UTC Modified: 2011-09-30 08:55 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: sajidm at gmx dot net Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 5.3.8 OS: openSuse 11.1(Linux 2.6.27)
Private report: No CVE-ID: None
 [2011-09-29 09:45 UTC] sajidm at gmx dot net
Description:
------------
when i try 
php -r 'preg_match("!((\d+)(\s*,\s*(?R))*)$!","22222,  33333, 44444 ,55555 ,
abcd,defg,4537, 6537, 8774", $m);echo print_r($m,1);'
Array
(
    [0] => 4537, 6537, 8774
    [1] => 4537, 6537, 8774
    [2] => 4537
    [3] => , 6537, 8774
)

Similarly it should work with start of subject ^ option but it does not work.


It works fine without start of subject ^ option

Test script:
---------------
php -r 'preg_match("!^((\d+)(\s*,\s*(?R))*)!","22222,  33333, 44444 ,55555 , abcd,defg,4537, 6537, 8774", $m);echo print_r($m,1);'

Expected result:
----------------
Array
(
    [0] => 22222,  33333, 44444 ,55555
    [1] => 22222,  33333, 44444 ,55555
    [2] => 22222
    [3] => ,  33333, 44444 ,55555
)

Actual result:
--------------
Array
(
    [0] => 22222
    [1] => 22222
    [2] => 22222
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-29 14:03 UTC] sajidm at gmx dot net
Workaround: use wrapper function for preg_match

function preg_match_ex($pattern, $str, &$m, $recursive = false)
{
    if($recursive)
    {
        $pattern = "$pattern(\s*,\s*$pattern)*";
    }
    return preg_match("!^($pattern)!i", $str, $m);
}

Usage: preg_match_ex("\d+", "22222,  33333, 44444 ,55555,abcd,defg,4537, 6537,
8774", true);
 [2011-09-29 15:21 UTC] laruence@php.net
IMO the (?R) means match the regular pattern itself, in your case it like 
(^pattern(^pattern**** , so I think the result was worked `as expect`.
 [2011-09-30 05:05 UTC] laruence@php.net
so as I said above,  this is not a bug, I am going to mark this as bogus, thanks 
for you time to write these to us.
 [2011-09-30 05:05 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-09-30 05:05 UTC] laruence@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-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 01 16:03:34 2021 UTC