正则表达式在PHP中重复捕获组

时间:2014-12-09 13:33:28

标签: php regex

我正试图从一个带有路线的文件中获取信息,所以对于这项工作,我选择了正则表达式,但我有重复信息的问题,为了做一个更好的问题,我会把我拥有的,我想要的拥有:

所以我有一个文件:

Codes: C - Connected, S - Static, R - RIP, B - BGP,
       O - OSPF IntraArea (IA - InterArea, E - External, N - NSSA)
       A - Aggregate, K - Kernel Remnant, H - Hidden, P - Suppressed,
       U - Unreachable, i - Inactive

O E       0.0.0.0/0           via 10.140, bond1.30, cost 1:10, age 5  
                              via 10.141, bond1.31  
                              via 10.142, bond1.32  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O IA      10.138/29       via 10.140, bond1.30, cost 46, age 1029440  
C         10.141/29    is directly connected, bond2.35
C         10.141/29    is directly connected, bond2.35   

我做了这个正则表达式:

(S|R|B|O|A|K|H|P|U|i) +(IA|E|N|) +([0-9.]+)\/([0-9]+) +via +([0-9.]+), +([a-zA-Z0-9.]+|), +cost +([0-9]+:|)([0-9]+), +age +[0-9]+ +\n(( +via +([0-9.]+), +([a-zA-Z0-9.]+|) +\n)+|)

我的问题是结尾部分(( +via +([0-9.]+), +([a-zA-Z0-9.]+|) +\n)+|),因为这个正则表达式让我这个

array[0]=>' via 10.141, bond1.31  
           via 10.142, bond1.32';
array[1]=>'10.142';

array[2]=>'bond1.32';

但我想得到

array[0]=>'10.141';

array[1]=>'bond1.31';

array[3]=>'10.142';

array[4]=>'bond1.32';

我在关于正则表达式的页面中测试正则表达式,其中一个告诉我:

  

注意:重复捕获组仅捕获最后一次迭代。   将捕获组放在重复组周围以捕获所有组   迭代或使用非捕获组,如果你不是   对数据感兴趣

但我真的不知道这是什么意思以及如何解决它。

注意:这是为了获取文件与cisco中show ip route

的路线有关

更新1

我将正则表达式更改为

(S|R|B|O|A|K|H|P|U|i) +(IA|E|N|) +([0-9.]+)\/([0-9]+) +via +([0-9.]+), +([a-zA-Z0-9.]+|), +cost +([0-9]+:|)([0-9]+), +age +[0-9]+ +\n(?: +via +([0-9.]+), +([a-zA-Z0-9.]+|) +\n)*

这种方式我没有

array[0]=>' via 10.141, bond1.31  
       via 10.142, bond1.32';

但我没有重复的部分

2 个答案:

答案 0 :(得分:1)

我担心你需要使用另一个正则表达式来获得重复的子模式匹配。所以,你可以这样做:

preg_match_all("/(?:S|R|B|O|A|K|H|P|U|i) +(?:IA|E|N|) +[0-9.]+\/[0-9]+ +via +([0-9.]+), +([a-zA-Z0-9.]+|), +cost +(?:[0-9]+:|)[0-9]+, +age +[0-9]+ +\n((?: +via +[0-9.]+, +(?:[a-zA-Z0-9.]+|) +\n)*)/",$s,$matches,PREG_SET_ORDER);
foreach($matches as $id=>$match)
{
    unset($matches[$id][0]);
    if(isset($match[3])) {
        preg_match_all("/ +via +([0-9.]+), +([a-zA-Z0-9.]+|) +\n/",$match[3],$subpatternMatches,PREG_SET_ORDER);
        unset($matches[$id][3]);
        foreach($subpatternMatches as $spmid=>$spm) {
                unset($subpatternMatches[$spmid][0]);
                $matches[$id] = array_merge($matches[$id],$subpatternMatches[$spmid]);
        }
    }
}

获取示例文件的以下数据:

array(4) {
  [0]=>
  array(6) {
    [0]=>
    string(6) "10.140"
    [1]=>
    string(8) "bond1.30"
    [2]=>
    string(6) "10.141"
    [3]=>
    string(8) "bond1.31"
    [4]=>
    string(6) "10.142"
    [5]=>
    string(8) "bond1.32"
  }
  [1]=>
  array(2) {
    [1]=>
    string(6) "10.140"
    [2]=>
    string(8) "bond1.30"
  }
  [2]=>
  array(2) {
    [1]=>
    string(6) "10.140"
    [2]=>
    string(8) "bond1.30"
  }
  [3]=>
  array(2) {
    [1]=>
    string(6) "10.140"
    [2]=>
    string(8) "bond1.30"
  }
}

答案 1 :(得分:1)

好的,我已经改变了你的正则表达式:

$txt = "Codes: C - Connected, S - Static, R - RIP, B - BGP,
       O - OSPF IntraArea (IA - InterArea, E - External, N - NSSA)
       A - Aggregate, K - Kernel Remnant, H - Hidden, P - Suppressed,
       U - Unreachable, i - Inactive

O E       0.0.0.0/0           via 10.140, bond1.30, cost 1:10, age 5  
                              via 10.141, bond1.31  
                              via 10.142, bond1.32  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O IA      10.138/29       via 10.140, bond1.30, cost 46, age 1029440  
C         10.141/29    is directly connected, bond2.35
C         10.141/29    is directly connected, bond2.35   
";

$regexp = '#(.) +([A-Z]{1,2}) +([\d.]+/\d+) +via ([\d.]+), ([a-zA-Z0-9.]+), cost [\d:]+, age \d+ +(?:\n +via ([\d.]+), ([a-zA-Z0-9.]+))*#m';
$matches = [];
preg_match_all($regexp, $txt, $matches, PREG_SET_ORDER);
var_dump($matches);

这是输出:

array(4) {
  [0] =>
  array(8) {
    [0] =>
    string(125) "O E       0.0.0.0/0           via 10.140, bond1.30, cost 1:10, age 5  
                                  via 10.141, bond1.31"
    [1] =>
    string(1) "O"
    [2] =>
    string(1) "E"
    [3] =>
    string(9) "0.0.0.0/0"
    [4] =>
    string(6) "10.140"
    [5] =>
    string(8) "bond1.30"
    [6] =>
    string(6) "10.141"
    [7] =>
    string(8) "bond1.31"
  }
  [1] =>
  array(6) {
    [0] =>
    string(69) "O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  "
    [1] =>
    string(1) "O"
    [2] =>
    string(1) "E"
    [3] =>
    string(9) "10.112/23"
    [4] =>
    string(6) "10.140"
    [5] =>
    string(8) "bond1.30"
  }
  [2] =>
  array(6) {
    [0] =>
    string(69) "O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  "
    [1] =>
    string(1) "O"
    [2] =>
    string(1) "E"
    [3] =>
    string(9) "10.112/23"
    [4] =>
    string(6) "10.140"
    [5] =>
    string(8) "bond1.30"
  }
  [3] =>
  array(6) {
    [0] =>
    string(70) "O IA      10.138/29       via 10.140, bond1.30, cost 46, age 1029440  "
    [1] =>
    string(1) "O"
    [2] =>
    string(2) "IA"
    [3] =>
    string(9) "10.138/29"
    [4] =>
    string(6) "10.140"
    [5] =>
    string(8) "bond1.30"
  }
}

它不起作用,因为缺少第三个通道

新版本,逐行:

$txt = "Codes: C - Connected, S - Static, R - RIP, B - BGP,
       O - OSPF IntraArea (IA - InterArea, E - External, N - NSSA)
       A - Aggregate, K - Kernel Remnant, H - Hidden, P - Suppressed,
       U - Unreachable, i - Inactive

O E       0.0.0.0/0           via 10.140, bond1.30, cost 1:10, age 5  
                              via 10.141, bond1.31  
                              via 10.142, bond1.32  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O E       10.112/23       via 10.140, bond1.30, cost 46:1, age 2511  
O IA      10.138/29       via 10.140, bond1.30, cost 46, age 1029440  
C         10.141/29    is directly connected, bond2.35
C         10.141/29    is directly connected, bond2.35   
";

$grouped = [];
$i = 0;
foreach (explode("\n", $txt) as $line) {
    $matches = [];
    if (preg_match('#^(.) +([A-Z]{1,2}) +([\d.]+/\d+) +via ([\d.]+), ([a-zA-Z0-9.]+)#', $line, $matches)) {
        array_shift($matches);
        $grouped[++$i] = $matches;
    } else if(preg_match('#^ +via ([\d.]+), ([a-zA-Z0-9.]+)#', $line, $matches)){
        array_push($grouped[$i], $matches[1], $matches[2]);
    }
}
var_dump($grouped);

现在它正在运作:

array(4) {
  [1] =>
  array(9) {
    [0] =>
    string(1) "O"
    [1] =>
    string(1) "E"
    [2] =>
    string(9) "0.0.0.0/0"
    [3] =>
    string(6) "10.140"
    [4] =>
    string(8) "bond1.30"
    [5] =>
    string(6) "10.141"
    [6] =>
    string(8) "bond1.31"
    [7] =>
    string(6) "10.142"
    [8] =>
    string(8) "bond1.32"
  }
  [2] =>
  array(5) {
    [0] =>
    string(1) "O"
    [1] =>
    string(1) "E"
    [2] =>
    string(9) "10.112/23"
    [3] =>
    string(6) "10.140"
    [4] =>
    string(8) "bond1.30"
  }
  [3] =>
  array(5) {
    [0] =>
    string(1) "O"
    [1] =>
    string(1) "E"
    [2] =>
    string(9) "10.112/23"
    [3] =>
    string(6) "10.140"
    [4] =>
    string(8) "bond1.30"
  }
  [4] =>
  array(5) {
    [0] =>
    string(1) "O"
    [1] =>
    string(2) "IA"
    [2] =>
    string(9) "10.138/29"
    [3] =>
    string(6) "10.140"
    [4] =>
    string(8) "bond1.30"
  }
}