Getting correct string length in Python for strings with ANSI color codes
The pyparsing wiki includes this helpful expression for matching on ANSI escape sequences: ESC = Literal(‘\x1b’) integer = Word(nums) escapeSeq = Combine(ESC + ‘[‘ + Optional(delimitedList(integer,’;’)) + oneOf(list(alphas))) Here’s how to make this into an escape-sequence-stripper: from pyparsing import * ESC = Literal(‘\x1b’) integer = Word(nums) escapeSeq = Combine(ESC + ‘[‘ + Optional(delimitedList(integer,’;’)) + oneOf(list(alphas))) … Read more