I would like to match the time string python with python re module. My code is like this:
import re
def match_time(el):
year = '(?:19|20)[0-9]{2}'
month = '(?:0?[1-9]|1|[0-2])'
day = '(?:0?|[1-9]|[1-2][0-9]|3[0-1])'
hour = '(?:[0-1]?[0-9]|2[0-4])'
minute = '(?:[0-5]?[0-9])'
second = '(?:[0-5]?[0-9])'
sep = '\s?'
date_pattern = '{year}[/-]{month}[/-]{day}'.format(year=year, month=month, day=day)
time_pattern = '(?:{sep}{hour}:{minute}:{second})?'.format(sep=sep, hour=hour, minute=minute, second=second)
p = date_pattern + time_pattern
m = re.findall(p, el)
return m
el = '2019/3/23 0:11:01 fxxxxxff fff'
m = match_time(el)
print(m)
The output of this is 2019/3/. What error it is with my code please ? I hope the function can match sentences like both 2019/3/23 0:11:01 fxxxxx ffff and 2019/3/23 fffdddd dddwww, so I added a ? after the time_pattern, but the output is weird. How could I make it work please ?