I'd say there is a regex solution to this, but it will be complicated. You will have to find the last occurrence of the '.' and get what's after that, then you should get what's between the last and second to last '.', then get anything before the second to last period, up to the third to the right (subdomain) '.'.
Does that make sense?? I'm not sure of the regex required, but here are some questions on SO to get you started...
Regex: To pull out a sub-string between two tags in a string
Regex to replace all \n in a String, but no those inside [code] [/code] tag
Need regexp to find substring between two tokens