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
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
1
Also Liked
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.
4
kip
ex_cldr Core Team
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.
1
edmundobiglia
Thanks a million, works like a charm! 
1
Popular in Questions
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
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
I would like to know what is the best IDE for elixir development?
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
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
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
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
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
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
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
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
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
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
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
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







