a more precise re for email addys

rbt rbt at athop1.ath.vt.edu
Wed Jan 18 16:54:07 EST 2006


dave.brueck at gmail.com wrote:
> Does it really need to be a regular expression? Why not just write a
> short function that breaks apart the input and validates each part?
> 
> def IsEmail(addr):
>   'Returns True if addr appears to be a valid email address'
> 
>   # we don't allow stuff like foo at bar@biff.com
>   if addr.count('@') != 1:
>     return False
>   name, host = addr.split('@')
> 
>   # verify the hostname (is an IP or has a valid TLD, etc.)
>   hostParts = host.split('.')
>   ...
> 
> That way you'd have a nice, readable chunk of code that you could tweak
> as needed (for example, maybe you'll find that the RFC is too liberal
> so you'll end up needing to add additional rules to exclude "bad"
> addresses).
> 

Good idea. I'll see what I can do with this. Thanks!



More information about the Python-list mailing list