$time =~ /^(\d{1,2}):(\d\d)\s?([APM]{2})$/ #----------------------------^ Added this # And actually, now that I come to think of it, the # dealing with AM or PM problems may be better sorted using: $time =~ /^(\d{1,2}):(\d\d)\s?(AM|PM)$/