开发者

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 开发者_Python百科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$


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜