24-Hour Time in PY
Match 24-hour time formats HH:MM or HH:MM:SS with valid hour (00–23) and minute/second (00–59) ranges.
Try it in the PY tester →Pattern
regexPY
(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)? (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?")
input_text = "14:30"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
Hour alternation enforces 00–23, minute/second classes enforce 00–59. The optional :SS group makes seconds optional.
Examples
Input
14:30Matches
14:30
Input
23:59:59Matches
23:59:59
Input
25:00No match
—