本帖最后由 tinerli 于 2011-10-08 17:44:44 编辑

解决方案 »

  1.   


    ls的不行啊
    我在这个网址验证都不行呢。
    http://tool.jz123.cn/regxtest.asp
      

  2.   

    "^\b[^a-zA-Z0-9][a-zA-Z0-9_\-]+[^a-zA-Z]\b$"   ---  搜索到即不合法
      

  3.   


    还是不对啊,这个表达式怎么我输入‘test’就是不合法的呢
    我用js的value.match(/^\b[^a-zA-Z0-9][a-zA-Z0-9_\-]+[^a-zA-Z]\b$/)来验证的
      

  4.   

    /^[^-_][a-zA-Z0-9]+([-_]+[a-zA-Z]+[\d]*)*$/
      

  5.   

    <SCRIPT Language="VBScript">
    Function reg(Str)
    Dim Re,Match,Matches 
    Set Re = New Regexp
    Re.Global = True
    Re.Ignorecase = True Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z]\b$"
    Str=re.Replace(Str,"") If str = "" Then
    MsgBox "合法"
    Else
    MsgBox "不合法"
    End if
    End Function</SCRIPT><input type="text" id="sss" value=""><input type="button" value="ccc" onclick="reg(sss.value)">js我不是很熟悉,vbs的,测试过了,你放在页面中试试看吧
      

  6.   

    楼主重复发帖愿意送分,我也重复上代码:我刚学javascript 语法还不懂,(好像只是\\和\有区别。楼主自行修改),下面的java代码 ,可以对付上个第10楼的例子
    regex = "^[a-zA-Z\\d]+([a-zA-Z][a-zA-Z\\d]|[a-zA-Z\\d-_]*([a-zA-Z]|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]))$";
     
      

  7.   

    这是详细的java代码,如果你发现哪个匹配不对 很可能是我理解题意错误,你楼下指出bug,再进行修正
    import java.util.regex.Pattern;public class TestRegex {
        public static void main(String[] args) {
        String[] sourceStrings = {
            "-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
            ,"test","test_test123","test-test123"//这是你给出的正确例子
            ,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
            ,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
            ,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
        };//不存在-_时,至少有一个字母    
        
        
        String regex = "^[a-zA-Z\\d]+([a-zA-Z][a-zA-Z\\d]|[a-zA-Z\\d-_]*([a-zA-Z]|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]))$";
        //以字母、数字和'-'和'_'组成,
        //命名必需以非'-'和'_'开头,
        //末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
        //不能以纯数字命名。
        
        Pattern pattern = Pattern.compile(regex);
        
        for(int i=0; i<sourceStrings.length; i++){
            System.out.println("----------------------------------");
            System.out.println(sourceStrings[i] + "  匹配吗? " );
            System.out.println(pattern.matcher(sourceStrings[i]).find());
        }    
        }
    }
    /*output:
    ----------------------------------
    -test  匹配吗? 
    false
    ----------------------------------
    _test  匹配吗? 
    false
    ----------------------------------
    test_  匹配吗? 
    false
    ----------------------------------
    test_123  匹配吗? 
    false
    ----------------------------------
    test-  匹配吗? 
    false
    ----------------------------------
    test-123  匹配吗? 
    false
    ----------------------------------
    123  匹配吗? 
    false
    ----------------------------------
    test  匹配吗? 
    true
    ----------------------------------
    test_test123  匹配吗? 
    true
    ----------------------------------
    test-test123  匹配吗? 
    true
    ----------------------------------
    1test2  匹配吗? 
    true
    ----------------------------------
    te1st  匹配吗? 
    true
    ----------------------------------
    1test2  匹配吗? 
    true
    ----------------------------------
    test2  匹配吗? 
    true
    ----------------------------------
    1te-st2A  匹配吗? 
    true
    ----------------------------------
    te-1stA  匹配吗? 
    true
    ----------------------------------
    1te-st2A  匹配吗? 
    true
    ----------------------------------
    te-st2A  匹配吗? 
    true
    ----------------------------------
    1te-st2  匹配吗? 
    true
    ----------------------------------
    te-1st2  匹配吗? 
    true
    ----------------------------------
    1te-st2  匹配吗? 
    true
    ----------------------------------
    te-st2  匹配吗? 
    true
    */
      

  8.   

    //上面的java代码,其实是由三个部分合并而成,请看下面的做法
    import java.io.UnsupportedEncodingException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class TestRegex {
        public static void main(String[] args) {
        String[] sourceStrings = {
            "-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
            ,"test","test_test123","test-test123"//这是你给出的正确例子
            ,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
            ,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
            ,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
        };//不存在-_时,至少有一个字母    
            
        String regex = "^[a-zA-Z\\d]+[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
        regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
        regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时
        
        //以字母、数字和'-'和'_'组成,
        //命名必需以非'-'和'_'开头,
        //末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
        //不能以纯数字命名。
        
        Pattern pattern = Pattern.compile(regex);
        
        for(int i=0; i<sourceStrings.length; i++){
            System.out.println("----------------------------------");
            System.out.println(sourceStrings[i] + "  匹配吗? " );
            System.out.println(pattern.matcher(sourceStrings[i]).find());
        }    
        }
    }
    /*
    ----------------------------------
    -test  匹配吗? 
    false
    ----------------------------------
    _test  匹配吗? 
    false
    ----------------------------------
    test_  匹配吗? 
    false
    ----------------------------------
    test_123  匹配吗? 
    false
    ----------------------------------
    test-  匹配吗? 
    false
    ----------------------------------
    test-123  匹配吗? 
    false
    ----------------------------------
    123  匹配吗? 
    false
    ----------------------------------
    test  匹配吗? 
    true
    ----------------------------------
    test_test123  匹配吗? 
    true
    ----------------------------------
    test-test123  匹配吗? 
    true
    ----------------------------------
    1test2  匹配吗? 
    true
    ----------------------------------
    te1st  匹配吗? 
    true
    ----------------------------------
    1test2  匹配吗? 
    true
    ----------------------------------
    test2  匹配吗? 
    true
    ----------------------------------
    1te-st2A  匹配吗? 
    true
    ----------------------------------
    te-1stA  匹配吗? 
    true
    ----------------------------------
    1te-st2A  匹配吗? 
    true
    ----------------------------------
    te-st2A  匹配吗? 
    true
    ----------------------------------
    1te-st2  匹配吗? 
    true
    ----------------------------------
    te-1st2  匹配吗? 
    true
    ----------------------------------
    1te-st2  匹配吗? 
    true
    ----------------------------------
    te-st2  匹配吗? 
    true
    */
      

  9.   

    ^(?![0-9]+$)[^-_]([a-zA-Z0-9-]+|_(?![_0-9]+$|$))+$
      

  10.   


    ----------------------------------
    test-  匹配吗? 
    true
    ----------------------------------
    test-123  匹配吗? 
    true这两个你匹配错了
      

  11.   

    是吗,楼主的要求里没有这么说啊。如果末尾也不能是'-'或者'-'加数字,应该这样:
    ^(?![0-9]+$)[^-_]([a-zA-Z0-9]+|[_-](?![_0-9-]+$|$))+$
      

  12.   


    也是错误的,比如 :
    #abc  匹配吗? 
    true
      

  13.   

    一个正则不行就多来几个
    <SCRIPT Language="VBScript">
    Function reg(Str)
        Dim Re,Match,Matches 
        Set Re = New Regexp
            Re.Global = True
            Re.Ignorecase = True        Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z0-9]\b$"
            ck = re.Replace(Str,"")
            If ck = "" Then
                reg = "合法"
            Else
                reg = "不合法"
    Exit Function 
            End If        Re.Pattern="^\b[0-9]+\b$"
            ck = re.Replace(Str,"")
            If ck = "" Then
                reg = "不合法"
    Exit Function 
            Else
                reg = "合法"
            End If        Re.Pattern="^\b.*[\_|\-][0-9]*\b$"
            ck = re.Replace(Str,"")        If ck = "" Then
                reg = "不合法"
    Exit Function 
            Else
                 reg = "合法"
           End If

    End Functionarr = "-test,_test,test_,test_123,test-,test-123,123,test,test_test123,test-test123,1test2,te1st,1test2,test2,1te-st2A,te-1stA,1te-st2A,te-st2A,1te-st2,te-1st2,1te-st2,te-st2"
    arr = Split(arr,",")
    For i = 0 To UBound(arr)
    Document.write arr(i) & "-" & reg(arr(i)) & "<br>"
    next
    </SCRIPT>
      

  14.   

    ^(?![0-9]+$)([a-zA-Z0-9-]+|(?!^)_(?![_0-9]+$|$))+$谢谢啊,帮我找出错误来,当时没有仔细想,现在这个修改版你再看看。
    btw,失误总是难免的,比如你的a123就不能通过
      

  15.   

    上面又错了,试试这个
    ^(?![0-9]+$)(?![-_])([a-zA-Z0-9-]+|_(?![_0-9]+$|$))+$
      

  16.   


    这个错了
    ----------------------------------
    test-123  匹配吗? 
    true
      

  17.   


    当初的是这个。不知道合并的时候 是不是给我改了
       regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
    regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
    regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时

      

  18.   


        //不好意思合并的时候 出了点误差,把*误看做是+了
        //原始是这样的
        regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
        regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
        regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时    //合并后(现在的版本):
        regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*([a-zA-Z]$|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$)";