简体   繁体   中英

Extract specific data from line in python

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?

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)

您可以在要提取的模式周围使用捕获组:

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

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