正则表达式提取属性

时间:2014-10-10 11:30:11

标签: perl

我试图让子字符串形成一个文件。但我只能得到一个子字符串,但无法得到第二个。

以下是行示例:

<testPoolResult name="Pool" loop="1" currency="false" randomized="false" startTime="01.01.70_02:10:059""01.01.70_02:11:000" duration="0">

<testCase name="Test" status="SUCCESS" iteration="1" startTime="01.01.70_02:10:059" endTime="01.01.70_02:10:059" duration="0">

<testAction name="pend" status="SUCCESS" iteration="1" startTime="01.01.70_02:10:059" endTime="01.01.70_02:10:059" duration="0"/>

我需要解析具有testCase的行并提取测试的名称和测试的状态。

这是我的代码:

use warnings;
open( my $fh, "<", "test.txt" ) or die "can not open the file $! \n";
while ( my $line = <$fh> ) {
    if ( $line =~ /testCase\s+name[=""](.+?)\s+status[=""](.+?)/x ) {
        print $1. " " . $2 . "\n";
    }
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

替换此行:

if($line =~ /testCase\s+name[=""](.+?)\s+status[=""](.+?)/x)

if ($line =~ /testCase\s+name="(.+?)"\s+status="(.+?)"/x)

答案 1 :(得分:0)

您正在处理XML文件吗?使用适当的解析器,例如xsh

open test.xml ;
for //testCase echo @name @status ;
相关问题