#! perl -slw use strict; my $re_etime = qr[ (?{ our $ETIME = 0 }) (?: (\d{2}) (?{ $ETIME = 24 * $^N||0 }) - )? (?: (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N||0) }) : )? (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N) }) : (\d{2}) (?{ $ETIME += $^N }) ]x; for my $test ( '25:41', '19:25:41', '11-19:25:41' ) { our $ETIME; print "$test => $ETIME" if $test =~ $re_etime; } __DATA__ D:\Perl\test>261741 25:41 => 91541 19:25:41 => 69941 11-19:25:41 => 1020341 #### #! perl -slw use strict; my $re_etime = qr[ # Init a var to hold our value. (?{ our $ETIME = 0 }) # The whole line is optional # Capture 2 digits followed by - if present # What we captured $ETIME = ($^N) or 0 * 24 (?: (\d{2}) (?{ $ETIME = 24 * $^N||0 }) - )? # Optional \d{2}: if present, add them in, convert to seconds (?: (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N||0) }) : )? # Compulsory 2 digits , add em in and convert to seconds (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N) }) : # Compulsory colon # And the seconds. (\d{2}) (?{ $ETIME += $^N }) ]x; for my $test ( '25:41', '19:25:41', '11-19:25:41' ) { our $ETIME; # Gain access to our var #If the match succeeds, $ETIME has our value. print "$test => $ETIME" if $test =~ $re_etime; }