正则表达式模式以匹配python中的日期时间 [英] regex pattern to match datetime in python

查看:401
本文介绍了正则表达式模式以匹配python中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期时间的字符串,我正在尝试根据日期时间出现次数来拆分字符串,

I have a string contains datetimes, I am trying to split the string based on the datetime occurances,

data="2018-03-14 06:08:18, he went on \n2018-03-15 06:08:18, lets play"

我在做什么

out=re.split('^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$',data)

我会得到什么

["2018-03-14 06:08:18, he went on 2018-03-15 06:08:18, lets play"]

我想要什么:

["2018-03-14 06:08:18, he went on","2018-03-15 06:08:18, lets play"]

推荐答案

您希望使用至少1个空格和后跟日期(如pattern)进行拆分,因此,您可以使用

You want to split with at least 1 whitespace followed with a date like pattern, thus, you may use

re.split(r'\s+(?=\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}\b)', s)

请参见 regex演示

详细信息

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆