0

给出以下文本

bond0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
eth0: 11329920252 12554462    0    0    0     0          0      3561 13072970332 12899522    0    0    0     0       0          0

我需要捕获列值。我想到了这些行:

Regex: `(\w+):(?:\s+(\d+))+`
Php: `preg_match_all('/(\w+):(?:\s+(\d+))+/sim', $data, $regs)

但不幸的是,它只捕获第一列。

Array
(
    [0] => Array
        (
            [0] => dummy0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
            [1] => bond0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
            [2] => eth0: 11329920252 12554462    0    0    0     0          0      3561 13072970332 12899522    0    0    0     0       0          0
            [3] => ip6tnl0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
            [4] => lo: 51675995  100695    0    0    0     0          0         0 51675995  100695    0    0    0     0       0          0
            [5] => sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
            [6] => tunl0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
        )

    [1] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
        )

)

有什么建议吗?谢谢`

====编辑==== 只是要清楚一点:我知道我可以preg_match搜索\d+值或将整个字符串分成几行并在每一行上运行explode,但我对我有第一列的正则表达式解决方案感兴趣作为结果数组的第一个成员(实际上忘记在问题的初稿中放置捕获大括号),并在后面的列中包含数据,每一行都放在它的专用数组中......

4

4 回答 4

1

为什么使用preg_matchpreg_match_all根本不使用?

$results = array();
foreach (preg_split("/\r\n|\r|\n/", $data) as $line)
{
    list($key, $values) = explode(":", $line);
    $results[$key] = preg_split("/\s/", trim($values));
}

只要:每行不超过一个,这应该可以工作。在我看来,这也是写这个的最短和最快的方法。

于 2013-08-25T15:32:52.863 回答
0

干得好:

$data = explode("\n", $data);
$out = array();
foreach ($data as $d) {
    preg_match_all('/\s(\d+)/', $d, $matches);

放置$matches[0]等于匹配的数组。然后,您要将其添加到行数组中:

    $out[] = $matches[0];
}

您现在有一个锯齿状的行和列数组。因此,要参考第 2 行第 4 列,您可以转到$out[1][3].

于 2013-08-25T14:47:53.420 回答
0

你可以这样做:

$subject = <<<LOD
dummy0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
bond0:  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
eth0: 11329920252 12554462 0 0 0 0 0 3561 13072970332 12899522 0 0 0 0 0 0
ip6tnl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lo: 51675995 100695 0 0 0 0 0 0 51675995 100695 0 0 0 0 0 0
sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tunl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LOD;

$pattern = '~^(?<item>\w+):|\G\h+(?<value>\d+)~m';
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
$i=-1;
foreach($matches as $match) {
    if (isset($match['value']))
        $result[$i]['values'][] = $match['value'];
    else {
        $i++;
        $result[$i]['item'] = $match['item']; }
}
print_r($result);

您将获得您在 EDIT 中描述的格式。

图案细节:

~               # pattern delimiter
^               # anchor for the line start (in multiline mode)
(?<item>\w+)    # named capture "item"
:       
|               # OR
\G              # force the match to be contigous from precedent
\h+             #
(?<value>\d+)   # named capture "value"
~m              # pattern delimiter, m modifier for multiline mode
于 2013-08-25T15:12:35.787 回答
0

我知道您正在寻找preg_match解决方案,但如果您没有找到任何有用的答案

<?php
$val = "bond0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
eth0: 11329920252 12554462    0    0    0     0          0      3561 13072970332 12899522    0    0    0     0       0          0";
$arr1 = explode("\n",$val);
foreach ($arr1 as $value) {
    $exp = explode(":",$value);
    $ex = preg_replace('/\s+/', ' ',trim($exp[1]));
    $arr[$exp[0]] = explode(" ",$ex);
}
var_dump($arr);
?>

结果:

array (size=2)
  'bond0' => 
    array (size=17)
      0 => string '' (length=0)
      1 => string '0' (length=1)
      2 => string '0' (length=1)
      3 => string '0' (length=1)
      4 => string '0' (length=1)
      5 => string '0' (length=1)
      6 => string '0' (length=1)
      7 => string '0' (length=1)
      8 => string '0' (length=1)
      9 => string '0' (length=1)
      10 => string '0' (length=1)
      11 => string '0' (length=1)
      12 => string '0' (length=1)
      13 => string '0' (length=1)
      14 => string '0' (length=1)
      15 => string '0' (length=1)
      16 => string '0' (length=1)
  'eth0' => 
    array (size=17)
      0 => string '' (length=0)
      1 => string '11329920252' (length=11)
      2 => string '12554462' (length=8)
      3 => string '0' (length=1)
      4 => string '0' (length=1)
      5 => string '0' (length=1)
      6 => string '0' (length=1)
      7 => string '0' (length=1)
      8 => string '3561' (length=4)
      9 => string '13072970332' (length=11)
      10 => string '12899522' (length=8)
      11 => string '0' (length=1)
      12 => string '0' (length=1)
      13 => string '0' (length=1)
      14 => string '0' (length=1)
      15 => string '0' (length=1)
      16 => string '0' (length=1)
于 2013-08-25T15:26:03.160 回答