使用来自字符串的正则表达式解析日期和时间

时间:2016-09-28 03:21:58

标签: python regex

编辑:已经完成了!致谢

代码如下:

导入ast,重新

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)))

1 个答案:

答案 0 :(得分:1)

您可以迭代列表项,并匹配这些项目。

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