I need to extract the domain name from a long host name. For example if I have a string, “www.domain.com”, I need to extract just “domain.com” - and if I have “blah.blah.blah.domain.co.uk” I need to extract “domain.co.uk” - is there a regular expression which can do this?
Yes there is a regular expression which can do this (we all know that regexes can solve any problem, right? ( but as crmalibu pointed out, you’d need a list of (first, second, or more level) domains to be a part of that regex. As it happens, there are in the region of 2,500 of those domains to be taken into consideration (and almost certainly more that I couldn’t find in my all-too-brief search) so it’s entirely possible to construct a monster-regex to match what you’re looking for. Just how monster the regex is, is up to you but one that I came up with is over 19,000 characters long!
Do you still want to use a regular expression for this problem?
Luckily I don’t need to support all possible domain suffixes. Only com/co.uk/net
I’m just really bad at writing regular expressions. I think an alteration of the code shown in post#2 would work… (currently, it works perfectly for .com domains but fails for .co.uk)