Removing ASCII color codes
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.
source to share
How about this regex
replaceAll("\\d{1,2}(;\\d{1,2})?", "");
Depending on the format found here: http://bluesock.org/~willg/dev/ansi.html
source to share