user2947950
user2947950

Reputation: 129

Parsing date and time using regex from a chuck of string

EDIT: DONE ALREADY! THANKS

Code as below:

import ast,re

a = "('=====================================', '30/06/2016 17:15 T001 -------------------------------')"

t=ast.literal_eval(a)

z=re.compile(r"(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)")

m = z.match(t[1])

if m:
    print("date: {}, time {}".format(m.group(1),m.group(2)))

Upvotes: 1

Views: 59

Answers (1)

falsetru
falsetru

Reputation: 369074

You can iterate list items, and match those items.

t = ast.literal_eval(a)  # assuming `t` is an iterable
z = re.compile(r"(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)")
for item in t:   # <-----
    m = z.match(item)
    if m:
        print("date: {}, time {}".format(m.group(1), m.group(2)))
        # break  # if you want to get only the first matched data/time pair

Upvotes: 1

Related Questions