被验证字符串:2010410000001,2011410600004,2011410600004a2011410600004
实际应用中,字符a是一个中文逗号,是为不让中文逗号通过验证。
验证表过式:(\d+,?)+
测试类:public class Hello { public static void main(String[] args) {

String btxmsPtn = "(\\d+,?)+";
String str = "2010410000001,2011410600004,2011410600004a2011410600004";
if(str!=null&&!"".equals(str)){//如果不空验证,如果为空直接通过
System.out.println("111");
System.out.println( str.matches(btxmsPtn));
System.out.println("222");
}else {
System.out.println("没验证");
}

}
}

解决方案 »

  1.   


    public class Hello { public static void main(String[] args) { String btxmsPtn = "[\\w,]+";
    String str = "2010410000001,2011410600004,2011410600004a2011410600004";
    if (str != null && !"".equals(str)) {// 如果不空验证,如果为空直接通过
    System.out.println("111");
    System.out.println(str.matches(btxmsPtn));
    System.out.println("222");
    } else {
    System.out.println("没验证");
    } }
    }
    这样来写
      

  2.   

    二楼的很简洁,应该把w换成d,我原意是通过用英文逗号分隔的数字字符串,有中文逗号是不能通过的,2010410000001,2011410600004,2011410600004a2011410600004中的a是一个中文逗号,如果这里直接写中文逗号,可能显示不出来那个中文逗号的效果。我想知道,我写的那个验证字符串,为什么会出现卡死的情况?
      

  3.   

    我觉得应该是你的正则有问题,估计是逻辑嵌套出来问题吧。
    (\\d+,?)+
    \\d+指多个数字,而,?表明0/1个,其实就表明,号是可有可无的,那么你的\\d增加个+号就无意义了,
    所以何不(\\d,?)+这样。如果你的数字个数是能确实定的,那么
    (\\d{x,x},?)+这样。
      

  4.   

    public static void main(String[] args) { String btxmsPtn = "(\\d?+(,?))+";
    String str = "2010410000001,2011410600004,2011410600004,2011410600004";
    if (str != null && !"".equals(str)) {// 如果不空验证,如果为空直接通过
    System.out.println("111");
    System.out.println(str.matches(btxmsPtn));
    System.out.println("222");
    } else {
    System.out.println("没验证");
    } }
    加个?号就好了,贪婪匹配。
      

  5.   

    翻了下API,叫Possessive,占有欲强的