0

I got this regex:

$val = "(123)(4)(56)";
$regex = "^(\((.*?)\))+$";
preg_match_all("/{$regex}/", $val, $matches);

Can anyone please tell me why this matches only the last number (56) and not each set of numbers individually?

This is what $matches contains after the above regex runs:

array
  0 => 
    array
      0 => string '(123)(4)(56)' (length=12)
  1 => 
    array
      0 => string '(56)' (length=4)
  2 => 
    array
      0 => string '56' (length=2)
John
  • 898
  • 2
  • 9
  • 25
  • 2
    See this question: http://stackoverflow.com/questions/6571106/can-you-retrieve-multiple-regex-matches-in-javascript – Felix Kling Jul 09 '11 at 14:38
  • @Felix King: Any idea how to get the captures in PHP? Any chance? – hakre Jul 09 '11 at 14:47
  • heh never expected that such a simple thing would not be possible with PCRE. Is there any work around? I'm basically trying to match this: "(any_amount_of_numbers)" in a string, as long as it is the only thing a string contains, as many times as required. – John Jul 09 '11 at 14:48
  • See this question: http://stackoverflow.com/questions/6579908/get-repeated-matches-with-preg-match-all – hakre Jul 14 '11 at 08:45

1 Answers1

-1

As @develroot already has answered the way you want to use preg_match_all does not work, it will only return the last matching group, not all captures of that group. That's how regex works. At this point I don't know how to get all group catpures in PHP, I assume it's not possible. Might not be right, might change.

However you can work around that for your case by first check if the whole string matches your (repeated) pattern and then extract matches by that pattern. Put it all within one function and it's easily accessible (Demo):

$tests = explode(',', '(123)(4)(56),(56),56');   

$result = array_map('extract_numbers', $tests);

print_r(array_combine($tests, $result));

function extract_numbers($subject) {
    $number = '\((.*?)\)';
    $pattern = "~^({$number})+$~";
    if (!preg_match($pattern, $subject)) return array();
    $pattern = "~{$number}~";
    $r = preg_match_all($pattern, $subject, $matches);
    return $matches[1];
}
hakre
  • 193,403
  • 52
  • 435
  • 836