Perl, regex searches
From FreeBSDwiki
To extract values from a string using regular expressions in Perl, the format looks like this:
$time = '18:03:34'; (my $hours, my $minutes, my $seconds) = ($time =~ /(\d\d):(\d\d):(\d\d)/);
Beware of trying to use matches based on $1, $2, $3, and such in expressions after the actual match - the results are unpredictable. For example, consider the following code:
$time = '18:03:34'; $time =~ /(\d\d):(\d\d):(\d\d)/; $hours = $1; $minutes = $2; $seconds = $3; print "The first time is $hours:$minutes:$seconds.\n"; $time = '19:04:55'; $time =~ /(\d\d):(\d\d):(\d\d)/; $hours = $1; $minutes = $2; $seconds = $3; print "The second time is $hours:$minutes:$seconds.\n";
This code produces the following results:
The first time is 18:03:34. The second time is 18:03:34.
Not what you expected, was it? So remember, if you are using match values OUTSIDE the regular expression they occur in, ALWAYS use the form (my $one,my $two,my $three) = ($string =~ /(one)...(two)...(three)/); or you'll get into trouble.