【发布时间】:2021-01-10 00:28:32
【问题描述】:
为了分析日志文件,我需要使用 python 和正则表达式提取异常类型。
异常类型始终包含子字符串“Exception”。
问题是子字符串“Exception”并不总是在他们名字的末尾。
此外,异常类型由未知数量的点组成。
预期行为:
输入
“2021 年 8 月 1 日:存在 System.InvalidCalculationException - 系统重新启动”
“2021 年 9 月 1 日:SuperSystem 识别出 System.IO.WritingException 询问用户下一步该做什么”
“2021 年 10 月 1 日:哦,不,不再是 InternalException.NullReference.NonCritical.User,我们应该修复它!”
输出
“System.InvalidCalculationException”
“System.IO.WritingException”
“InternalException.NullReference.NonCritical.User”
正则表达式的外观如何?
对于以“Exception”结尾的异常类型,我已经尝试使用“\w+[.]\w+[.]*Exception”。
但是如果异常类型包含更多的点并且“异常”不在末尾怎么办?
【问题讨论】:
标签: python python-3.x regex string