为什么我只得到第一个捕获组?

U. *_*ndl 3 regex perl regex-group

/sf/answers/161323851//sf/answers/2590295011/没有帮助我)

/proc/stat在分析 Linux 中的问题时,我开始编写一个小实用程序,但是我无法按照我想要的方式获取捕获组。这是代码:

#!/usr/bin/perl
use strict;
use warnings;

if (open(my $fh, '<', my $file = '/proc/stat')) {
    while (<$fh>) {
        if (my ($cpu, @vals) = /^cpu(\d*)(?:\s+(\d+))+$/) {
            print "$cpu $#vals\n";
        }
    }
    close($fh);
} else {
    die "$file: $!\n";
}
Run Code Online (Sandbox Code Playgroud)

例如,使用这些输入行,我得到输出:

> cat /proc/stat
cpu  2709779 13999 551920 11622773 135610 0 194680 0 0 0
cpu0 677679 3082 124900 11507188 134042 0 164081 0 0 0
cpu1 775182 3866 147044 38910 135 0 15026 0 0 0
cpu2 704411 3024 143057 37674 1272 0 8403 0 0 0
cpu3 552506 4025 136918 38999 160 0 7169 0 0 0
intr 176332106  ...
Run Code Online (Sandbox Code Playgroud)
 0
0 0
1 0
2 0
3 0
Run Code Online (Sandbox Code Playgroud)

所以匹配实际上有效,但我没有将捕获组放入@vals(perls 5.18.2 和 5.26.1)。

zdi*_*dim 7

仅捕获来自单个模式的最后一个重复匹配项。

相反,可以只拆分线,然后检查 - 并调整 - 第一个字段

while (<$fh>) {
    my ($cpu, @vals) = split;
    next if not $cpu =~ s/^cpu//;
    print "$cpu $#vals\n";
}
Run Code Online (Sandbox Code Playgroud)

如果split的返回的第一个元素不以cpu正则表达式替换失败开始,则该行被跳过。否则,您会得到后面的数字cpu(或空字符串),就像在 OP 中一样。

或者,可以使用您处理的生产线的特定结构

while (<$fh>) {
    if (my ($cpu, @vals) = map { split } /^cpu([0-9]*) \s+ (.*)/x) { 
        print "$cpu $#vals\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

正则表达式返回两个项目,每个项目都split在 中map,除了第一个按原样传入$cpu(可以是数字或空字符串),而另一个则生成数字。

这两者都在我的测试中产生了所需的输出。