0

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 : some1@domain.com
sub.domain.com : some1@sub.domain.com
domain2.com : some1@domain2.com
  • So you have the domain name to match to and you want to validate any email address to that specific domain? – ejel Apr 14 '10 at 19:35
  • 1
    You're going to have to explain why `www.domain.com` corresponds to `@domain.com` - there is no reason why that must be the case, and indeed `some1@www.domain.com` is a completely different email address from `some1@domain.com`. – Greg Hewgill Apr 14 '10 at 19:36
  • Why is it 'I need a regex' and not 'I need code'. Also, do you need to verify it's a legal e-mail as well, or just to verify that, given it's an e-mail, it matches domain.com? – moshez Apr 14 '10 at 19:54
  • i'm looking forward to implement a similar system to what GetSatisfaction implements to validate if a user is a company's employee, by matching if his account e-mail domain belongs to the company's domain. The domain and e-mail itself are validated separately. I excluded the www from the 1st example as it is a standard practice, "no one" has e-mails in the form user1@www.website.com. I started thinking about this problem today and regexp seemed like a good start. – José Moreira Apr 14 '10 at 20:41
  • So everyone works for Yahoo!, I guess, since anyone can get an address @yahoo.com... – moshez Apr 14 '10 at 20:49

2 Answers2

1

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
diegueus9
  • 29,351
  • 16
  • 62
  • 74
0

You don't need a regex:

>>> def match_domain(domain, email):
...    return email.endswith(domain.lstrip('www.'))
...
>>> match_domain('www.domain.com', 'some1@domain.com')
True
>>> match_domain('www.domain.com', 'some1@www.domain.com')
True
>>> match_domain('sub.domain.com', 'some1@sub.domain.com')
True
>>> match_domain('sub.domain2.com', 'some1@sub.domain.com')
False
>>> match_domain('domain2.com', 'some1@domain.com')
False
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • be careful with `lstrip` because the argument passed is a string containing the characters that will be removed, so `domain.lstrip('www.')` will do the same that `domain.lstrip('w.')` – Diego Navarro Dec 22 '11 at 10:32
  • It's just an example. It's not meant to be robust. I would actually use split('.') myself, I just want to make a point. Regex are nice, but you don't always need them. – Bite code Dec 22 '11 at 10:35
  • 1
    I told you because sometimes might be a source of bugs. For example: `'www.who.com'.lstrip('www.')` will return `'ho.com'`. – Diego Navarro Dec 22 '11 at 10:42