import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(0\\d:[0-5]\\d)|(^1[01]:[0-5]\\d)|(^12:[0-2]\\d)|(^12:3[0-5])";
final String string = "00:00\n"
+ "01:00\n"
+ "02:00\n"
+ "03:00\n"
+ "04:00\n"
+ "05:00\n"
+ "06:00\n"
+ "07:00\n"
+ "08:00\n"
+ "09:00\n"
+ "10:00\n"
+ "11:00\n"
+ "12:00\n"
+ "12:09\n"
+ "12:19\n"
+ "12:29\n"
+ "12:30\n"
+ "12:31\n"
+ "12:32\n"
+ "12:33\n"
+ "12:34 \n"
+ "12:35 midday (all days of week)\n"
+ "12:36\n"
+ "13:00\n"
+ "14:00\n"
+ "15:00\n"
+ "16:00\n"
+ "17:00\n"
+ "18:00\n"
+ "19:00\n"
+ "19:35 evening (sun-fri)\n"
+ "20:00\n"
+ "20:05 evening (saturday)\n"
+ "21:00\n"
+ "22:00\n"
+ "23:00";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html