CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Matching second value in a line

 



agdodson1
New User

Feb 7, 2013, 8:03 AM

Post #1 of 4 (497 views)
Matching second value in a line Can't Post

I am editing a program to scrape data from a given website. Currently, it searches for a given phrase and then grabs the next number it sees. For example, if that website has:

Temperature 28 30

the program grabs '28' with the following:


Code
($obd =~ m/Temperature.*?>\s*([0-9\.]+)\s*/is)[0],


I need the program to grab the second number. How do I do this given that the first number will not be constant?


BillKSmith
Veteran

Feb 7, 2013, 9:45 AM

Post #2 of 4 (490 views)
Re: [agdodson1] Matching second value in a line [In reply to] Can't Post


Code
use strict; 
use warnings;
use 5.14.0;
my $odb = 'Temperature 28 30';
my $temperature2 = $odb =~ s/Temperature \D+ \d+ \D+ (\d+) /$1/rx;
say $odb;
say $temperature2;

Good Luck,
Bill


Laurent_R
Enthusiast / Moderator

Feb 8, 2013, 7:56 AM

Post #3 of 4 (475 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:


Code
my $temperature2 = $1 if $odb =~ /Temperature\s+\d+\s+(\d+)/;


Or even:

Code
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:


Code
$t = "temp 43.5 37.2"; 
print "$1 $2\n" if $t =~ /temp ([\d.]+) ([\d.]+)/; # prints "43.5 37.2"



BillKSmith
Veteran

Feb 8, 2013, 1:04 PM

Post #4 of 4 (469 views)
Re: [Laurent_R] Matching second value in a line [In reply to] Can't Post

My regular expression was not intended to be a great solution. It was intended to emphasize the concept of match every field and return the one of interest. I felt that the unnecessary whitespace helped make that point. "Best Practice" requires matching whitespace explicitly. From that viewpoint, It would have been better had I used \s+ rather than \D+.

Many of our readers would find a true "Best Practice" solution to be obscure. You seem to have a good compromise.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives