Extract Time Using Re In Python
How can I extract the time from the following array? Right now I am trying with re data = ['Mys--dreyn M (00:07:04): Yes', ' of course.\r\n'] time_search = re.search('(*)', ''.jo
Solution 1:
Just an alternative way to extract the time - use the dateutil.parser
in the "fuzzy" mode:
In [1]: from dateutil.parser import parse
In [2]: s = "'Mys--dreyn M (00:07:04): Yes', ' of course.\r\n'"
In [3]: dt = parse(s, fuzzy=True) # dt is a 'datetime' object
In [4]: dt.hour
Out[4]: 0In [5]: dt.minute
Out[5]: 7In [6]: dt.second
Out[6]: 4
And, as far as your regular expression goes, I'd improve it by checking the digit pairs as well:
time_search = re.search('\((\d{2}:\d{2}:\d{2})\)', "".join(data), re.IGNORECASE)
Post a Comment for "Extract Time Using Re In Python"