I want to judge the time from 0:00 to 23:59 with a regular expression. I want to support single-digit hours and double-digit hours and minutes.
If it is below, 29:59 will also be judged as the time.
re.match(r'([0-2]?[0-9]):([0-5]?[0-9])', '29:59')
<re.Match object; span=(0, 5), match='29:59'>
If it was as follows, it worked as expected.
re.match(r'([0-1]?[0-9]|2[0-3]):([0-5]?[0-9])', '23:59')
<re.Match object; span=(0, 5), match='23:59'>
re.match(r'([0-1]?[0-9]|2[0-3]):([0-5]?[0-9])', '24:00')
If there is another good way, please comment!
Recommended Posts