Here's my two minutes (plus two to clean it up and comment it):
#!/usr/bin/perl
my $a = "1 + 2:30 - 0:45:30.2 - 1:40.243";
$a =~ s{
\b (\d+) # opening hour or minute
: (\d+ (?:\.\d+)?) # minute or second (decimals optional)
(?:
: (\d+ (?:\.\d+)?) # seconds (if present) (decimals optional)
)?
}{
defined($3) # had seconds ?
? $3+60 * ($2+60 * $1)
: $2+60 * $1
}xge;
print "($a)\n";
# prints
(1 + 150 - 2730.2 - 100.243)
That regex will also work in javascript as well.
Various assumptions were made that weren't described in the problem
description.
Paul
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/