如何在php中获取所有正则表达式匹配?

时间:2013-09-06 22:11:58

标签: php regex

$pattern = '/b+[^\s-]*/';
$subject = 'brew and this is bunny';
preg_match($pattern, $subject, $matches);

显然,brew和bunny应匹配,但$ match只包含brew。

如果$ match包含所有正则表达式匹配

,我该怎么做

1 个答案:

答案 0 :(得分:1)

您的正则表达式中+之后不需要b

$str = 'brew and this is bunny';
preg_match_all('/b[^\s]+/', $str, $matches);
print_r($matches);

输出

Array
(
    [0] => Array
        (
            [0] => brew
            [1] => bunny
        )
)