Discuss / Java / 这个题主要还是在分组匹配上的设置要求比较细节

这个题主要还是在分组匹配上的设置要求比较细节

Topic source

乞与追逐

#1 Created at ... [Delete] [Delete and Lock User]

public static int[] parseTime(String s) {

// FIXME:

if(s==null) {

throw new IllegalArgumentException();

}

Pattern pattern=Pattern.compile("([0-1][0-9]||2[0-3]):([0-5][0-9]):([0-5][0-9])");

Matcher matcher=pattern.matcher(s);

int[] array=new int[3];

if(matcher.matches()) {

String hour=matcher.group(1);

String minute=matcher.group(2);

String second=matcher.group(3);

array[0]=Integer.valueOf(hour);

array[1]=Integer.valueOf(minute);

array[2]=Integer.valueOf(second);

// Integer hour=Integer.parseInt(matcher.group(1));//两种都可以

// Integer minute=Integer.parseInt(matcher.group(2));

// Integer second=Integer.parseInt(matcher.group(3));

// array[0]=hour;

// array[1]=minute;

// array[2]=second;

return array;

}else {

throw new IllegalArgumentException();

}

}

}


  • 1

Reply