# File: matchTime.pl # Title: Find in a sentence where is time # any number of blanks is (\s{0,}) # \s is blank and {0,} means any number greater or equal to 0 # [0-9] means any char between 0 and 9 (a digit) # Author: Mihaela Malita $s = "Come today at 12:30PM in my office"; # find 2:30 PM print "Finds time expression in sentence $s.\n"; $s =~ m/[0-9][0-9]:[0-9][0-9](AM|PM)/; # variable $1 takes the value of the match print "result= $&\n"; # prints exact match $& # same but extra () puts everything in $1 $s =~ m/([0-9][0-9]:[0-9][0-9](AM|PM))/; # variable $1 takes the value of the match print "result= $1 \n"; # prints Time # if you which to capture also blanks $s = "Come today at 12:30 PM in my office"; # find 2:30 PM $s =~ m/[0-9][0-9]:[0-9][0-9](\s{0,})(AM|PM)/; print "result= $&\n"; # prints exact match $&= 12:30 PM $s = "Come today at 5:30 AM in my office"; # find 2:30 PM $s =~ m/([0-9]|[0-9][0-9]):[0-9][0-9]\s(AM|PM)/; # 1 digit or 2 digits for hour,only 1 space,then AM or PM print "result= $&\n"; # prints exact match $&= 5:30 PM exit;