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 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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
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
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
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
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,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New








