简体   繁体   中英

Need to split below string log based on timestamp using regex in python

input - 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"

I want to use regex to split by the timestamp_format = "YYYY-MM-DDThh:mm:ssZ" so I can have a list like this, ["2018-10-23T09:35:29Z sent message", "2018-10-23T09:35:29Z transmit error",...]

Basically I want to filter all transmit errors and create a list of transmit errors. I want to do this in python.

Please help.

Using Jeff's regex pattern:

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}T\d{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])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM