判断网址的正则表达式 ((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0

时间:2021-11-07 21:18:42

//判断网址的正则表达式
((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?




reg代表就是你要写的正则的规则!让后就会返回一个boolean值,代表该字符串是否符合你写的正则规则!



private static boolean match(String regex, String str) {
// Pattern pattern = Pattern.compile(regex);
// Matcher matcher = pattern.matcher(str);
// boolean matches = matcher.matches();
// System.out.println("math:::" + matcher);
// return matcher.matches();
// }

public boolean IsUrl(String result) {
// String regex = "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
// return match(regex, result);
String reg="((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(result);
boolean b = matcher.matches();
return b;

}