When working with date and time data in Python, you may need to be able to match a 12-hour or 24-hour time format. This task can easily be accomplished using regular expressions. 

12hour time format

without seconds
(?:0?[0-9]|1[0-2])[-:][0-5][0-9]\s*[apAP][mM]

The above regular expression matches a time in the format of "hh:mm am/pm" or "hh:mmam/pm". Here's a breakdown of the different components:

  • (?:0?[0-9]|1[0-2]) matches the hour part of the time. It can be either a single digit hour (optional leading zero) ranging from 0 to 9, or a two-digit hour ranging from 10 to 12.
  • [-:] matches a hyphen or a colon, which separates the hour and minute parts of the time.
  • [0-5][0-9] matches the minute part of the time. It matches a digit from 0 to 5 followed by any digit from 0 to 9, representing a minute from 00 to 59.
  • \s* matches zero or more whitespace characters (spaces or tabs), allowing for optional spacing between the minute part and the AM/PM indicator.
  • [apPM][mM] matches the AM/PM indicator. It can be either "am" or "pm", case-insensitive.
With seconds
(?:0?[0-9]|1[0-2])[-:][0-5][0-9][-:][0-5][0-9]\s*[apP][mM]

 

def validate_12hr(t):
   import re
   pat = r"^(?:0?[0-9]|1[0-2])[-:][0-5][0-9]\s*[apP][mM]$"
   return bool(re.match(pat, t))

print(validate_12hr("12:34 pm"))
print(validate_12hr("09:45am"))
print(validate_12hr("3:00 PM"))
print(validate_12hr("8:15am"))
print(validate_12hr("13:00 pm"))#hour greater than 12
print(validate_12hr("02:76 am"))#invalid minutes
print(validate_12hr("12:00"))#missing AM/PM indicator

In the above example we added and at the start and end of the pattern respectively in order to match the full string.

def find_12hr(entry):
    import re
    pattern = "(?:0?[0-9]|1[0-2])[-:][0-5][0-9]\s*[apAP][mM]"
    return re.findall(pattern, entry)

text1 = "The meeting is scheduled for 3:30 pm. Please arrive by 2:45PM sharp."
matches = find_12hr(text1)
print(matches)

24hr time Format

without seconds
(?:[01][0-9]|2[0-3])[-:hH][0-5][0-9]

The regular expression above pattern matches a time in the format "hh:mm" in a 24-hour clock format. Let's break down the different components:

  • (?:[01][0-9]|2[0-3]) matches the hour part of the time. It can be either a single-digit hour (00-09) or a two-digit hour (10-23).
  • [-:hH] matches a hyphen, colon, "h" or "H", which separates the hour and minute parts of the time.
  • [0-5][0-9] matches the minute part of the time. It represents a digit from 0 to 5 followed by any digit from 0 to 9, allowing for values from 00 to 59.
with the seconds
(?:[01][0-9]|2[0-3])[-:hH][0-5][0-9][-:mM][0-5][0-9]

 

#without seconds
def validate_24hr(t):
    import re
    pattern = "^(?:[01][0-9]|2[0-3])[-:hH][0-5][0-9]$"
    return bool(re.match(pattern, t))

print(validate_24hr("12:34"))
print(validate_24hr("09-45"))
print(validate_24hr("18h30"))
print(validate_24hr("23:59"))
print(validate_24hr("12:33")) 
print(validate_24hr("12h34"))
print(validate_24hr("24:00"))

 

import re
def find_24hr(entry):
    pattern = r"\b(?:[01][0-9]|2[0-3])[-:hH][0-5][0-9][-:mM][0-5][0-9]\b"
    return re.findall(pattern, entry)

text = "The schedule includes times like 12:34:50, 09-45-20, 18h30m25, 23:59:30, and 24:00:35"
matches = find_24hr(text)
print(matches)