0

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.

1
  • 1
    import re re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z") Commented Aug 12, 2022 at 16:08

1 Answer 1

1

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])]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.