正则表达式在 linux log4J 文件中不能正常工作?
Regex not working properly in linux log4J file?
我有一个由 log4j 生成的日志文件,我想用正则表达式识别特定的行,我已经在 regex101 中测试过。
这是一个例子,它匹配 regex101:
regex = r"(Batch name): (.*?) (started)"
test_str = "Batch name: AS_ValueOnly started"
但是当我遍历 python 中的行时,此日志文件无法与正则表达式匹配。我进一步挖掘,发现问题来自 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
对此有什么想法吗?
知道了,我应该用
re.search
没有
re.match
我有一个由 log4j 生成的日志文件,我想用正则表达式识别特定的行,我已经在 regex101 中测试过。
这是一个例子,它匹配 regex101:
regex = r"(Batch name): (.*?) (started)"
test_str = "Batch name: AS_ValueOnly started"
但是当我遍历 python 中的行时,此日志文件无法与正则表达式匹配。我进一步挖掘,发现问题来自 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
对此有什么想法吗?
知道了,我应该用
re.search
没有
re.match