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

时间:2013-08-27 07:27:18

标签: java regex

我的字符串:

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

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

请帮助

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

这是一个非常简单的匹配包含前缀协议的东西。 [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"

等效的javascript

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

答案 2 :(得分:0)

从字符串类中简单使用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];

答案 3 :(得分:0)

我认为这个正则表达式是您正在寻找的:

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