Validating and Parsing Email Addresses

  • + 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]}")