1

I have one variable which contains many information and I want to extract some data from that line but not getting expected result.

Following is code

import re

line = "2019-08-10 00:57:24 [Thread-0] DEBUG CSConnection - Serial : LOG: ABC=1 XYZ=42 PQR=0015236800MOSAER"
result = re.search((r" (.*?):(.*?):"), line).group(0)
print(result)

Following output I am expecting

00:57

Following output I am getting.

 00:57:

What is missing to get the result?

2 Answers 2

2

If you know what you are searching for always try to be as precise as possible. This will make it much more robust.

result = re.search(r"(\d{2}):(\d{2})", line).group(0)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a capture group around the pattern you want to extract:

result = re.search(r" (.*?:.*?):", line).group(1)

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.