2014-10-10 70 views
1

我想获取子字符串形式的文件。但我只能得到一个子字符串,但无法得到呃第二个。正则表达式提取属性

这里是线例如:

<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

如果你解析XML使用XML解析器,例如XML ::嫩枝https://metacpan.org/pod/XML::Twig – AKHolland 2014-10-10 11:33:49

+0

其位原油way'if($线=〜 /testCase\s+name\=\"(.*?)\"\s+status\=\"(.*?)\"/i) { print $ 1,$ 2; }' – run 2014-10-10 11:39:22

回答

2

替换此行:

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

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

你处理XML文件?使用适当的解析器,例如xsh

open test.xml ; 
for //testCase echo @name @status ;