字符串中进程ID的preg_match_all


preg_match_all on process ids in string

我试图从字符串中获取某些id,但无法使其工作。我得到的价值观是我没有预料到的。

这就是我所拥有的:

<?php
$grep = ' 7027 ?        S      0:00 nginx: worker process                                          
 7632 ?        S      0:00 sh -c ps ax | grep nginx
 7634 ?        S      0:00 grep nginx
16117 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf';

if ( preg_match_all('~([0-9]+) '?(.*?)nginx:~si', $grep, $matches) )
{   
    echo '<pre>';
    print_r($matches);
    echo '</pre>';
}

我对~([0-9]+) '?(.*?)nginx:的期望是它将匹配这两行:

7027 ?        S      0:00 nginx: worker process  
16117 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf';

我特别关注他们的进程id,在本例中是:702716117

但我得到的是:70277632

我的正则表达式应该如何获得我想要的数据?

下面是一个演示:http://codepad.viper-7.com/2R5OfF

s修饰符强制.跨换行序列匹配。您需要删除它,然后可以按照如下方式简化正则表达式,以返回您所要处理的进程id。

preg_match_all('~('d+).*nginx:~i', $grep, $matches);
print_r($matches[1]);

输出

Array
(
    [0] => 7027
    [1] => 16117
)