Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用正则表达式获取URL?_Java_Url - Fatal编程技术网

如何在java中使用正则表达式获取URL?

如何在java中使用正则表达式获取URL?,java,url,Java,Url,我有一个类似url的文件,我只想用java中的正则表达式提取http:/www.google.com,我怎样才能得到它???只需使用url: public static void main(String[] args) throws Exception { final URL url = new URL("http://www.google.com/api/js/example"); System.out.println(url.getProtocol() + "://" + u

我有一个类似url的文件,我只想用java中的正则表达式提取http:/www.google.com,我怎样才能得到它???

只需使用
url

public static void main(String[] args) throws Exception {
    final URL url = new URL("http://www.google.com/api/js/example");
    System.out.println(url.getProtocol() + "://" + url.getHost());
}

+1这当然是更聪明的解决方案。使用内置模块比定义一个自己的正则表达式要好得多。这正是我所想的,但我发布它的时候你已经做到了+我认为如果您的应用程序是Java的,那么几乎不需要使用正则表达式。我在Bash中使用regex,仅此而已——我从来没有为其他任何事情需要过regex。