I am trying to remove ANSI escape sequences from a string.
I have tried all solutions proposed in this post but none of them worked, thus I concluded that my case is a bit different.
I have the following code that should have replaced all ANSI escape sequences:
print("ascii: " + ascii(string))
x = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])').sub('', string)
y = re.compile(br'(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])').sub(b'', string.encode("utf-8"))
print("not escaped X: " + ascii(x))
print("not escaped Y: " + ascii(y))
however, I got the following output:
ascii: '\x1b[m>....\x1b[?1h\x1b=\x1b[?2004h>....\r\x1b[K\x1b[32m[03:33:57] blabla'
not escaped X: '>....\x1b=>....\r[03:33:57] blabla'
not escaped Y: b'>....\x1b=>....\r[03:33:57] blabla'
How can I replace all the ANSI escape sequences so the expected result would be: [03:33:57] blabla
?