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
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Other popular topics
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
I would like to know what is the best IDE for elixir development?
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







