monocular
monocular

Reputation: 323

Regex match all email except email of domain something.com

I'm using Vbscript/Classic Asp/Vb6

I want to match all email have domain at least .com except something.com , i try to using the follow expression but isn't working

@(?!something).com$

Upvotes: 1

Views: 5843

Answers (2)

ProgramFOX
ProgramFOX

Reputation: 6390

You forgot to specify the allowed domains. Try this:

@(?!something\.com)([A-Za-z0-9]+\.com)$

You can add more characters than A-Z, a-z and 0-9 if you want. Also, you should escape a dot inside a regex, because the dot is a special character (it matches each character).

Upvotes: 2

Eamon Nerbonne
Eamon Nerbonne

Reputation: 48096

You'll need to introduce a positive match too. What's a matching email domain? Well, if you don't have control over the domain encoding, it may be punycoded already, but it's more likely that it can contain non-ascii characters. If that's the case, then in general it's easier to be lenient rather than really trying to express the full complexity of what's a possible domain name. My recommendation is thus to focus on the structure of the domain rather then the exact characters that are legal; however the right choice depends on your need.

A reasonable simple regex for a .com domain might be something like:

([^.]+\.)+com$

Including the restriction that it must be preceded by @ and not be something.com:

@(?!something\.com$)([^.]+\.)+com$

Note that the negative lookahead includes the .com and terminator symbol $ to it doesn't falsely reject somethingelse.com nor something.comedy.com.

A limitation is that this accepts invalid domains such as _.com or hmm-.com, so if you'd prefer to reject such cases (but also internationalised domains), you could write:

@(?!something\.com$)(([a-z0-9][-a-z0-9]*)?[a-z0-9]\.)+com$

This ensures valid characters for all labels, allows more than one label (e.g. "mail" and "google" in mail.google.com), and ensures no label may start or end with a hyphen.

Edit: If you want to enforce the 253 character total-length and per-label 63 character length limits as well, you could do something like this:

@(?!.{254})(?!something\.com$)(([a-z0-9][-a-z0-9]{0,61})?[a-z0-9]\.)+com$

Limitations: Unfortunately, since VBScript's unicode-foo is weak, the regex for the allowable unicode characters is quite large (lots of character ranges). Finally, this regex does not take casing into account: neither does DNS, so that should be fine, but remember to run it in case-insensitive mode or to normalize to lower-case before testing.

Honestly, however: I'd not do this. Keep it simple - do you really care if an invalid emails address is entered? Then verify the email - no amount of syntactic checking will really work. Some simple, basic checking - like does it have an @, a . and not end in something.com should catch most accidental errors; but unless you really need to, don't bother with this detailed approach.

Upvotes: 3

Related Questions