过滤掉ANSI转义序列 [英] Filtering out ANSI escape sequences
问题描述
我有一个python脚本,该脚本试图解释分别向stdout和stdin写入和读取的数据痕迹。问题在于,这些数据充满了我不在乎的ANSI转义符。这些转义是JSON编码的,因此看起来像 \033 [A和 \033] 0;。我实际上不需要解释代码,但是我确实需要知道每个代码中包含多少个字符(您会注意到第一个序列是6个字符,而第二个序列是7个字符)。有没有直接的方法可以从我拥有的字符串中过滤掉这些代码?
I have a python script that's trying to interpret a trace of data written to and read from stdout and stdin, respectively. The problem is that this data is riddled with ANSI escapes I don't care about. These escapes are JSON encoded, so they look like "\033[A" and "\033]0;". I don't actually need to interpret the codes, but I do need to know how many characters are included in each (you'll notice the first sequence is 6 characters while the second is 7). Is there a straightforward way to filter out these codes from the strings I have?
推荐答案
控制序列的完整正则表达式(又名ANSI)转义序列)是
The complete regexp for Control Sequences (aka ANSI Escape Sequences) is
/(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/
请参考 ECMA-48第5.4节和 ANSI转义代码
这篇关于过滤掉ANSI转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!