[英]Python Regular Expression to find exception in log file
我正在编写一个脚本来解析日志。 我需要一个正则表达式来查找日志文件中的 python 异常(如果有)。
例如,如果日志文件中有以下异常,则应以字符串格式返回以下异常。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'klj' is not defined
我试过下面的代码,它不会工作:
import urllib.request
import re
txt = urllib.request.urlopen(log_url).read().decode('utf-8')
exceptions = re.findall("(\w+)Error:", txt)
提前致谢
您的正则表达式只寻找以“错误”结尾的异常名称,但许多以“异常”结尾。 而且您在捕获组 1 中仅捕获“错误”之前的字符,因此您不会获得全名。
以下正则表达式将仅在行首查找异常名称并捕获全名:
import re
txt = """Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'klj' is not defined"""
exceptions = re.findall("""(?x) # Verbose mode
(?m) # Multiline mode (^ matches start of a line)
^ # Match start of a line
(?:\w+) # Match one or more word characters in a non-capturing group
(?:Error|Exception) # Match 'Error' or 'Exception':
(?=: ) # Lookahead assertion: next characters are ': '
""", txt)
print(exceptions)
印刷:
['NameError']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.