script

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

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
Post #2

Where Next?

Popular in Questions Top

lessless
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
jerry
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
sergio_101
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
Kagamiiiii
Student &amp; 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
Phillipp
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
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
siddhant3030
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 Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
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
ycv005
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
lucidguppy
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
joeerl
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

We're in Beta

About us Mission Statement