import re
regex = re.compile(r"(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)))?$", flags=re.MULTILINE)
test_str = ("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")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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 Python, please visit: https://docs.python.org/3/library/re.html