628b75cc5fce9ab14d9e8584a0b3681c

I have written this regular expression which matches any subdomain (consisting of only letters and digits) right below example.com (including the domain, e.g. test.example.com) but not www.example.com. It works, but it is not very elegant.

/(?<part>(w(?!ww)|w(?=www)|w(?=[a-z0-9]+ww)|ww(?=[a-z0-9]+w)|www[a-z0-9]+|[a-vx-z0-9])[a-z0-9]*)\.example\.com/

Refactorings

No refactoring yet !

628b75cc5fce9ab14d9e8584a0b3681c

troethom

November 19, 2008, November 19, 2008 18:58, permalink

No rating. Login to rate!

Did a little logical thinking and managed to simplify a bit.

/(?<part>(w(?!ww)|w(?=[a-z0-9]{3,})|[a-vx-z0-9])[a-z0-9]*)\.example\.com/
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

November 21, 2008, November 21, 2008 07:53, permalink

1 rating. Login to rate!

I would maybe consider extending URI similarly to below. 'example' is technically a subdomain of the TLD 'com' so I would maybe allow that to have a param like below, just as well 'www' is still a valid subdomain of course so maybe that logic could belong in a different method

require 'uri'

module URI
  class HTTP
    def domains
      host.split '.'
    end
    
    def tld
      domains.last
    end
    
    def subdomain(level = 1)
      domains[-(level + 1)] unless domains.length < level + 1
    end
  end
end

p URI.parse("http://something.example.com").domains
p URI.parse("http://something.example.com").tld
p URI.parse("http://something.example.com").subdomain
p URI.parse("http://something.example.com").subdomain 2
p URI.parse("http://example.com").subdomain
p URI.parse("http://example.com").subdomain 2


Outputs :

["something", "example", "com"]
"com"
"example"
"something"
"example"
nil
5da4c52f43677f395aff5bde775593c2

Daniel Schierbeck

November 21, 2008, November 21, 2008 12:17, permalink

2 ratings. Login to rate!

This ought to do it.

/^(?!www\.)(?:\w+\.)*example\.com$/

Your refactoring





Format Copy from initial code

or Cancel