日期:2014-05-20 浏览次数:20883 次
String[] s = { "1,2,3,4", "1,2,3,4", "1,2,3,4", "1,2,3,4," }; for (int i = 0; i < s.length; i++) { System.out.println(s[i].matches("(\\d[,,])+(\\d[,,]?)?")); }
------解决方案--------------------
上面的那个有 bug,改进一下:
public class Test { public static void main(String[] args) { String[] s = { "1,2,3,4", "1,2,3,4", "33", "1,2,3,4", "1,2,3,4,", "1,3,4,5,6,7,8,9,1000," }; String pattern = "(?:\\d(?!\\d)[,,]?)+"; for (int i = 0; i < s.length; i++) { System.out.println(s[i] + " " + s[i].matches(pattern)); } } }
------解决方案--------------------
str.matches("(\\d+\\W+?)+")
------解决方案--------------------