Home: Perl Programming Help: Beginner:
Perl pattern match to end of line



rdowd
New User

Jun 13, 2013, 6:46 AM


Views: 906
Perl pattern match to end of line

Hi All,

I'm trying to use Perl to read in file contents and grab some values (via pattern match) in the file.

My File contents is as below:

Total number of testcases executed: 100
Number of passing testcases: 20
Number of failing testcases: 80
..more file contents here not shown..

I've read the file into a variable and I want to get the value "20" out of the line:
"Number of passing testcases: 20"

I'm using the below:

if ($fileContents =~ m/Number of passing testcases*/s) {
print("Match\n");
print("LINE = [$&]\n");
} else {
print("No Match\n");
}

How to I grab the value of "20" on that line. As this number could be any number?

Any help would be great.
ps: I'm using Perl 5.10.1

Thanks - Ro


FishMonger
Veteran / Moderator

Jun 13, 2013, 7:44 AM


Views: 901
Re: [rdowd] Perl pattern match to end of line


Code
if ($fileContents =~ /Number of passing testcases executed:\s*(\d+)/) { 
print("Match\n");
print("LINE = [$1]\n");
}
else {
print("No Match\n");
}



rdowd
New User

Jun 13, 2013, 8:02 AM


Views: 899
Re: [FishMonger] Perl pattern match to end of line

Nice one - thanks!
Ro