1

给定以下 2 行字符串,例如:

May 22 00:46:38.340  prod-lab03c-rd1 fpc4 XETH(4/2): %PFE-6: Link 0 XFP: Low Rx power warning set
[May 24 11:24:28.299 LOG: Notice] MIC(0/1) link 1 SFP receive power low  warning set

我想将以下数字存储在 3 个变量中:

[1] the 1st number after the "(", it could be 1 or more digits
[2] the 1st number after the "/", it could be 1 or more digits
[3] the first number after the "(L|l)ink" word, it could be 1 or more digits

你能帮我解决这个问题吗?

非常感谢

4

3 回答 3

3

要获得第一个之后的第一个数字(,我们可以使用.*\((\d+). 然后要得到 之后的第一个数字/,我们可以使用/(\d+)\). 然后获取 "link": 之后的第一个数字[lL]ink (\d+)。我们把这些放在一起得到

^.*\((\d+)/(\d+)\).*[lL]ink (\d+)

这三个数字将在三个组中

于 2012-05-24T18:55:14.697 回答
1

这在这里有效

\((.*?)/(.*?)\).*?[Ll]ink (\d+)

给你的输入它会回馈

group 1  group 2 group 3
4        2       0
0        1       1
于 2012-05-24T18:52:58.897 回答
0

Perl 中经过完整测试的示例:

#!/usr/bin/env perl

use strict;
use warnings;

my @a;

$a[0] = "May 22 00:46:38.340  prod-lab03c-rd1 fpc4 XETH(4/2): %PFE-6: Link 0 XFP: Low Rx power warning set\n";
$a[1] = "[May 24 11:24:28.299 LOG: Notice] MIC(0/1) link 1 SFP receive power low  warning set";

foreach (@a) {
    print "\$1=$1|\$2=$2|\$3=$3\n" if m!\((\d+)/(\d+)\).*?(?:L|l)ink\s+(\d+)!;
}

输出 :

$1=4|$2=2|$3=0
$1=0|$2=1|$3=1
于 2012-05-24T19:08:01.867 回答