需要一个判定网址的正则表达式
域名可以是中文或E文
如下边例子的前 6 个需要被判定为 True
后 7 个为 False List<string> testArray = new List<string>();
//能过
testArray.Add("HTTP://g.cn");
testArray.Add("HTTP://北京.cn");
testArray.Add("http://192.168.0.100");
testArray.Add("http://192.168.0.100:8080");
testArray.Add("http://fendou.org/2009/11/20/javascript-certi-url/");
testArray.Add("http://com.我的");
//不能过
testArray.Add("http://@.com/");
testArray.Add("http://www. com/");
testArray.Add("http://com.#/");
testArray.Add("http://com.1../");
testArray.Add("http://com");
testArray.Add("http://com.");
testArray.Add("http://com.?");
域名可以是中文或E文
如下边例子的前 6 个需要被判定为 True
后 7 个为 False List<string> testArray = new List<string>();
//能过
testArray.Add("HTTP://g.cn");
testArray.Add("HTTP://北京.cn");
testArray.Add("http://192.168.0.100");
testArray.Add("http://192.168.0.100:8080");
testArray.Add("http://fendou.org/2009/11/20/javascript-certi-url/");
testArray.Add("http://com.我的");
//不能过
testArray.Add("http://@.com/");
testArray.Add("http://www. com/");
testArray.Add("http://com.#/");
testArray.Add("http://com.1../");
testArray.Add("http://com");
testArray.Add("http://com.");
testArray.Add("http://com.?");
比如下面几种也是正常的网址:
http://a.b.c?d=123
http://a.b.c/d/e.html?123
https://a.b.c
不如试下这个
首先,http是大写的时候就没过……
HTTP://北京.cn这种应该是要被判定为 true 的
String regex="(?i:http)://([\\w\\u4e00-\\u9fa5]+\\.[\\w\\u4e00-\\u9fa5]+|((\\d+{1,3}\\.){3}\\d{1,3}(\\:\\d{1,4})?)|" +
"(([\\w\\u4e00-\\u9fa5])+\\.[\\w\\u4e00-\\u9fa5]+(/[-\\wu4e00-\\u9fa5]+){0,}/?))";
//能过
list.add("HTTP://g.cn");
list.add("HTTP://北京.cn");
list.add("http://192.168.0.100");
list.add("http://192.168.0.100:8080");
list.add("http://fendou.org/2009/11/20/javascript-certi-url/");
list.add("http://com.我的");
//不能过
list.add("http://@.com/");
list.add("http://www. com/");
list.add("http://com.#/");
list.add("http://com.1../");
list.add("http://com");
list.add("http://com.");
list.add("http://com.?");
for(String str:list){
System.out.println(str.matches(regex));
}
List<String> testArray = new ArrayList<String>();
//能过
testArray.add("HTTP://g.cn");
testArray.add("HTTP://北京.cn");
testArray.add("http://192.168.0.100");
testArray.add("http://192.168.0.100:8080");
testArray.add("http://fendou.org/2009/11/20/javascript-certi-url/");
testArray.add("http://com.我的");
//不能过
testArray.add("http://@.com/");
testArray.add("http://www. com/");
testArray.add("http://com.#/");
testArray.add("http://com.1../");
testArray.add("http://com");
testArray.add("http://com.");
testArray.add("http://com.?");
Pattern pattern=Pattern.compile(".+[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}.*|.+[\\u4e00-\\u9fa5a-zA-Z]+\\.[\\u4e00-\\u9fa5a-zA-Z].*");
for (String s : testArray){
Matcher matcher=pattern.matcher(s);
if (matcher.matches())
System.out.println(s);
}
}
基本覆盖上这些例子就差不多了
麻烦写下吧 呵呵9楼的基本可以了
不过我这里只让 http 或 https 的通过 希望能改下哎 成伸手党了 呵呵
我那个没判断这个,需要加上楼上说的(?i:https?)
比如http://192.168.北京.1:8080这种
额,关注~