Removing ASCII color codes

So I have a problem. I am catching some stuff from the Logger and the output looks something like this:

11:41:19 [INFO] ←[35;1m[Server] hi←[m

      

I need to know how to remove those pesky ASCII color codes (or parse them).

+3


source to share


2 answers


If they are intact, they should be ESC ( U+001B

) plus [

plus a list of numbers separated by semicolons plus m

. (See fooobar.com/questions/528874 / .... ) In this case, you can remove them by writing:

final String msgWithoutColorCodes =
    msgWithColorCodes.replaceAll("\u001B\\[[;\\d]*m", "");

      

., or you can take advantage of them by using less -r

when researching your logs. :-)



(Note: this refers to color codes. If you find other ANSI escape sequences as well, you'd like to generalize this a bit. I think a pretty general regex would be \u001B\\[[;\\d]*[ -/]*[@-~]

. You can find http://en.wikipedia.org/wiki/ ANSI_escape_code to be helpful.)

If the sequences are not intact - that is, if they are mutilated in some way; then you have to investigate and find out exactly what happened.

+16


source


How about this regex

replaceAll("\\d{1,2}(;\\d{1,2})?", "");



Depending on the format found here: http://bluesock.org/~willg/dev/ansi.html

-1


source







All Articles