my $regex = qr/(\d\d):(\d\d)(?::(\d\d))?/; # 01234567890123456 my $string = "11:30 or 11:29:53"; #### $res = { 0 => ['11', '30', undef], 19 => ['11', '29', '53'] } #### $res->{pos ($string)} = [$1, $2, $3] while $string = /$regex/g; #### @res = $string = /$regex/g;