import re
regex = re.compile(r"^(\s*(?:Su|M|Tu|W|Th|F|S|R))((?:Su|M|Tu|W|Th|F|S|R){1,2})(.*)", flags=re.MULTILINE)
test_str = ("WRF 9a-5p \n"
" WR 9-10:15a \n"
" WR 7:15-8:45p \n"
" WF 9a-12:10p \n"
" WF 9:30-11:30a \n"
" WF 8-10:45a \n"
" WF 12-2:45p\n"
" Su 9a-6p \n"
" WF 10-11:50a \n"
" W 9a-12p \n"
" W 9a-12p \n"
" W 9a-12:20p \n"
" W 9a-12:20p \n"
" W 9a-12:20p \n"
" W 9:30a-4:45p")
subst = "$1$3\\n$2$3"
result = regex.sub(subst, test_str)
if result:
print(result)
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