可能重复:
Regex to match URL
是否有正则表达式从字符串返回http值?
所以
sdfads saf as fa http://www.google.com some more text
变为
http://www.google.com
答案 0 :(得分:2)
一种非常简单的方法:
https?://\S+
如果你必须检查有效网址,那么正则表达式要复杂得多
答案 1 :(得分:0)
这是一个简单而有效的示例,包含检索搜索模式并使用它来替换整个输入:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regextest {
static String[] matchThese = new String[] {
"sdfads saf as fa http://www.google.com some more text",
"sdfads fa http://www.dupa.com some more text",
"should not match http://" };
public static void main(String[] args) {
String regex = "(https?://|www)\\S+";
Pattern p = Pattern.compile(regex);
System.out.println("Those that match are replaced:");
for (String input : matchThese) {
if (p.matcher(input).find()) {
Matcher matcher = p.matcher(input);
matcher.find();
// Retrieve matching string
String match = matcher.group();
String output = input.replace(input, match);
System.out.println(output);
}
}
}
}