Validating and Parsing Email Addresses

Sort by

recency

|

359 Discussions

|

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

    from email.utils import parseaddr import re

    pattern = r'^[A-Za-z][a-zA-Z0-9.,-_]*@[a-zA-Z]+.[a-zA-Z]{1,3}$' input_no = input() address = [] for i in range(int(input_no)): e_input = input() address.append(e_input)

    for i in range(int(input_no)): name,addr = parseaddr(address[i]) match = re.match(pattern,addr) if match: print(name + "<" + addr + ">") print('Valid email')

  • + 1 comment

    This was a very simple challenge. ~~Even without using email.utils, it would've been simple.~~

    Update: I changed my submission to not use email.utils, just to be shorter and to depend on fewer modules. Basically, it's the simplest possible program to read all input lines and output only those that match a regex.

    import re, sys
    
    # I included support for "+" in the address.
    # I hate when email address validators don't allow it.
    # Note that this is only a regex exercise, not a real email
    # address validation.  The ONLY TRUE way to validate an
    # email address is to send a message to it and instruct the
    # recipient to respond.  When their response is received, ONLY
    # THEN can the email address be considered valid.
    
    print(''.join(l for l in sys.stdin.readlines()[1:]
      if re.search(r'<[a-z][\w.+-]*@[a-z]+\.[a-z]{1,3}>', l, re.I)))
    
    • + 0 comments

      The Markdown support in this discussion board is HORRIBLE. It doesn't support the ~~ code for strikethrough.

  • + 1 comment

    how to solve this code

    • + 0 comments

      Read the instructions thoroughly. It includes an example for the first step in parsing, using email.utils.parseaddr(). If that returns an email address, check its validity using re.match() with a regex as described in the instructions.

      And then… Voilà! You're done!

  • + 0 comments

    import re import email.utils

    pattern = r'^[a-zA-Z][\w.-]*@[a-zA-Z]+.[a-zA-Z]{1,3}$'

    n = int(input()) for _ in range(n): line = input() name, addr = email.utils.parseaddr(line) if re.fullmatch(pattern, addr): print(email.utils.formataddr((name, addr)))