提取消息的正则表达式

2024-04-19 02:52:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个可以读取文件行的脚本。。有些行包含错误消息。。所以我做了一个循环(这里只针对一行)来找到这些行并提取消息:

import re

data = "15:31:17 TPP    E Line 'MESSAGE': There is a technical problem in the server."

if (re.findall(".*E Line.*",data)):
    err = re.match(r'\'MESSAGE\':\s(*$)',data)
    print err

执行此脚本时出现错误:/I希望它返回:

There is a technical problem in the server

Tags: theinre脚本消息messagedataserver
2条回答

试试这个:

import re

data = "15:31:17 TPP    E Line 'MESSAGE': There is a technical problem in the server."

r = re.compile("^.*E Line.*'MESSAGE':[ ]*([^ ].*)$")
m = r.match(data)
if m:
    err = m.group(1)
    print(err)

当然,您应该在循环之外编译regex。你知道吗

如果它们都遵循相同的格式,则不需要正则表达式:

>>> data = "15:31:17 TPP    E Line 'MESSAGE': There is a technical problem in the server."
>>> data.rsplit(':', 1)[1]
' There is a technical problem in the server.'

但如果你一定要用的话。。。你知道吗

>>> data = "15:31:17 TPP    E Line 'MESSAGE': There is a technical problem in the server."
>>> ms = re.search(r"'MESSAGE': (.*)$", data)
>>> ms.group(1)
'There is a technical problem in the server.'

如果需要,还可以提取其他信息:

>>> ms = re.match(r"(\d\d:\d\d:\d\d)\s+(\S+)\s+(\S+)\s+Line\s+'MESSAGE':\s+(.*)", data)
>>> ms.groups()
('15:31:17', 'TPP', 'E', 'There is a technical problem in the server.')

相关问题 更多 >