John Ohara
John Ohara

Reputation: 2901

Regex: how can exclude all TLD except my own domain

I have an Asp.Net website on which I'm implementing some basic anti-spam stuff via the validation controls.

One such regex is: "^(?!.*(//|[.]({com|net|info|uk|etc}))).*$"

It pretty much does what it needs to as far as blocking goes — it doesn't need to be too sophisticated. However, I want to include the option to whitelist my own domain.

So, I want to block all .uk domains, except mydomain.co.uk.

This is a regex step beyond me — can anyone help?

Upvotes: 1

Views: 285

Answers (1)

anubhava
anubhava

Reputation: 785521

You may use a nested negative lookbehind while uk to fail the already existing negative lookahead for mydomain.co. part before matching uk:

^(?!.*(//|[.](com|net|info|(?<!mydomain\.co\.)uk))).*$

RegEx Demo

Take note of (?<!mydomain\.co\.) which is a negative lookbehind to not to match uk if it is preceded by mydomain.co..

Upvotes: 2

Related Questions