def grep_in_file(filename, regex):
- with open(filename, 'r') as f:
- return filter(lambda x: x is not None, [re.search(regex, line) for line in f])
+ with open(filename, "r") as outfile:
+ return filter(lambda x: x is not None,
+ list(re.finditer(regex, outfile.read(), re.MULTILINE)))
@pytest.mark.parametrize("regex,expected", [
('not exist', []),
('Lorem (\S+)', [{'groups': ('ipsum',), 'groupdict': {}}]),
- ('nisi ut (?P<name>\S+)', [{'groups': ('aliquip',), 'groupdict': {'name': 'aliquip'}}])
+ ('nisi ut (?P<name>\S+)', [{'groups': ('aliquip',), 'groupdict': {'name': 'aliquip'}}]),
+ ('Lorem\s(\w+)\s.+\nconsectetur\s(\w+)\s.+\n',
+ [{'groups': ('ipsum', 'adipiscing',), 'groupdict': {}}])
])
def test_grep_in_file(logfile, regex, expected):
matches = grep_in_file(logfile, regex)