You may fix your code if you add re.M
flag:
re.findall(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$", my_file.read(), re.M)
Since you read in the whole file with my_file.read()
, the ^
and $
should match start/end of the line, not string, and the re.M
flag does that.
Also, you may read the file line by line and only get those lines that fully match your pattern:
items = []
email_rx = re.compile(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$")
with open('emails.txt', 'r+') as my_file:
for line in my_file:
if email_rx.match(line):
items.append(line)
Note that only $
anchor is necessary as re.match
only anchors matches at the start of the string.
Note that you may have CRLF endings, then, you might either rstrip
each line before testing against regex and appending to items
, or add \s*
pattern at the end before $
anchor.