/(?<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 !
troethom
November 19, 2008, November 19, 2008 18:58, permalink
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/
Tj Holowaychuk
November 21, 2008, November 21, 2008 07:53, permalink
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
Daniel Schierbeck
November 21, 2008, November 21, 2008 12:17, permalink
This ought to do it.
/^(?!www\.)(?:\w+\.)*example\.com$/
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.