我试图在基于 regex 的日志文件中找到匹配的行,然后检查其中的组并报告预期的和实际的行。以下示例显示了我正在尝试做的事情。
regex_str = '^.*name is (?P<name>[^.]*).*lives? in (?P<place>[^.]*).*$'
statement = 'My name is bala. I live in mumbai.'
statement2 = 'His name is bala. He lives in mumbai.'
ret = find_line([statement,'Garbage'],regex_str,name='bala',place='chennai')
ret == {
'Expected' : 'My name is bala. I live in chennai.'
'Actual' : 'My name is bala. I live in mumbai.'
'isMatched': False
}
ret = find_line([statement2,'Garbage'],regex_str,name='bala',place='chennai')
ret == {
'Expected' : 'His name is bala. He lives in chennai.'
'Actual' : 'His name is bala. He lives in mumbai.'
'isMatched': False
}
ret = find_line(['Garbage1','Garbage2'],regex_str,name='bala',place='chennai')
ret == {
'Expected' : '^.*name is (?P<name>[^.]*).*lives? in (?P<place>[^.]*).*$'
'Actual' : '<No matching line>'
'isMatched': False
}
ret = find_line(['Garbage1',statement,'Garbage2',statement2],regex_str,name='bala',place='mumbai')
ret == {
'Expected' : 'My name is bala. I live in mumbai.'
'Actual' : 'My name is bala. I live in mumbai.'
'isMatched': True
}
我真正的用例是第一个参数将是一个文件对象。find_line 函数将继续跟踪文件,直到找到匹配项(正确或错误)或固定超时。
我正在寻找一种find_line
能够提供所需输出的实现。有什么建议/解决方案吗?