I\'m trying to extract email addresses from plain text transcripts of emails. I\'ve cobbled together a bit of code to find the addresses themselves, but I don\'t know how to mak
mailsrch = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}')
Expression breakdown:
[\w-]: any word character (alphanumeric, plus underscore) or a dash
[\w-.]+: any word character, a dash, or a period/dot, one or more times
@: literal @ symbol
[\w-][\w-.]+: any word char or dash, followed by any word char, dash, or period one or more times.
[a-zA-Z]{1,4}: any alphabetic character 1-4 times.
To make this match only lines starting with From:, and wrapped in < and > symbols:
import re
foundemail = []
mailsrch = re.compile(r'^From:\s+.*<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>', re.I | re.M)
foundemail.extend(mailsrch.findall(open('text.txt').read()))
print foundemail