script
Removing spaces and characters from string
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the equivalent regex.
Thanks
Marked As Solved
Eiji
@script Depends for your use case there are few solutions. For some of them Regex is not even required.
Answering your question without directly:
String.replace("1000 cfu/ml", ~r"[a-z /]", "")
Here are some example other solutions:
#1 String.slice/3:
string = "1000 cfu/ml"
String.slice(string, 0, String.length(string) - 7)
#2 String.split/2:
"1000 cfu/ml" |> String.split(" ") |> List.first()
#3 not integer Regex:
"1000 cfu/ml" |> String.split(~r"[^\d]", trim: true) |> List.first()
#4 Integer.parse/1:
Integer.parse("1000 cfu/ml")
# {1000, " cfu/ml"}
#5 Regex.scan/2:
~r"\d+" |> Regex.scan("1000 cfu/ml") |> List.first() |> List.first()
#6 Custom pattern-maching:
defmodule Example do
@codepoints ?0..?9
def sample(<<>>), do: <<>>
def sample(<<char::utf8, rest::binary>>) when char in @codepoints, do: <<char::utf8>> <> sample(rest)
def sample(<<_char::utf8, rest::binary>>), do: sample(rest)
end
# or
defmodule Example do
@codepoints Enum.to_list(?a..?z) ++ Enum.to_list(?A..?Z) ++ [?\s, ?/]
def sample(<<>>), do: <<>>
def sample(<<char::utf8, rest::binary>>) when char not in @codepoints, do: <<char::utf8>> <> sample(rest)
def sample(<<_char::utf8, rest::binary>>), do: sample(rest)
end
and many more …
11
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
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 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
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
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
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 AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
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
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
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 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
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







