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
|
359 Discussions
|
Please Login in order to post a comment
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')
This was a very simple challenge. ~~Even without using
email.utils
, it would've been simple.~~Update: I changed my submission to not use
email.utils
, just to be shorter and to depend on fewer modules. Basically, it's the simplest possible program to read all input lines and output only those that match a regex.The Markdown support in this discussion board is HORRIBLE. It doesn't support the
~~
code for strikethrough.how to solve this code
Read the instructions thoroughly. It includes an example for the first step in parsing, using
email.utils.parseaddr()
. If that returns an email address, check its validity usingre.match()
with a regex as described in the instructions.And then… Voilà ! You're done!
import re import email.utils
pattern = r'^[a-zA-Z][\w.-]*@[a-zA-Z]+.[a-zA-Z]{1,3}$'
n = int(input()) for _ in range(n): line = input() name, addr = email.utils.parseaddr(line) if re.fullmatch(pattern, addr): print(email.utils.formataddr((name, addr)))