从python中的文件一次读取多行以进行正则表达式匹配

时间:2018-12-23 05:38:57

标签: regex re-python

我有一个日志文件,而我想使用正则表达式搜索包含多行的模式。据我所知,我一次只能读取和匹配一行。如何在内存中加载整个文件并进行模式匹配?

我有一个日志文件,我在其中寻找这样的断言消息:

*** ASSERT: 0xb83c6056

    *** type of assert: <xyz>

    *** file: .

    *** firmware revision: <xyz>

    *** firmware build timestamp: <xyz>

    *** seconds since 1970: <xyz>

    *** at address: <xyz>

    *** fault address: xyz>

    *** Error sub-type: <xyz>

我正在使用以下正则表达式。

re.compile(r'(\*){3}(\s){1}(ASSERT: )(.+)(\n){2}(\t)(\*){3}(\s){1}(type of assert: )(.+)(\n){2}(\t)(\*){3}(\s){1}(file: )(.+)(\n){2}(\t)(\*){3}(\s){1}(firmware revision: )(.+)(\n){2}(\t)(\*){3}(\s){1}(firmware build timestamp: )(.+)(\n){2}(\t)(\*){3}(\s){1}(seconds since 1970: )(.+)(\n){2}(\t)(\*){3}(\s){1}(at address: )(.+)(\n){2}(\t)(\*){3}(\s){1}(fault address: )(.+)(\n){2}(\t)(\*){3}(\s){1}(Error sub-type: )(.+)')

请告诉我如何改善正则表达式以便一次加载整个文件并进行正则表达式匹配。现在,据我所知,我只能逐行阅读。

0 个答案:

没有答案