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

**URL:** <https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 26, 2017, 3:11am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455 "2017-06-26T03:11:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 26, 2017, 3:11am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/1 "2017-06-26T03:11:20Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5ece3b81bd8988cc021f7dc6f82ffb843a0aee6.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 26, 2017, 3:57am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/2 "2017-06-26T03:57:59Z")

</div>

> [@djsegal](#):
>
> because one’s teal and the other’s grey.

So what’s the question?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 26, 2017, 3:59am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/3 "2017-06-26T03:59:32Z")

</div>

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

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

julia> print(s)
This is red

```

See [ANSI escape code - Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) for more details.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 26, 2017, 4:07am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/4 "2017-06-26T04:07:47Z")

</div>

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

Is there?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 26, 2017, 4:43am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/5 "2017-06-26T04:43:04Z")

</div>

Perhaps you could strip control characters using a regular expression?

```julia
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.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 26, 2017, 4:46am UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/6 "2017-06-26T04:46:09Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 26, 2017, 2:18pm UTC](https://discourse.julialang.org/t/why-do-colored-strings-break-contains-in-v0-6/4455/7 "2017-06-26T14:18:34Z")

</div>

```julia
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.
