import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(([A-Za-z]{3}\\s[0-9]{1,2})\\s?([0-9]{2}:[0-9]{2})?\\s?-?\\s?([0-9]{2}:[0-9]{2})?\\s?([A-Z0-9]{3,4})?\\s?)";
final String string = "Sun 16\n"
+ "08:00 -\n"
+ "16:00\n"
+ "RTW\n"
+ "Mon 17\n"
+ "08:00 - 16:00\n"
+ "RTW\n"
+ "Tue 18\n"
+ "08:00 -\n"
+ "16:00\n"
+ "RTW\n"
+ "Wed 19\n"
+ "08:00 -\n"
+ "16:00\n"
+ "RTW\n"
+ "Thu 20\n"
+ "08:00 - 16:00\n"
+ "RTW\n"
+ "Fri 21\n"
+ "08:00 - 16:00\n"
+ "RTW\n"
+ "Sat 22\n"
+ "OFF\n"
+ "Sun 23\n"
+ "OFF\n"
+ "Mon 24\n"
+ "OFF\n"
+ "Tue 25\n"
+ "04:40 -\n"
+ "13:07\n"
+ "5180\n"
+ "KM+\n"
+ "Wed\n"
+ "26\n"
+ "04:11\n"
+ "-\n"
+ "12:22\n"
+ "5178\n"
+ "Thu 27\n"
+ "10:00 - 18:00\n"
+ "Work.Group\n"
+ "Scheduling &\n"
+ "Rostering 1300-\n"
+ "1500 @Burwood...\n"
+ "Fri 28\n"
+ "OFF\n"
+ "Sat 29\n"
+ "NTA";
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