Validating and Parsing Email Addresses

  • + 0 comments

    The regex pattern ^[a-z][\w\-.]*@[a-z]+\.[a-z]{1,3}$ is used to validate email addresses. It requires that the username portion of the email address starts with an English alphabetical character and can contain any number of alphanumeric characters, hyphens (-), periods (.), and underscores (_). The domain and extension portions of the email address must contain only English alphabetical characters, and the extension must be 1, 2, or 3 characters in length.

    import email.utils
    import re
    
    for _ in range(int(input())):
        parser = email.utils.parseaddr(input())
        email_pattern = r"^[a-z][\w\-.]*@[a-z]+\.[a-z]{1,3}$"
        match = re.search(email_pattern, parser[1], re.I)
        if match:
            print(email.utils.formataddr(parser))