fork download
  1. import re
  2.  
  3. def check_time(text):
  4. pattern = r"^(?:1[0-2]|0?[1-9]):(?:[0-5]?[0-9])(?:\s?[AP]M)?$"
  5. return bool(re.search(pattern, text, flags=re.I))
  6.  
  7. print(check_time("12:45pm")) # True
  8. print(check_time("9:59 AM")) # True
  9. print(check_time("6:60am")) # False
  10. print(check_time("five o'clock")) # False
Success #stdin #stdout 0.02s 9388KB
stdin
Standard input is empty
stdout
True
True
False
False