edmundobiglia

edmundobiglia

How to remove ANSI escape sequences from a string?

What is the best way to strip ANSI escape sequences from a string?

Go from:

# Go from
"[\e[32m\"one\"\e[0m, \e[32m\"two\"\e[0m, \e[32m\"three\"\e[0m]"

# To
["one", "two", "three"]

Thanks a million!

Marked As Solved

ericgray

ericgray

You can use a Regex
I just grabbed one online and used it. Not really good with Regexes so not sure how efficient this is.

defmodule MyAnsi do
  @ansi_regex ~r/(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/
  @non_alpha_regex ~r/[^0-9a-zA-Z]/

  def strip_ansi(ansi_string) when is_binary(ansi_string) do
    Regex.replace(@ansi_regex, ansi_string, "")
    |> String.split(",")
    |> Enum.map(&Regex.replace(@non_alpha_regex, &1, ""))
  end

end

Also Liked

kip

kip

ex_cldr Core Team

Would produce a very expected output for all the people of the world that don’t use 7-bit ASCII scripts.

kip

kip

ex_cldr Core Team

Good catch! Yes, unexpected!

I read the ECMA-48 spec and yes, quite a lot more complex than just colour code!

axelson

axelson

Scenic Core Team

Did you mean to say unexpected?

As a side-note I half (but probably less) implemented an ANSI parser, but ultimately changed the program output I was consuming to be most Lynn plain text. There’s quite a number of potential ANSI codes that you’d have to deal with for an accurate parser.

edmundobiglia

edmundobiglia

Thanks a million, works like a charm! :slightly_smiling_face:

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement