AlexW AlexW - 4 days ago 5
Python Question

Python/Regex - get start/end date and time from a string?

Im trying to extract the start date and time and the end date and time from this string, but have no idea how to do it, my first though was to find "Start Time & Date" and then get the next 20 chars from this point and try convert them to dates and times after, but im presuming there is a better way to do this?

body = """
Dear Customer,

Details of service affected:

Start Time & Date: 02/12/2016 02:00
Completion Time & Date: 02/12/2016 06:00

Details of Work:
Change faulty card
"""
if "Start Time & Date" in body:
#get next 20 chars?

Answer

Try with regex:

>>> import re
>>> 
>>> re.findall('\d{2}/\d{2}/\d{4} \d{2}:\d{2}', body)
['02/12/2016 02:00', '02/12/2016 06:00']
Comments