Date and Time String Scan
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Trying again, the time in this example returns "00:00:00" not "00:02:55". Thanks!

import re
import datetime
line="07/12/18 00:02:55 - Process(28389.1166381) User(mqsystem) Program(amqrmppa)"
match = re.search(r'\d{2}/?\d{2}/?\d{2,4} \d{2}:\d{2}:\d{2}', line)

if match is not None:
    dte = datetime.datetime.strptime(match.group(), '%m/%d/%y %H:%M:%S').date()
    dte='{0:%Y-%m-%d %H:%M:%S}'.format(dte)
   print(dte) # Displays 2018-07-12 00:00:00 NOT  2018-07-12 00:02:55
awarded to kostasx
Tags
regex
python3

Crowdsource coding tasks.

1 Solution

Winning solution

import re
import datetime
line="07/12/18 00:02:55 - Process(28389.1166381) User(mqsystem) Program(amqrmppa)"
match = re.search(r'\d{2}/?\d{2}/?\d{2,4} \d{2}:\d{2}:\d{2}', line)

if match is not None:

    from dateutil.parser import parse
    dte = parse(match.group())
    print(dte)
Hi, heres an added stuff @broadreach might be needing to look for in next post print(dte.strftime("%d/%m/%Y %H:%M:%S"))
SilverHood Apps 3 months ago
View Timeline