import re
regex = re.compile(r"\b([01][0-9]|2[0-3])([0-5][0-9])\b")
test_str = ("0000\n"
"2359\n"
"1200\n"
"1201\n"
"0059\n\n"
"0090\n"
"2459\n"
"000\n"
"3000\n")
subst = "\\1:\\2"
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