如何从 Java 字符串中删除 ANSI 控制字符 (VT100) [英] How to remove ANSI control chars (VT100) from a Java String

查看:43
本文介绍了如何从 Java 字符串中删除 ANSI 控制字符 (VT100)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动化并使用 Jsch 连接到远程设备并自动执行一些任务.

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

我在解析命令结果时遇到问题,因为有时它们带有 ANSI 控制字符.

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

您发布的正则表达式中的一些替代方案以 \[ 开头,而不是转义字符,这可能意味着您可能会删除一些不应该删除的文本,或者删除部分控制序列但保留 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屋!

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