如何从Java字符串中删除ANSI控件字符(VT100) [英] How to remove ANSI control chars (VT100) from a Java String
问题描述
我正在使用自动化并使用Jsch连接到远程盒并自动执行某些任务。
I am working with automation and using Jsch to connect to remote boxes and automate some tasks.
我在解析命令结果时遇到问题,因为有时它们会带有< a href =http://www.termsys.demon.co.uk/vtansi.htm =nofollow noreferrer> ANSI Control chars 。
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", "");
如何从Java字符串中删除ANSI控制字符(VT100)?
How to remove ANSI control chars (VT100) from a Java String?
推荐答案
大多数ANSI VT100序列的格式为 ESC
[
,可选地后跟一个数字或两个数字,用;
分隔,然后是一些不是数字的字符或;
。所以类似
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;]","");
或
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.)
你发布的正则表达式中的一些替代品以 \\ [$ c $开头c>,而不是转义字符,这可能意味着您可能正在删除一些您不应删除的文本,或者删除控制序列的一部分但是保留ESC字符。
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.
这篇关于如何从Java字符串中删除ANSI控件字符(VT100)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!