At a minimum, for the ANSI TTY escape sequence, this works:
import re strip_ANSI_pat = re.compile(r""" \x1b # literal ESC \[ # literal [ [;\d]* # zero or more digits or semicolons [A-Za-z] # a letter """, re.VERBOSE).sub def strip_ANSI(s): return strip_ANSI_pat("", s) s = 'potato\x1b[01;32mpotato\x1b[0;0mpotato' print s, len(s) s1=strip_ANSI(s) print s1, len(s1)
Print
potato[01;32mpotato[0;0mpotato 32 potatopotatopotato 18
For backspaces \ b or vertical tabs or \ r vs \ n - depends on how and where it is printed, no?
source share