Skip to content

Instantly share code, notes, and snippets.

@yeukhon
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeukhon/565f23a77d3d58c1cf02 to your computer and use it in GitHub Desktop.
Save yeukhon/565f23a77d3d58c1cf02 to your computer and use it in GitHub Desktop.
import re
text1 = "alphanumeric@example.org"
text2 = "alphanumeric@long.subdomain.more.subdomain.domain.org"
r1 = re.compile(r'[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+(\.[a-zA-Z]+)*')
print(r1.match(text1).group())
print(r1.match(text2).group())
print("part 2 - compare with and without (?:")
r2 = re.compile(r'[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+(?:\.[a-zA-Z]+)*')
print(r1.findall(text2))
print(r2.findall(text2))
print("part 3 - udacity assignment")
def addresses(haystack):
emails = re.findall(r'\w+@[a-zA-Z]+\.[a-zA-Z]+(?:\.[a-zA-Z]+)*', haystack)
return [re.sub("NOSPAM", "", email) for email in emails]
input1 = """louiseNOSPAMaston@germany.de (1814-1871) was an advocate for
democracy. irmgardNOSPAMkeun@NOSPAMweimar.NOSPAMde (1905-1982) wrote about
the early nazi era. rahelNOSPAMvarnhagen@berlin.de was honored with a 1994
deutsche bundespost stamp. seti@home is not actually an email address."""
output1 = ['louiseaston@germany.de', 'irmgardkeun@weimar.de', 'rahelvarnhagen@berlin.de']
print addresses(input1) == output1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment