Validating and Parsing Email Addresses

Sort by

recency

|

343 Discussions

|

  • + 0 comments

    BTW, if you want to do it with regex, this is one way:

    import re
    
    for _ in range(int(input())):
        r_in = r"^([a-zA-Z]+) <([a-zA-Z][\w.-]+)@([a-zA-Z]+)\.([a-z]{1,3})>$"
        found = re.match(r_in, input())
        if found:
            name, username, domain, ext = found.group(1), found.group(2), found.group(3), found.group(4)
            print(f'{name} <{username}@{domain}.{ext}>')
    
  • + 0 comments

    import email.utils, re for i in range(int(input())): name, e=input().split() l=email.utils.parseaddr(e) if re.search(r"^a-zA-Z{1,}@([A-Za-z]{1,}).[A-Za-z]{1,3}$",l[1]): print(name, e)

  • + 0 comments
    import re
    import email.utils
    
    n = int(input())
    pattern = r'^[a-z](\w|-|\.)*@[a-z]+\.[a-z]{1,3}$'
    
    for i in range(n): 
        email_string = input()
        email_string_to_check = (email.utils.parseaddr(email_string))
        if re.search(pattern, str(email_string_to_check[1])):
            print(email_string)
    
  • + 0 comments

    Whitout email.utils -->

    import sys, re
    
    text = sys.stdin.read().splitlines()[1:]
    
    match_pattern = re.compile(
        r"^[a-z]+\s"
        r"<[a-z](\w|[.-])*[@][a-z]+[.][a-z]{1,3}>"
    , re.I)
    
    match_list = [re.search(match_pattern, line) for line in text]
    
    for match in match_list:
        if match: sys.stdout.write(f'{match.group()}\n')
    
  • + 0 comments
    import re 
    import email.utils as email 
    
    n = int(input())
    
    r = re.compile(r'^[a-z](\w|-|\.)*@[a-z]+\.[a-z]{1,3}$')
    
    for i in range(n):
        name,mail = email.parseaddr(input())
        
        if r.match(mail):
            print(email.formataddr((name,mail)))