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
Popular in Questions
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lists...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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 have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
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
Other popular topics
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
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
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?
Would
mix ecto.rollback -v 200809061...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









