String input = "11:59 am";
Pattern pattern =
Pattern.compile("([1-9]|1[0-2])\\s*:\\s*([0-5][0-9])\\s*([ap]m)");
Matcher matcher = pattern.matcher(input);
//matches() requires full match for the whole string,
//find() -- finds the first match
if (matcher.matches()) {
String hours = matcher.group(1); //11
String minutes = matcher.group(2); //59
String period = matcher.group(3); //am
}