需要使用python中的regex根据时间戳在字符串日志下面进行拆分



输入-logs=";2018-10-23T09:35:29Z发送消息2018-10-23To9:35:29 Z发送错误2018-10-23TO9:49:29 Z发出消息2018-10/23T10:01:49 Z发送消息"2018-10-23T10:05:29 Z发送错误";

我想使用regex按时间戳_ format="YYYY-MM-DDThh:MM:ssZ";这样我就可以有一个这样的列表,["2018-10-23T09:35:29Z发送消息","2018-10/23T09:35:39Z发送错误",…]

基本上,我想过滤所有的传输错误,并创建一个传输错误列表。我想用python做这个。

请帮忙。

使用Jeff的正则表达式模式:

logs = "2018-10-23T09:35:29Z sent message 2018-10-23T09:35:29Z transmit error 2018-10-23T09:49:29Z sent message 2018-10-23T10:01:49Z sent message 2018-10-23T10:05:29Z transmit error"
pattern = r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}Z"
# find the starts of each timestamp
split_indices = [m.start(0) for m in re.finditer(pattern, logs)]
# slice the log from one start to the next
output = [logs[i:j].strip() for i,j in zip(split_indices, split_indices[1:]+[None])]

最新更新