Leo
Leo

Reputation: 1443

How to remove string "\u001b[1m" in java

After I receive a content response of the server, and print the content to console and I realize existing of the string "\u001b[1m", and I think it's the reason I can't parse the conte tresponse as a JSON object.

And I want to remove the string, any solution?

Upvotes: 0

Views: 3547

Answers (5)

Dmitry Budakov
Dmitry Budakov

Reputation: 1

sed  -E 's/\\u001b\[[0-9][0-9]?m//g'

Upvotes: 0

Foubar
Foubar

Reputation: 21

During the development of an SSH program that also utilizes command line mysql commands, I created the following regex that I strip these sequences with:

\u001b[\[\(]([0-9];[0-9]|\?[0-9]+)?[Bmh]

It covers the ranges that I have run into issues with

Upvotes: 0

Ry-
Ry-

Reputation: 225095

That's an escape sequence, and I think it's used to turn on formatting in some things like the Terminal. Is that really supposed to be JSON? What's the rest of the text? Does that escape sequence come as plain text or as one character?

Upvotes: 0

david van brink
david van brink

Reputation: 3642

Make sure you know what's actually in the string -- try examining (and maybe printing) it character by character using someString.charAt().

And, String newString = someString.replaceAll(oldexp); is one of many ways you might try to "fix" it, if it turns out that's needed.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

That looks like an ANSI video escape sequence to turn bold mode on. Are you sure that the server is sending back a JSON object?

Upvotes: 6

Related Questions