Reputation: 94
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.
Upvotes: 0
Views: 454
Reputation: 175
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])]
Upvotes: 1