Skip to content

Instantly share code, notes, and snippets.

@mattjbarlow
Created November 4, 2013 13:11
Show Gist options
  • Save mattjbarlow/7302201 to your computer and use it in GitHub Desktop.
Save mattjbarlow/7302201 to your computer and use it in GitHub Desktop.
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
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