6

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Leo
  • 6,480
  • 4
  • 37
  • 52
  • 1
    A side comment that I have created an Expect-like library that supports filtering out the input, such as ANSI control characters, out-of-the-box. Could give it a try. Link: https://github.com/Alexey1Gavrilov/expectit – Alexey Gavrilov Aug 07 '14 at 20:14
  • hey, I will surely take a look on that! it's apache 2 license, is it ok if I copy a snippet of your code and add to my solution? – Leo Aug 07 '14 at 20:34
  • Sure! It is Apache 2 :-). If you have any feedback post a message to the google group: https://groups.google.com/forum/#!forum/java-expectit . – Alexey Gavrilov Aug 07 '14 at 20:41
  • I've been working with expectJ for a year, only because the alternatives could not deal with expect interact(). I've saw your solution briefly but it looks like a big improvement, full of interesting ideas. I wonder if the new closure syntax will allow java developers to have an even more powerful expect implementation. Thanks a bunch! – Leo Aug 07 '14 at 23:03
  • 1
    thanks. I have created GitHub issues to add support for interact() method and Java 8 lambdas. – Alexey Gavrilov Aug 08 '14 at 06:39
  • 1
    here is a working example of the ExpectIt interact support: https://github.com/Alexey1Gavrilov/ExpectIt/blob/master/expect-java8/src/main/java/net/sf/expectit/java8/Java8Example.java – Alexey Gavrilov Jul 24 '15 at 09:01

1 Answers1

7

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.

ajb
  • 31,309
  • 3
  • 58
  • 84