#!perl
my $sentence = 'A marathon is 26.2 miles.';
$sentence =~ s! # substitution
\b((?:\d+)?(?:\.\d+)?)\b # find a number
\s+ # followed by space
(kilometer|mile)s? # "km"s or "m"s
! #
sprintf "%.1f %s%s", # convert,
$1 * ($2eq'mile'?1.609:.6214), # "m"s, use 1.609
($2eq'mile'?'kilometer':'mile'), # "km"s, use .6214
($1*($2eq'mile'?1.609:.6214))==1? # the math
'' : 's' # pluralize or not
!xeg; #
print $sentence, "\n";
[close]
Permanent link · http://querylog.com/q/How+many+km+in+a+marathon%3F