您的位置:首页 > 理论基础 > 计算机网络

从一串字符串中匹配URL地址 正则 (可以没有http或https开头)

2017-02-15 16:50 609 查看
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class helloWorld2 {
private static final String TRACK_SEPARATE = ",";
public static void main(String[] args) {
String str = "\t<html>\n" +
"\t\t<body>\n" +
"\t\t\t<a href=\"http://stackoverflow.com/questions/ask-1-1-1\" /  >\n" +
"\t\t\t<a href=\"http://stackoverflow.com/questions/ask\" >kkkkkk</a>\n" +
"\t\t\t<img width=\"584\" height=\"154\" alt=\"BIGLOBE WiMAX 2+\" src=\"http://img.google.com/images/shopbnr/imax2_160517_584_154\">\n" +
"\t\t</body>\n" +
"\t</html>";

getStrings(str);
}

private static void getStrings( String str) {
Pattern p = Pattern.compile("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)");
Matcher m = p.matcher(str);
ArrayList<String> strs = new ArrayList<String>();
while (m.find()) {
strs.add(m.group(1));
}
for (String s : strs){
System.out.println(s);
}
}
}


http://stackoverflow.com/questions/ask-1-1-1 http://stackoverflow.com/questions/ask http://img.gpoint.co.jp/images/shopbnr/biglobewimax2_160517_584_154
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8

Process finished with exit code 0


Pattern p = Pattern.compile("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐