note 105978 deleted from regexp.reference.recursive by salathe

From: Date: Thu, 29 Sep 2011 14:17:17 +0000
Subject: note 105978 deleted from regexp.reference.recursive by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183527@lists.php.net to get a copy of this message
Note Submitter: dilbir_majo at yahoo dot com Reason: bad code ---- Recursive pattern match does not work with start of subject ^ option 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 ) 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);

« previous php.notes (#183527) next »