Hello,
In a multiline text file, I need to find the last occurence of a pattern, ie. the Python equivalent of "tail".
None of the following works:
Thank you.
In a multiline text file, I need to find the last occurence of a pattern, ie. the Python equivalent of "tail".
None of the following works:
import re INPUTFILE = "log.txt" with open(INPUTFILE) as reader: content = reader.read() #==================== #very slow, even on 130KB file p = re.compile("(?s:.*)^Some pattern.+$") m = p.search(content) if m.group(0): print(m.group(0)) #==================== #IndexError: list index out of range p = re.compile("^Some pattern.+$") m = p.findall(content)[-1] if m.group(0): print(m.group(0)) #==================== #AttributeError: 'NoneType' object has no attribute 'group' p = re.compile("^Some pattern.+$") for line in content: m = p.search(line) if m.group(0): print(m.group(0)) #==================== #AttributeError: 'str' object has no attribute 'readlines' for line in content.readlines(): m = p.search(line) if m.group(0): print(m.group(0))Anyone knows?
Thank you.