Validating and Parsing Email Addresses

  • + 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)))