简体   繁体   中英

Regex : every alphanumeric subdomains, except www

This is a piece of a big regex (php) :

((?:[0-9A-z_-]+))

I use it to valid a subdomain, but I would like to accept every subdomains except "www"

How can I do that when there's an alphanumeric "selector" ?

EDIT : Finally used (\\b(?!www\\b)(?:[0-9A-Za-z_-]+)) , thanks to Tim and ATS

First, that regex isn't correct, it should be ((?:[0-9A-Za-z_-]+)) (or simply ([\\w-]+) - the non-capturing group is unnecessary). There are some characters between Z and a that you don't want to match.

Second, use a negative lookahead assertion to make sure that the string you're matching isn't www . To make sure that we're not just taking the submatch ww from www (or fail to match wwwwwhat.sgoing.on ), it might be necessary to add word boundary anchors , depending on context:

\b(?!www\b)([\w-]+)

Try this:

((?!www)(?:[0-9A-Za-z_-]+))

This uses negative lookahead to say "not www".

This assumes that the rest of the regex (as you wrote, it's part of a bigger expression) makes sure that this part contains just a possible subdomain. So this part should match all of a subdomain but shouldn't need to check if it did match all of it.
What you still need to do, that I can't (since I don't know what you're using), is insert the proper boundary detection after www . Probably \\b would suffice.

这对我有用以验证子域。

^([a-z0-9]+([\-a-z0-9]*[a-z0-9]+)?\.){0,}([a-z0-9]+([\-a-z0-9]*[a-z0-9]+)?){1,63}(\.[a-z0-9]{2,7})+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM