Python check for valid email address?
Question: Is there a good way to check a form input using regex to make sure it is a
proper style email address? Been searching since last night and everybody that
has answered peoples questions regarding this topic also seems to have
problems with it if it is a subdomained email address.


Answer: There is no point. Even if you can verify that the email address is
syntactically valid, you'll still need to check that it was not mistyped, and
that it actually goes to the person you think it does. The only way to do that
is to send them an email and have them click a link to verify.

Therefore, a most basic check (e.g. that they didn't accidentally entered
their street address) is usually enough. Something like: it has exactly one
`@` sign, and at least one `.` in the part after the `@`:

    
    
    [^@]+@[^@]+\.[^@]+
    

You'd probably also want to disallow whitespace -- there are probably valid
email addresses with whitespace in them, but I've never seen one, so the odds
of this being a user error are on your side.

If you want the full check, have a look at [this
question](http://stackoverflow.com/questions/201323/using-a-regular-
expression-to-validate-an-email-address).

* * *

Update: Here's how you could use any such regex:

    
    
    import re
    
    if not re.match(r"... regex here ...", email):
      # whatever
    

Note the `r` in front of the string; this way, you won't need to escape things
twice.

If you have a large number of regexes to check, it might be faster to compile
the regex first:

    
    
    import re
    
    EMAIL_REGEX = re.compile(r"... regex here ...")
    
    if not EMAIL_REGEX.match(email):
      # whatever
    

