Bug #55811 [Opn]: preg_match recursive does not work with start of subject ^ option
| From: | laruence@php.net | Date: | Thu, 29 Sep 2011 15:21:55 +0000 |
| Subject: | Bug #55811 [Opn]: preg_match recursive does not work with start of subject ^ option | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163979@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55811&edit=1
ID: 55811
Updated by: laruence@php.net
Reported by: sajidm at gmx dot net
Summary: preg_match recursive does not work with start of
subject ^ option
Status: Open
Type: Bug
Package: *Regular Expressions
Operating System: openSuse 11.1(Linux 2.6.27)
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-09-29 14:03:09] 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 09:45:38] 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
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55811&edit=1