I need to match a host name–but don’t want the tld:
example.com =~ /regex/ => example
sub.example.com =~ /regex/ => sub.example
sub.sub.example.com =~ /regex/ => sub.sub.example
Any help with the regex? Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming your string is correctly formatted and doesn’t include things like protocol [i.e. http://], you need all characters up to but not including the final .tld.
So this is the simplest way to do this. The trick with regular expressions is not to overcomplicate things:
This basically says, give me all characters in the set that is followed by [for example] .xxx, which will basically just return everything prior to the last period.
If you don’t have lookahead, it would probably be easiest to use:
which will give you everything up to and including the final ‘.’ and then just trim the ‘.’.