0

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

Maxime R.
  • 133
  • 1
  • 9
  • 1
    Try `(?!www)`. [More info](http://www.regular-expressions.info/completelines.html) (go to paragraph 'Finding Lines Containing or Not Containing Certain Words') – Laoujin Feb 19 '14 at 09:45
  • _Almost_, but not completely, a duplicate of [Regex for ANY string except “www”? (subdomain)](http://stackoverflow.com/q/7099397/2936460). – SQB Feb 19 '14 at 10:04

3 Answers3

3

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-]+)
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Works fine, thanks for your explanation, but it accept multiple subdomains (subsubdomain, subsubsubdomain...) and I just one to valid one – Maxime R. Feb 19 '14 at 09:52
  • Okay I used the following : (\b(?!www\b)(?:[0-9A-Za-z_-]+)) to don't accept dots – Maxime R. Feb 19 '14 at 09:56
2

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
SQB
  • 3,926
  • 2
  • 28
  • 49
0

This works for me to validate subdomain.

^([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})+$
Karthick oc
  • 88
  • 1
  • 2
  • 7
  • 1
    This doesn't exclude `www` from the match, which is what was asked **explicitly**. – SQB Feb 19 '14 at 10:02