user1362796
user1362796

Reputation: 63

Regex to extract attributes

I am trying to get the sub-strings form an file.but i am only able to get the one sub-string but unable to get eh second one .

Here is the line example :

<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"/>

I need to parse the line which is having testCase and extract the name of the test and status of the test.

here is my code :

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";
    }
}

Can any one help me ?

Upvotes: 1

Views: 60

Answers (2)

choroba
choroba

Reputation: 241918

Are you processing an XML file? Use a proper parser, e.g. xsh:

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

Upvotes: 0

Toto
Toto

Reputation: 91428

Replace this line:

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

with

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

Upvotes: 2

Related Questions