我有一个由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
你知道吗?