I am writing a function using regular expressions for emails. I think I write the pattern correctly, however, I couldn't find out why example 2 'what-do-you-know+not-much@gmail.com' failed to be detected while example 1 worked successfully?
def parse_email(s):
try:
pattern = re.compile(r'\b([a-zA-Z])([\w.-_+]+)@([\w.-]+)([a-zA-Z])\b')
matches = pattern.finditer(s)
for match in matches:
print(match.group(0))
return (match.group(1)+match.group(2), match.group(3)+match.group(4))
except AttributeError:
#print('here')
raise ValueError
print(parse_email('JKRowling@Huge-Books.org'))
print(parse_email('what-do-you-know+not-much@gmail.com'))
Results:
JKRowling@Huge-Books.org
('JKRowling', 'Huge-Books.org')
much@gmail.com
('much', 'gmail.com')