import re
regex = re.compile(r"((?:\\033|\e|\\x1B|\\x1b){1}(?:\[(?:=|\?|[0-9]*)[0-9]*[a-gA-GnmsuJKhlHL]{0,1}(?:(?:\;[0-9]*)*m){0,1}))")
test_str = ("0x7ffff00196a0 \"\\r\\r\\r\\033[9999B[\\033[m\\033[36madmin\\033[m@\\033[m\\033[32mLeonards_switch\\033[m] > \"\n"
"\\r routerboard: yes\\r\\n model: CRS112-8P-4S\\r\\n revision: r2\\r\\n serial-number: F1 4F0FC0637C\\r\\n firmware-type: qca8513 L \\r \\n f a c t o r y - f i r m w a r e : 6 . 4 7 . 1 0 \\r \\n c u r r e n t - f i r m w a r e : 6 . 4 7 . 1 0 \\r \\n u p g r a d e - f i r m w a r e : 6 . 4 7 . 1 0 \\r\n"
"\\x1b[1;31mHello \\x1b[2;37;41mWorld\n"
"\\033[38;2;1;22;252m\n"
"\\033[38;5;5m\n"
"\\033[48;5;5m\n"
"\\033[48;2;15;251;3m\n"
"\\033[0J\n"
"\\033[J\n"
"\\033[1J\n"
"\\033[3J\n"
"\\033[K\n"
"\\033[0K\n"
"\\033[u\n"
"\\033[s\n"
"\\033[8\n"
"\\033[7\n"
"\\033[m\n"
"\\033[6n\n"
"\\033[5G\n"
"\\033[=14h\n"
"\\033[=15l\n"
"\\033[?25h\n"
"\\033[?1049l")
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