# HG changeset patch # User Goffi # Date 1499018024 -7200 # Node ID ad2a8e8b52daf76f2d372271ee64c8c4cea5807b # Parent 1a64fd7b648dfbf09ce6ffb75493ee7566d10419 core (tools/common/regex): new ansiRemove method to remove ANSI escape codes from a string diff -r 1a64fd7b648d -r ad2a8e8b52da src/tools/common/regex.py --- a/src/tools/common/regex.py Sun Jul 02 19:52:21 2017 +0200 +++ b/src/tools/common/regex.py Sun Jul 02 19:53:44 2017 +0200 @@ -23,6 +23,8 @@ path_escape = {'%': '%25', '/': '%2F', '\\': '%5c'} path_escape_rev = {re.escape(v):k for k, v in path_escape.iteritems()} path_escape = {re.escape(k):v for k, v in path_escape.iteritems()} +# thanks to Martijn Pieters (https://stackoverflow.com/a/14693789) +RE_ANSI_REMOVE = re.compile(r'\x1b[^m]*m') def reJoin(exps): @@ -60,3 +62,11 @@ @return (str, unicode): unescaped string """ return reSubDict(path_escape_rev_re, path_escape_rev, string) + +def ansiRemove(string): + """Remove ANSI escape codes from string + + @param string(basestr): string to filter + @return (str, unicode): string without ANSI escape codes + """ + return RE_ANSI_REMOVE.sub('', string)