Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So, I'm having an issue. I'm catching some stuff from a 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 to parse them).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
394 views
Welcome To Ask or Share your Answers For Others

1 Answer

If they're intact, they should consist of ESC (U+001B) plus [ plus a semicolon-separated list of numbers, plus m. (See https://stackoverflow.com/a/9943250/978917.) In that 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 examining your logs. :-)

(Note: this is specific to color codes. If you also find other ANSI escape sequences, you'll want to generalize that a bit. I think a fairly general regex would be u001B\[[;\d]*[ -/]*[@-~]. You may find http://en.wikipedia.org/wiki/ANSI_escape_code to be helpful.)

If the sequences are not intact — that is, if they've been mangled in some way — then you'll have to investigate and figure out exactly what mangling has happened.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...