0

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('(*)', "".join(data), re.IGNORECASE)
if time_search:
    time = time_search.group(1)
    print time

2 Answers 2

3

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]: 0

In [5]: dt.minute
Out[5]: 7

In [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)
Sign up to request clarification or add additional context in comments.

Comments

2

You need to fix re expression:

time_search = re.search('\((.*)\)', "".join(data), re.IGNORECASE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.