日志文件:
INFO:werkzeug:127.0.0.1 - - [20/Sep/2018 19:40:00] "GET /socket.io/?polling HTTP/1.1" 200 -
INFO:engineio: Received packet MESSAGE, ["key",{"data":{"tag1":12,"tag2":13,"tag3": 14"...}}]
我有兴趣仅从包含关键字的括号中提取文本,"key"
而不是从下面提取与正则表达式模式匹配的所有匹配项。
这是我到目前为止所尝试的:
import re
with open('logfile.log', 'r') as text_file:
matches = re.findall(r'\[([^\]]+)', text_file.read())
with open('output.txt', 'w') as out:
out.write('\n'.join(matches))
这将输出与正则表达式匹配的所有事件。output.txt 的所需输出如下所示:
"key",{"data":{"tag1":12,"tag2":13,"tag3": 14"...}}