Reputation: 10695
Consider this input in a text file:
foo
bar
foobar
If I look in python API for the re package I understand that if I want to match the foo
and not the foobar
I understand that this code should do it
import re
code = open ('play.txt').read()
print code
print re.findall('^foo$',code)
However the output reads
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import play
foo
bar
foobar
[]
>>>
Why?
Upvotes: 2
Views: 1346
Reputation: 5821
You need to add re.MULTILINE to your flags.
s = '''foo
bar
foobar'''
re.findall('^foo$', s, flags=re.MULTILINE)
Out[14]: ['foo']
Upvotes: 8