Created
November 4, 2013 13:11
-
-
Save mattjbarlow/7302201 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
address = re.compile( | |
r''' | |
# The regular name | |
(\w+) # first name | |
\s+ | |
(([\w.]+)\s+)? # optional middle name or initial | |
(\w+) # last name | |
\s+ | |
< | |
# The address: [email protected] | |
(?P<email> | |
\1 # first name | |
\. | |
\4 # last name | |
@ | |
([\w\d.]+\.)+ # domain name prefix | |
(com|org|edu) # limit the allowed top-level domains | |
) | |
> | |
''', | |
re.UNICODE | re.VERBOSE | re.IGNORECASE) | |
candidates = [ | |
u'First Last <[email protected]>', | |
u'Different Name <[email protected]>', | |
u'First Middle Last <[email protected]>', | |
u'First M. Last <[email protected]>', | |
] | |
for candidate in candidates: | |
print 'Candidate:', candidate | |
match = address.search(candidate) | |
if match: | |
print ' Match name :', match.group(1), match.group(4) | |
print ' Match email:', match.group(5) | |
else: | |
print ' No match' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment