我正在开发一个http机器人,并且我开发了这个正则表达式 (((?:f|ht)tp(?:s)?\\://)?|www)([^/]+)
来检测并从链接(href)中提取主机名。 现在我把这里的测试结果:正则表达式主机名
String -> http://www.meloteca.com/empresas-editoras.htm
Returns http://www.meloteca.com
String -> www.meloteca.com/empresas-editoras.htm
Returns www.meloteca.com
String -> /empresas-editoras.htm
Returns empresas-editoras.htm (without the slash)
在这种情况下,我期待的是,正则表达式不返回任何值?这是为什么发生? 同样的事情,如果我尝试用下面的代码串
String -> empresas-editoras.htm
Returns empresas-editoras.htm
的片段:
Pattern padrao = Pattern.compile("(((?:f|ht)tp(?:s)?\\://)?|www)([^/]+)");
Matcher mat = padrao.matcher("empresas-editoras.htm");
if(mat.find())
System.out.println("Host->"+mat.group());