Python 正则表达式搜索查找
我已经为我编写的脚本生成了一个日志文件,我想查找错误 日志文件如下所示:Python 正则表达式搜索查找,python,regex,regex-lookarounds,regex-greedy,Python,Regex,Regex Lookarounds,Regex Greedy,我已经为我编写的脚本生成了一个日志文件,我想查找错误 日志文件如下所示: 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 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".
在上面的示例中,它们是“RFGD-1_5.cfg”和“ZXTD-3_1.cfg”
我使用Python来运行这个正则表达式,因此我将整个文件连接到一行,并使用以下正则表达式
'(?<=Reading config file "(.{13})").+?\| ERROR \|'.
”(?直觉上,我们会说匹配ERROR
之前的最后一个配置文件
import regex as re
rx = re.compile(r'''
(?<=config\ file\ "([^"]+)"(?s:.*?))
\|\ ERROR\ \|
''', re.VERBOSE)
print(rx.findall(string))
# ['RFGD-1_5.cfg', 'ZXTD-3_1.cfg']
将regex作为re导入
rx=重新编译(r''
(?嗨,简。谢谢你的时间。这就像魔术一样有效。我可以请你澄清一下你在这一部分做什么:(?s:.*)@jerry_doe:是的,请看修改后的答案。非常感谢你,简。这太棒了。我非常感谢。
import regex as re
rx = re.compile(r'''
(?<=config\ file\ "([^"]+)"(?s:.*?))
\|\ ERROR\ \|
''', re.VERBOSE)
print(rx.findall(string))
# ['RFGD-1_5.cfg', 'ZXTD-3_1.cfg']
(?<= # a pos. lookbehind
config file # config file literally
\"([^\"]+)\" # capture anything between "..."
(?s:.*?) # turns on single line mode (dot matches all including newline),
# lazily, expanded as needed
) # closing lookbehind
\| ERROR \| # | ERROR | literally