How can I remove the ANSI escape sequences from a string in python

前端 未结 6 730
太阳男子
太阳男子 2020-11-22 14:26

This is my string:

\'ls\\r\\n\\x1b[00m\\x1b[01;31mexamplefile.zip\\x1b[00m\\r\\n\\x1b[01;31m\'

I was using code to retrieve the output from

6条回答
  •  -上瘾入骨i
    2020-11-22 15:05

    The accepted answer to this question only considers color and font effects. There are a lot of sequences that do not end in 'm', such as cursor positioning, erasing, and scroll regions.

    The complete regexp for Control Sequences (aka ANSI Escape Sequences) is

    /(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/
    

    Refer to ECMA-48 Section 5.4 and ANSI escape code

提交回复
热议问题