【发布时间】:2017-05-25 10:14:07
【问题描述】:
我为我编写的这个脚本生成了一个日志文件,我想查找错误。
日志文件如下所示:
24/05/2017 07:39:55 PM | DEBUG | Reading config file "FRGD-1_1.cfg". | main.py:81 - set_configparser()
...
...
24/05/2017 07:39:55 PM | DEBUG | Reading config file "FRGD-1_10.cfg". | main.py:81 - set_configparser()
...
...
24/05/2017 08:39:55 PM | DEBUG | Reading config file "RFGD-1_5.cfg". | main.py:81 - set_configparser()
...
25/05/2017 09:53:47 PM | ERROR | KeyError. There is an issue with the config file for this download. Please check the config file for errors. | script.py:137 - main()
...
...
24/05/2017 10:39:55 PM | DEBUG | Reading config file "DPGD-4_15.cfg". | main.py:81 - set_configparser()
...
...
24/05/2017 11:39:55 PM | DEBUG | Reading config file "ZXTD-3_1.cfg". | main.py:81 - set_configparser()
...
25/05/2017 03:53:47 AM | ERROR | KeyError. There is an issue with the config file for this download. Please check the config file for errors. | script.py:137 - main()
...
...
24/05/2017 07:39:55 PM | DEBUG | Reading config file "FRGD-1_1.cfg". | main.py:81 - set_configparser()
...
...
24/05/2017 07:39:55 PM | DEBUG | Reading config file "FRGD-1_10.cfg". | main.py:81 - set_configparser()
我想捕获所有引发错误的配置文件的名称。
为了识别线条,我寻找具有以下特征的线条:
DD/MM/YYYY HH:MM:SS PM | DEBUG | Reading config file "<file_name>.cfg".
直接在之前:
DD/MM/YYYY HH:MM:SS PM | ERROR |
在上面的例子中,它们是“RFGD-1_5.cfg”和“ZXTD-3_1.cfg”。
我正在使用 Python 来运行这个正则表达式,因此我将整个文件连接到一行并使用了以下正则表达式
'(?<=Reading config file "(.{13})").+?\| ERROR \|'.
不幸的是,它不起作用。
任何帮助都会很棒。
【问题讨论】:
标签: python regex regex-lookarounds regex-greedy