#!PERL.EXE -w
use strict;

print "Enter the time not necessarily in the format of \"HH:MM xm\" (^Z to quit)\n";
while (1) {
    print "\n\n> ";
    my $line=<STDIN>;
    $line or last; #   unless ($line) { last; }
    chomp($line);

    # $i              1             2             3
    if ( $line =~ m/\b([0-9]?[0-9]):([0-5][0-9]) ?(am|pm|AM|PM)\b/ )
    {
        print "EXTRACTED:\nhours:   $1\nminutes: $2\nam/pm:   $3";
    }
    else
    {
        print "WRONG TIME FORMAT!\n";
    }
}