需要一个判定网址的正则表达式
域名可以是中文或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.?");

解决方案 »

  1.   

    这个好像很难啊,网址的形式有好多种,很难区分啊
    比如下面几种也是正常的网址:
    http://a.b.c?d=123
    http://a.b.c/d/e.html?123
    https://a.b.c
      

  2.   

    再比如这个:http://topic.csdn.net/u/20100830/16/cc96ebeb-5b5a-46e5-89d2-f3bcd6dffd4d.html?seed=1628579897&r=68036953#r_68036953我估计就算写出来,正则表达式恐怕也得几百个字符。
      

  3.   

    String regex = "(http(s)?://([\\w-]+\\.)+[\\w-]+((:(([1-5]{0,1}[\\d]{1,4})|(6[0-4][\\d]{3} 65[0-4][\\d]{2})|(655[0-2][\\d])|(6553[0-5])))|())(/[\\w- ./?%&=]*)?)|(((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]\\d)|\\d)(\\.((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]\\d)|\\d)){3})";
    不如试下这个
      

  4.   


    首先,http是大写的时候就没过……
      

  5.   

    楼上的只能过一部分HTTP://g.cn
    HTTP://北京.cn这种应该是要被判定为 true 的
      

  6.   

    这种正则只能根据你的实例而定,想要覆盖所有的种类不可能。 List<String> list = new ArrayList<String>();
    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));
      }
      

  7.   

    public static void main(String[] args) {   
    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);
    }
      
    }
      

  8.   


    基本覆盖上这些例子就差不多了
    麻烦写下吧  呵呵9楼的基本可以了
    不过我这里只让 http 或 https 的通过 希望能改下哎 成伸手党了 呵呵
      

  9.   

    把(?i:http)//换成(?i:https?)就行了
      

  10.   


    我那个没判断这个,需要加上楼上说的(?i:https?)
      

  11.   

    这个包含的范围好宽,
    比如http://192.168.北京.1:8080这种
    额,关注~