Discuss / Java / 忘记使用或规则匹配

忘记使用或规则匹配

Topic source

Best of Me

#1 Created at ... [Delete] [Delete and Lock User]
public static int[] parseTime(String s) {

		// FIXME:

		if (s != null) {

			Pattern pattern = Pattern.compile("(\\d{2})\\:(\\d{2})\\:(\\d{2})");

			Matcher matcher = pattern.matcher(s);

			int a, b, c;

			if (matcher.matches() && (a = Integer.parseInt(matcher.group(1))) < 24

					&& (b = Integer.parseInt(matcher.group(2))) < 60 && (c = Integer.parseInt(matcher.group(3))) < 60) {

				return (new int[] { a, b, c });

			}

		}

		throw new IllegalArgumentException();

	}


  • 1

Reply