对正则表达式不太熟悉,现在有一个这样的需求,有一个输入框,可以接受一组或者多组3~21位之间的数字,如果多与一组之间用逗号隔开举例:
   1:345464564
   2:46456456,46457657567,23423432
大概就是这个意思,多谢各位了

解决方案 »

  1.   


    public class TestRegex
    {    /**
         * @param args
         */
        public static void main(String[] args)
        {        System.out.println(Regex());    }    private static boolean Regex()
        {
            String str = "6666664a,2224";
            String[] strSplit = str.split(",");        Matcher m = null;        int length = strSplit.length;
            if (length > 1)
            {
                for (int i = 0; i < length; i++)
                {
                    m = Pattern.compile("\\d{3,21}").matcher(strSplit[i]);
                    if (!m.matches())
                    {
                        return false;                    
                    }
                }
            }
            else
            {
                m = Pattern.compile("\\d{3,21}").matcher(str);
            }        return m.matches();
        }}可能不是最好的,但是能满足你的需求,
      

  2.   


    public class TestRegex
    {    /**
         * @param args
         */
        public static void main(String[] args)
        {        System.out.println(Regex());    }    private static boolean Regex()
        {
            String str = "6666664,444,333";                Matcher  m = Pattern.compile("(\\d{3,21})(,\\d{3,21})*").matcher(str);
             return m.matches();
        }}这个效率高些, 呵呵 
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
      </head><style type="text/css">
    *{font-size:12px;font-family:"courier new";}
    input.text{width:200px;height:20px;border:1px solid #c0c0c0;padding:1px 3px;}
    span.msg{color:red;padding:0 10px;}
    </style><script type="text/javascript">
    window.$ = function(id) {
      return (typeof id == 'string') ? document.getElementById(id) : id;
    }String.prototype.trim = function() {
      return this.replace(/^\s+/, '').replace(/\s+$/, '');
    }window.onload = function() {
      hover();
    }function hover() {
      var inputs = document.getElementsByTagName('input');
      if(!inputs) {
        return;
      }
      var color = '#ffee99';
      for(var i = 0; i < inputs.length; i++) {
        if(inputs[i].type != 'text') {
          continue;
        }
        inputs[i].onmouseover = inputs[i].onfocus = function() {
            this.style.backgroundColor = color;
          }
        inputs[i].onmouseout = function() {
            this.style.backgroundColor = 'transparent';
          }
      }
    }function check(frm) {
      var inp = frm.input;
      var v = inp.value.trim();
      inp.value = v;
      if(!/^[0-9]{3,21}(?:,[0-9]{3,21})*$/.test(v)) {
        $('inputMsg').innerHTML = '格式错误,请重新输入!一组只允许输入 3~21 个数字,多组使用 , 分隔';
        inp.focus();
        inp.select();
        return false;
      }
      $('inputMsg').innerHTML = '';
      return true;
    }</script><body>
      <form name="frm" action="#" method="post" onsubmit="return check(this);">
      输入框: <input type="text" name="input" class="text" /><span id="inputMsg" class="msg"></span><br />
      <input type="submit" value="提交" />
      </form>
    </body>
    </html>
      

  4.   

    "[a-zA-Z]+[0-9]+"
    例如password.matches("[a-zA-Z]+[0-9]+");  字符串password  是否属于a-z A-Z 0-9
      

  5.   

    (\d{3,21})(,\d{3,21})*
    (\d{3,21},)*(\d{3,21})
    都对·
      

  6.   

    public static final String REGEX = "(?:\\d{3,12},*)+";

    public static void main(String[] args) {

          String s = "123464,4232,34234235,453453,3";
          Matcher m = Pattern.compile(strRegex).matcher(s);
          System.out.println(m.matches());
    }