public static int[] parseTime(String s) { // FIXME: if (s == null) { throw new IllegalArgumentException(); } Pattern p = Pattern.compile("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"); Matcher m = p.matcher(s); if(m.matches()) { //返回一个int数组 return new int[] {Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3))}; }else { throw new IllegalArgumentException(); } }
之前在这里犯了个错误,在使用或 | 的匹配时,或 | 前后的子正则串 比如[0-1][0-9]|2[0-3] 不能写成([0-1][0-9])|(2[0-3]),否则会测试出错,编译器默认 将 | 前后的部分分别视为两个子正则串,自作聪明的加上小括号反而测试报错。
运行这段代码的测试代码会有一个Error,什么原因呢
Sign in to make a reply
_我是来微博看美女的