Read the entire file in line, then \ A matches only the beginning of the line, and \ Z matches only the end of the line. With re.MULTILINE '^' corresponds to the start line and just after the new line, and "$" matches the end of line and before a newline, See. The Python documentation for re syntax .
import re
data = '''sentence one.
sentence two.
a bad sentence
sentence three.
sentence four.'''
print re.findall(r'^.*\.$',data,re.MULTILINE)
print re.findall(r'\A^.*\.$',data,re.MULTILINE)
print re.findall(r'^.*\.$\Z',data,re.MULTILINE)
Output:
['sentence one.', 'sentence two.', 'sentence three.', 'sentence four.']
['sentence one.']
['sentence four.']
source
share