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

时光总嘲笑我的痴心妄想 提交于 2019-12-19 03:22:30

问题


I am working with automation and using Jsch to connect to remote boxes and automate some tasks.

I am having problem parsing the command results because sometimes they come with ANSI Control chars.

I've already saw this answer and this other one but it does not provide any library to do that. I don't want to reinvent the wheel, if there is any. And I don't feel confident with those answers.

Right now, I am trying this, but I am not really sure it's complete enough.

reply = reply.replaceAll("\\[..;..[m]|\\[.{0,2}[m]|\\(Page \\d+\\)|\u001B\\[[K]|\u001B|\u000F", "");

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


回答1:


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.



来源:https://stackoverflow.com/questions/25189651/how-to-remove-ansi-control-chars-vt100-from-a-java-string

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