Php Regex:如何匹配重复的模式

时间:2013-08-25 14:43:40

标签: php regex

给出以下文字

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+值或将整个字符串分成行并在每行上运行爆炸,但我对正则表达式解决方案感兴趣我在哪里将第一列作为结果数组的第一个成员(实际上忘了在第一个问题草案中放置捕获括号),然后在包含数据的列之后,将每一行放入其中的专用数组......

4 个答案:

答案 0 :(得分: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));
}

只要每行不超过一个:,这就应该有效。在我看来,这也是最短也最快的写作方式。

答案 1 :(得分:0)

你走了:

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

使$matches[0]等于一系列匹配。然后,您想将其添加到行数组中:

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

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

答案 2 :(得分: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++)\s*:|\G\s*(?<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);

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

模式细节:

~               # pattern delimiter
^               # anchor for the line start (in multiline mode)
(?<item>\w++)   # named capture "item"
\s*:       
|               # OR
\G              # force the match to be contiguous from precedent
\s*             #
(?<value>\d++)  # named capture "value"
~m              # pattern delimiter, m modifier for multiline mode

答案 3 :(得分: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)