Created
November 4, 2013 13:14
-
-
Save mattjbarlow/7302238 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( | |
''' | |
# The regular name | |
(?P<first_name>\w+) | |
\s+ | |
(([\w.]+)\s+)? # optional middle name or initial | |
(?P<last_name>\w+) | |
\s+ | |
< | |
# The address: [email protected] | |
(?P<email> | |
(?P=first_name) | |
\. | |
(?P=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.groupdict()['first_name'], match.groupdict()['last_name'] | |
print ' Match email:', match.groupdict()['email'] | |
else: | |
print ' No match' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment