如何使用java中的正则表达式模式从字符串中提取链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18459465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:48:49  来源:igfitidea点击:

How to extract link from a string using regex patterns in java?

javaregex

提问by Lokesh

My String:

我的字符串:

download_recording true Package available http://abc.com/recDownload/635131586215948750.exe

download_recording true Package available http://abc.com/recDownload/635131586215948750.exe

How to get http://abc.com/recDownload/635131586215948750.exefrom the above string?

如何http://abc.com/recDownload/635131586215948750.exe从上面的字符串中获取?

please help

请帮忙

回答by tom

Simple use of the split method from the string class should parse this.

简单使用 string 类中的 split 方法应该可以解析这个。

String s = "download_recording true Package available http://abc.com/recDownload/635131586215948750.exe";
//We now have the values on either side of where http:// was
String[] splitted = s.split("http://");
//so index 1 is the url and we can just append http:// back on the beginning to get the whole url
String url = "http://" + splitted[1];

回答by sara

回答by Hashbrown

This is a very simple one to match things that include a prefixed protocol. [a-z]+:\/\/[^ \n]*

这是一个非常简单的匹配包含前缀协议的内容。 [a-z]+:\/\/[^ \n]*

Pattern.compile("[a-z]+:\/\/[^ \n]*").matcher(
    "download_recording true Package available http://abc.com/recDownload/635131586215948750.exe click the link"
).find();
//"http://abc.com/recDownload/635131586215948750.exe"


Equivalent javascript

等效的 javascript

'download_recording true Package available http://abc.com/recDownload/635131586215948750.exe click the link'
    .match(/[a-z]+:\/\/[^ \n]*/)

回答by Aashray

I think this regex is what you are looking for:

我认为这个正则表达式就是你要找的:

(http\:\/\/){0,1}(www){0,1}[\.]{0,1}[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+(\/{1}[a-zA-Z0-9_\.]+)*

(http\:\/\/){0,1}(www){0,1}[\.]{0,1}[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+(\/{1}[a-zA-Z0-9_\.]+)*