We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating and Parsing Email Addresses
Validating and Parsing Email Addresses
Sort by
recency
|
362 Discussions
|
Please Login in order to post a comment
Imagine not following instructions while taking a challenge. Couldn't be me.
import re pattern = re.compile(r'^[\w.-]+@[A-Za-z]+.[A-Za-z]{1,3}$') n = int(input()) for _ in range(n): name, email = input().split() email_clean = email[1:-1] if pattern.match(email_clean): print(f"{name} {email}")
I didn't use emails.utils for my solution. Find my code below.
Just to precise, I started by splitting username / domain and extension into differents variables, then, I have created 3 differents regex pattern, to check if each one works.
When it was done, I've removed the split for username / domain and extension, then merge the 3 differents regex pattern into a big one.
And finally, it pass all the tests :
from email.utils import parseaddr import re
pattern = r'^[A-Za-z][a-zA-Z0-9.,-_]*@[a-zA-Z]+.[a-zA-Z]{1,3}$' input_no = input() address = [] for i in range(int(input_no)): e_input = input() address.append(e_input)
for i in range(int(input_no)): name,addr = parseaddr(address[i]) match = re.match(pattern,addr) if match: print(name + "<" + addr + ">") print('Valid email')