0

I have a list of different time formats and I want to extract only the HH:MM:SS format in 24 Hour time format. I used regex pattern to do like below

import re
from dateutil import parser

time_list = ['7:30 pm', '8:00 pm', '4:00 pm', '4pm. 1', '2 hour', '5 minutes', '1 hour', '30 minutes', 'one hour']
times_form = []
for t in time_list:
    regex = re.compile(r'/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/')
    if regex.match(t):
        dt = parser.parse(t)
        times_form.append(str(dt.hour)+":"+ str(dt.minute)+":"+ str(dt.second))
      

But I am getting None. The expected output is ['19:30:00', '20:00:00', '16:00:00']

I got the regex pattern from Regular expression for matching HH:MM time format

imhans4305
  • 677
  • 1
  • 16

0 Answers0