Validating and Parsing Email Addresses

Sort by

recency

|

363 Discussions

|

  • + 0 comments
    import re
    import email.utils
    
    n = int(input())
    pattern = r"[a-zA-Z][\w\.-]*@[a-zA-Z]+\.[a-zA-Z]{1,3}"
    for _ in range(n):
        mail = email.utils.parseaddr(input())
        if re.fullmatch(pattern, mail[1]):
            print(email.utils.formataddr((mail[0], mail[1])))
    
  • + 0 comments

    Imagine not following instructions while taking a challenge. Couldn't be me.

    import re
    import email.utils
    
    N = int(input())
    REX = r"[a-zA-Z][\w\.-]*@[a-zA-Z]+\.[a-zA-Z]{1,3}"
    for _ in range(N):
        EMAIL = email.utils.parseaddr(input())
        if re.fullmatch(REX, EMAIL[1]):
            print(email.utils.formataddr((EMAIL[0], EMAIL[1])))
    
  • + 0 comments

    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}")

  • + 0 comments
    import re
    import email.utils
    for _ in range(int(input())):
        i=input()
        i=email.utils.parseaddr(i)
        if re.match(r"^[a-zA-Z][a-zA-Z0-9\-\_\.]+@[a-z]+\.[a-z]{1,3}$",i[1]):
            print(email.utils.formataddr((i[0],i[1])))
    
  • + 0 comments

    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 :

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import re
    import email.utils
    
    
    n = int(input())
    output = list()
    for _ in range(n):
        current_line = input().split()
        if "@" in current_line[1]:
            current_name = current_line[0]
        
            current_mail = current_line[1]
            current_mail = current_mail[1:-1]
        
            pattern = r"^([A-Za-z][A-Za-z0-9\_\-\.]+)@([A-Za-z]+)([.])([A-Za-z]{1,3})$"
            
            # Debug check
            #print(bool(re.match(pattern, current_mail)))
        
            if bool(re.match(pattern, current_mail)):
                output.append(current_line)
            else:
                continue
        else:
            continue
    
    for element in output:
        print(f"{element[0]} {element[1]}")