Python regex to match VT100 escape sequences

谁说胖子不能爱 提交于 2019-11-30 03:00:34

问题


I'm writing a Python program that logs terminal interaction (similar to the script program), and I'd like to filter out the VT100 escape sequences before writing to disk. I'd like to use a function like this:

def strip_escapes(buf):
    escape_regex = re.compile(???) # <--- this is what I'm looking for
    return escape_regex.sub('', buf)

What should go in escape_regex?


回答1:


The combined expression for escape sequences can be something generic like this:

(\x1b\[|\x9b)[^@-_]*[@-_]|\x1b[@-_]

Should be used with re.I

This incorporates:

  1. Two-byte sequences, i.e. \x1b followed by a character in the range of @ until _.
  2. One-byte CSI, i.e. \x9b as opposed to \x1b + "[".

However, this will not work for sequences that define key mappings or otherwise included strings wrapped in quotes.




回答2:


VT100 codes are already grouped(mostly) according to similar patterns here:

http://ascii-table.com/ansi-escape-sequences-vt-100.php

I think the simplest approach would be to use some tool like regexbuddy to define a regex for each VT100 codes group.




回答3:


I found the following solution to successfully parse vt100 color codes and remove the non-printable escape sequences. The code snippet found here successfully removed all codes for me when running a telnet session using telnetlib:

    def __processReadLine(self, line_p):
    '''
    remove non-printable characters from line <line_p>
    return a printable string.
    '''

    line, i, imax = '', 0, len(line_p)
    while i < imax:
        ac = ord(line_p[i])
        if (32<=ac<127) or ac in (9,10): # printable, \t, \n
            line += line_p[i]
        elif ac == 27:                   # remove coded sequences
            i += 1
            while i<imax and line_p[i].lower() not in 'abcdhsujkm':
                i += 1
        elif ac == 8 or (ac==13 and line and line[-1] == ' '): # backspace or EOL spacing
            if line:
                line = line[:-1]
        i += 1

    return line


来源:https://stackoverflow.com/questions/7857352/python-regex-to-match-vt100-escape-sequences

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!