I'm looking for the "perfect" regexp's to validate if an e-mail belongs to a domain name (including sub-domains), for example:
www.domain.com : [email protected]
sub.domain.com : [email protected]
domain2.com : [email protected]
I'm looking for the "perfect" regexp's to validate if an e-mail belongs to a domain name (including sub-domains), for example:
www.domain.com : [email protected]
sub.domain.com : [email protected]
domain2.com : [email protected]
Taken from django:
email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE) # domain
You don't need a regex:
>>> def match_domain(domain, email):
... return email.endswith(domain.lstrip('www.'))
...
>>> match_domain('www.domain.com', '[email protected]')
True
>>> match_domain('www.domain.com', '[email protected]')
True
>>> match_domain('sub.domain.com', '[email protected]')
True
>>> match_domain('sub.domain2.com', '[email protected]')
False
>>> match_domain('domain2.com', '[email protected]')
False