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
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Other popular topics
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
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
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









