简体   繁体   English

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

[英]Regex repeated capturing group in PHP

i'm trying to get information from one file with routes, so for this work i chose regex, but i have the problem with the repeted information, for do a better question i will put what i have, and what i want to have: 我正在尝试从带有路由的一个文件中获取信息,因此对于这项工作,我选择了正则表达式,但是我对重复的信息有疑问,为了做一个更好的问题,我将放置我拥有的东西以及我想要拥有的东西:

So i have one file with: 所以我有一个文件:

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   

And i made this regex: 我做了这个正则表达式:

(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)+|)

My problem is with the end part (( +via +([0-9.]+), +([a-zA-Z0-9.]+|) +\\n)+|) because this regex get me this 我的问题是结尾部分(( +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';

But i want to get 但是我想得到

array[0]=>'10.141';

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

array[3]=>'10.142';

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

I test the regex in pages about regex and one of them tell me this: 我在有关正则表达式的页面中测试了正则表达式,其中之一告诉我:

Note:A repeated capturing group will only capture the last iteration. 注意:重复捕获组将仅捕获最后一次迭代。 Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data 将捕获组放在重复的组周围以捕获所有迭代,或者如果您对数据不感兴趣,则使用非捕获组

But i really dont know what is the mean of this and how to fix it. 但是我真的不知道这是什么意思,以及如何解决它。

Note: this is for get the file is about routes in cisco with show ip route 注意:这是为了获得文件关于与show ip route思科的show ip route

UPDATE 1 更新1

I change my regex to 我将正则表达式更改为

(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)*

This way i dont have the 这样我就没有了

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

But i dont have the repeted part 但是我没有重复的部分

I'm afraid you'll need to use another regular expression to get the repeated subpattern matches. 恐怕您将需要使用另一个正则表达式来获取重复的子模式匹配项。 So, you could do something like this: 因此,您可以执行以下操作:

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]);
        }
    }
}

Which gets the following data for your example file: 它将为您的示例文件获取以下数据:

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"
  }
}

Ok I've changed your regexp like this: 好的,我已经像这样更改了您的正则表达式:

$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);

This is output: 输出为:

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"
  }
}

It does not work because third via is missing 它不起作用,因为缺少第三个通孔

New version, line by line: 新版本,逐行:

$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);

Now it is working: 现在它正在工作:

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"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM