正则表达式无法在Linux log4J文件中正常工作?

时间:2018-12-04 20:50:10

标签: regex python-2.7 log4j

我有一个由log4j生成的日志文件,我想用regex标识一行,这是我在regex101中测试过的。

这里是一个示例,它与regex101匹配:

regex = r"(Batch name): (.*?) (started)"
test_str = "Batch name: AS_ValueOnly started"

但是当我遍历python中的行时,此日志文件与regex不匹配。我进一步研究发现问题来自re.match函数,当行出现时它不匹配,

import re
log = "test.log"
with open(log, 'rb') as f:
    for line in f.readlines():
        if re.match(r"Batch name", line):
            print "found by regex"
            break
        if "Batch name" in line:
            print 'found by in line'
            break

这是一个运行结果:

$python gen_split_log.py

found by in line

Process finished with exit code 0

对此有任何想法吗?

1 个答案:

答案 0 :(得分:1)

知道了,我应该使用

re.search

不是

re.match