删除ASCII颜色代码 [英] Remove ASCII color codes

查看:353
本文介绍了删除ASCII颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我遇到了问题。我从记录器中捕获了一些东西,输出看起来像这样:

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

我需要知道如何删除那些讨厌的ASCII颜色代码(或解析它们)。

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

推荐答案

如果它们完好无损,它们应该由ESC组成( U + 001B )加上 [加上以分号分隔的数字列表,再加上 m 。 (请参阅 https://stackoverflow.com/a/9943250/978917 。)在这种情况下,您可以删除它们写作:

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", "");

。 。 。或者您可以在检查日志时使用 less -r 来利用它们。 : - )

. . . or you can take advantage of them by using less -r when examining your logs. :-)

(注意:这是特定于颜色代码。如果你还找到其他ANSI转义序列,你会想要概括一点。我认为相当一般的正则表达式是 \\\\\ [[; \\\\] * * - - / * * [@ - 〜] 。你可能会发现< a href =http://en.wikipedia.org/wiki/ANSI_escape_code =noreferrer> http://en.wikipedia.org/wiki/ANSI_escape_code 有用。)

(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.

这篇关于删除ASCII颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆