Reputation: 12091
Need a regular expression (using PHP) to match a domain name. Examples:
http://sub.domain.com/somefolder/index.html -> domain.com
somedomain.info -> somedomain.info
http://anotherdomain.org/home -> anotherdomain.org
www.subdomain.anothersubdomain.maindomain.com/something/ -> maindomain.com
Upvotes: 2
Views: 5500
Reputation: 93636
This might not be a job for regexes, but for existing tools in your language of choice. Regexes are not a magic wand you wave at every problem that happens to involve strings. You probably want to use existing code that has already been written, tested, and debugged.
In PHP, use the parse_url
function.
Perl: URI
module.
Ruby: URI
module.
.NET: 'Uri' class
Upvotes: 2
Reputation: 4794
You can use this one as well:
((?>[a-z\-0-9]{2,}\.){1,}[a-z]{2,8})(?:\s|/)
It will match all the domain names found in a given text, while returning the domain found in the first group.
Upvotes: 0
Reputation: 4839
This parses Complete Urls you can check, match objects 2nd element for domain name
(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
Works for me :)
Upvotes: -1