How can I extract only top-level and second-level domain from a URL using regex? I want to skip all lower level domains. Any ideas?
See Question&Answers more detail:osHow can I extract only top-level and second-level domain from a URL using regex? I want to skip all lower level domains. Any ideas?
See Question&Answers more detail:osHere's my idea,
Match anything that isn't a dot, three times, from the end of the line using the $
anchor.
The last match from the end of the string should be optional to allow for .com.au
or .co.nz
type of domains.
Both the last and second last matches will only match 2-3 characters, so that it doesn't confuse it with a second-level domain name.
Regex:
[^.]*.[^.]{2,3}(?:.[^.]{2,3})?$
Demonstration: