How to remove ANSI control chars (VT100) from a Java String

天大地大妈咪最大 提交于 2019-11-30 22:26:24

Most ANSI VT100 sequences have the format ESC [, optionally followed by a number or by two numbers separated by ;, followed by some character that is not a digit or ;. So something like

reply = reply.replaceAll("\u001B\\[[\\d;]*[^\\d;]","");

or

reply = reply.replaceAll("\\e\\[[\\d;]*[^\\d;]","");  // \e matches escape character

should catch most of them, I think. There may be other cases that you could add individually. (I have not tested this.)

Some of the alternatives in the regex you posted start with \\[, rather than the escape character, which may mean that you could be deleting some text you're not supposed to delete, or deleting part of a control sequence but leaving the ESC character in.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!