($domain) = split '/', $string; $wanted = (split /\./, $domain)[-2]; # or $wanted = (split /\./, (split '/', $string)[0])[-2]; #### ($wanted) = $string =~ m{ ( [^.]+ ) # save the non-. sequence to $1 \. # . [^./]+ # the final non-. non-/ sequence (?: / | $) # / or the end of the string }x;