现有需求
表单填写域名只能填写 baseURL 或者 baseURL+端口 不带协议
否则为不合法
String url1 = ".com:90";
String url2 = "w.xx.com.cn:9900990w";
String url3 = "http://w.xx.com";
String url4 = "ww.ww";
String url5 = "www.com.com:9999";
Pattern pattern = Pattern .compile("^(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/]+|[A-Za-z0-9-~\/]+[:]{1}[\d]+)$");
System.out.println(pattern.matcher(url1).matches());
System.out.println(pattern.matcher(url2).matches());
System.out.println(pattern.matcher(url3).matches());
System.out.println(pattern.matcher(url4).matches());
System.out.println(pattern.matcher(url5).matches());
运行结果:
false
false
false
true
true