使用regexp提取网址的主机部分的最佳方法是什么?

Gen*_* S. 0 regex

我正在从我的网址中提取主机,并且通过制作最后一个/可选项而卡住了.正则表达式需要准备好接收以下内容:

http://a.b.com:8080/some/path/file.txt
or
ftp://a.b.com:8080/some/path
or
ftp://user@a.b.com/some/path
or
http://a.b.com
or 
a.b.com/some/path

and return a.b.com
Run Code Online (Sandbox Code Playgroud)

所以...

(ftp://|http://)? optionally matches the first part
then it gets hairy...
so... without adding ugly (and wrong) regexp here... just in english
(everything that isn't an '@') //optional
(everything that isn't a '/' up to the first '/' IF it's there) //this is the host group that I want
(everything else that trails) //optional
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 5

你需要使用正则表达式吗?大多数语言都支持解析URL.例如,Java有java.net.URL,Python有urlparse模块,Ruby有URI模块.您可以使用它们来查询给定URL的不同部分.