Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View re_problem.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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
Something went wrong with that request. Please try again.