Why do colored strings break contains() in v0.6?



As shown in the picture.

contains doesn’t work on matching the INFO: JulzDummy... because one’s teal and the other’s grey.

Why is that?


So what’s the question?


Colored strings use special terminal control characters to set colors when printed on a compatible terminal. That string contains those special characters. Try:

julia> s = "\e[31mThis is red\e[0m"
"\e[31mThis is red\e[0m"

julia> print(s)
This is red

See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for more details.


It just seems that there should be a way to make contains color-agnostic

Is there?


Perhaps you could strip control characters using a regular expression?

Julia-0.5.1> strip_ansi_escape(s::AbstractString) = replace(s, r"\e[^m]*m", "")
strip_ansi_escape (generic function with 1 method)

Julia-0.5.1> s = "\e[31mThis is red\e[0m"
"\e[31mThis is red\e[0m"

Julia-0.5.1> strip_ansi_escape(s)
"This is red"

Might have to play around with regex to catch them all.


How do you get the string in that regex-able format, though?

When I call String(...), I just get a colored string

julia> sprint(info, "foo")
"\e[1m\e[36mINFO: \e[39m\e[22m\e[36mfoo\n\e[39m"

Also, Base uses r"\e\[[0-9]+m" as the regex to strip ansi control chars.