具有拆分功能的 Perl 正则表达式
Perl regular expression with split function
我编写了一个函数来解析在 linux 框上执行的命令的输出。我希望函数解析命令输出和 return 输出到调用函数。我想解析输出以获得 0/9999 (0%)
和 return 0 和 9999。(注意:这些数字不是静态的。它可以是任意数量的数字。例如 4567/100000 (45%)
)。我猜我使用的正则表达式不正确。我得到的输出为 0.
命令输出:
Connecting to host 8.2.0.2, port 7775
[ 4] local 9.1.1.2 port 37675 connected to 8.2.0.2 port 7775
[ ID] Interval Transfer Bandwidth Total Datagrams
[ 4] 0.00-2.40 sec 5.49 MBytes 19.2 Mbits/sec 10000
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 4] 0.00-2.40 sec 5.49 MBytes 19.2 Mbits/sec 5.958 ms 0/9999 (0%)
[ 4] Sent 9999 datagrams
iperf Done.
函数:
sub test_output {
my ($self) = @_;
my $packets = 0;
$self->{output_obj}->_exec('enter the command here');
my @output = $self->{output_obj}->out();
foreach my $line (@output) {
if ($line =~ /(\d+\/\d+\s.*)/) {
packets =
}
}
my @values = split('/', $packets);
foreach my $val (@values) {
return "$val\n";
}
}
函数调用:
$self->{'client'} = $self->{'exec_obj'}->test_output()
函数的预期 return 值:
lost : 0
total : 9999
lost percentage: 0%
sub test_output {
my $self = shift;
$self->{output_obj}->_exec('enter the command here');
my @output = $self->{output_obj}->out();
foreach my $line (@output) {
if ($line =~ m/(\d+)\/(\d+)\s\((\d+%)\)/) {
my ($lost, $total, $percentage) = (, , );
return ($lost, $total, $percentage);
}
}
}
my ($lost, $total, $percentage) = $self->{'exec_obj'}->test_output()
我编写了一个函数来解析在 linux 框上执行的命令的输出。我希望函数解析命令输出和 return 输出到调用函数。我想解析输出以获得 0/9999 (0%)
和 return 0 和 9999。(注意:这些数字不是静态的。它可以是任意数量的数字。例如 4567/100000 (45%)
)。我猜我使用的正则表达式不正确。我得到的输出为 0.
命令输出:
Connecting to host 8.2.0.2, port 7775
[ 4] local 9.1.1.2 port 37675 connected to 8.2.0.2 port 7775
[ ID] Interval Transfer Bandwidth Total Datagrams
[ 4] 0.00-2.40 sec 5.49 MBytes 19.2 Mbits/sec 10000
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 4] 0.00-2.40 sec 5.49 MBytes 19.2 Mbits/sec 5.958 ms 0/9999 (0%)
[ 4] Sent 9999 datagrams
iperf Done.
函数:
sub test_output {
my ($self) = @_;
my $packets = 0;
$self->{output_obj}->_exec('enter the command here');
my @output = $self->{output_obj}->out();
foreach my $line (@output) {
if ($line =~ /(\d+\/\d+\s.*)/) {
packets =
}
}
my @values = split('/', $packets);
foreach my $val (@values) {
return "$val\n";
}
}
函数调用:
$self->{'client'} = $self->{'exec_obj'}->test_output()
函数的预期 return 值:
lost : 0
total : 9999
lost percentage: 0%
sub test_output {
my $self = shift;
$self->{output_obj}->_exec('enter the command here');
my @output = $self->{output_obj}->out();
foreach my $line (@output) {
if ($line =~ m/(\d+)\/(\d+)\s\((\d+%)\)/) {
my ($lost, $total, $percentage) = (, , );
return ($lost, $total, $percentage);
}
}
}
my ($lost, $total, $percentage) = $self->{'exec_obj'}->test_output()