Validating and Parsing Email Addresses

  • + 1 comment

    I managed to use both regex and email.util

    import email.utils
    import re
    
    n = int(input())
    rx = re.compile(r"^[a-zA-Z][\w\.\-_]*@[a-zA-Z]+\.[a-zA-Z]{1,3}$")
    emails = [email.utils.parseaddr(input()) for _ in range(n)]
    emails = filter(lambda x: rx.match(x[1]), emails)
    print(*list(map(email.utils.formataddr, emails)), sep='\n')