
Laurent_R
Enthusiast
Feb 8, 2013, 7:56 AM
Post #3 of 4
(212 views)
|
|
Re: [BillKSmith] Matching second value in a line
[In reply to]
|
Can't Post
|
|
I know that the /x modifier is part of the recommended best practices, but I nonetheless think that adding spaces within a regular expression does not help readability when spaces are part of what you want to match in the string. I would write the relevant line as follows:
my $temperature2 = $1 if $odb =~ /Temperature\s+\d+\s+(\d+)/; Or even:
my $temperature2 = $1 if $odb =~ /Temperature +\d+ +(\d+)/; BTW, all these regexes will fail if the temperature values may contain a decimal point. If such is the case, change "\d" to a character class such as [\d.], as in the following example:
$t = "temp 43.5 37.2"; print "$1 $2\n" if $t =~ /temp ([\d.]+) ([\d.]+)/; # prints "43.5 37.2"
|