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

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

问题描述:

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

Clearly, brew and bunny should match, but $matches only contains brew.

What should I do to have $matches contain all regex matches

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

显然,brew和bunny应匹配,但仅限$ match 含有啤酒。 p>

如何让$ matches包含所有正则表达式匹配 p> div>

You don't need the + after b in your regex.

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

Outputs

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