java正则表达式匹配url
【代码】java正则表达式匹配url。
·
StringUtil.java
public static final Pattern HTTP_OR_HTTPS_URL_PATTERN = Pattern.compile(
"((http|https)://)" + //协议
"([\\w\\-]+\\.)*[\\w\\-]+" + //域名
"(:[0-9]+)?" + //端口
"((/[\\w-]*)*" + //路径
"(\\?[\\w-%:;&@#+=]*)?)"); //参数
public static boolean isHttpOrHttpsUrl(String string) {
Objects.requireNonNull(string);
Matcher matcher = HTTP_OR_HTTPS_URL_PATTERN.matcher(string);
return matcher.matches();
}
//全部打印true
public static void main(String[] args) {
System.out.println(isHttpOrHttpsUrl("http://localhost:8080?abcd=123&adfdsa=2212#abc"));
System.out.println(isHttpOrHttpsUrl("http://localhost:8080?abcd=123"));
System.out.println(isHttpOrHttpsUrl("http://localhost:8080"));
System.out.println(isHttpOrHttpsUrl("http://localhost"));
System.out.println(isHttpOrHttpsUrl("http://www.sina.com"));
System.out.println(isHttpOrHttpsUrl("http://www.sina.com/abcd/adbc/?fileName=dasfj+@:;"));
}
更多推荐
所有评论(0)