被验证字符串: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("没验证");
}
}
}
实际应用中,字符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("没验证");
}
}
}
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("没验证");
} }
}
这样来写
(\\d+,?)+
\\d+指多个数字,而,?表明0/1个,其实就表明,号是可有可无的,那么你的\\d增加个+号就无意义了,
所以何不(\\d,?)+这样。如果你的数字个数是能确实定的,那么
(\\d{x,x},?)+这样。
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("没验证");
} }
加个?号就好了,贪婪匹配。