PeanutsMonkey
PeanutsMonkey

Reputation: 7095

Can this regular expression be done better?

I am attempting to only limit a particular denomination of a domain but allow others. For example the regular expression is

^(www\.)?(?!abc\.co)[a-zA-Z\-]+\.(co|org)\.uk$

This would disallow abc.co.uk and www.abc.co.uk but allow www.abcdef.co.uk, www.abc-def.co.uk, etc however am wondering if this can be done better?

EDIT

Here is an example of the same regex allowing sub-domains.

^(www\.)?([a-zA-Z\-]+\.)?(?!abc\.co)[0-9a-zA-Z\-]+\.(co|org)\.uk$

Upvotes: 0

Views: 94

Answers (1)

Bohemian
Bohemian

Reputation: 424983

It seems you want to match anything the doesn't end with abc.co.uk or abc.org.uk.
Assuming that to be correct, here's the regex that expresses that:

(?<!abc.(co|org).uk)$

This uses a negative look behind from the end of the input to assert it doesn't end with the given regex.

Upvotes: 1

Related Questions