import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(t(?:h[uứ])? ?(?:tư|[2-7]|hai|ba|năm|sáu|bảy)|chủ nhật)( *)(?: ?(?:đầu|cuối)? ?(?:tuần (?:trước|sau|này)))?$";
final String string = "thứ 2\n"
+ "thứ 3\n"
+ "thứ 4\n"
+ "thứ 5\n"
+ "thứ 6\n"
+ "thứ 7\n\n"
+ "t2\n"
+ "t3\n"
+ "t4\n"
+ "t5\n"
+ "t6\n"
+ "t7\n\n"
+ "thứ hai\n"
+ "thứ ba\n"
+ "thứ tư\n\n"
+ "thứ2\n"
+ "thứ3\n"
+ "chủnhật\n"
+ "Chủ nhật\n\n"
+ "thứ hai cuối tuần sau\n"
+ "thứ ba cuối tuần sau\n"
+ "thứ tư cuối tuần sau\n"
+ "thứ năm cuối tuần sau\n"
+ "thứ năm đầu tuần sau\n"
+ "thứ sáu đầu tuần sau\n"
+ "thứ bảy tuần sau\n"
+ "chủ nhật tuần trước\n"
+ "chủ nhật tuần này\n"
+ "t8 cuối tuần này\n\n\n\n"
+ "thuhai\n"
+ "thuba\n"
+ "thutu\n"
+ "thunam\n"
+ "tha thu\n";
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