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
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
@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 …
Thanks for your brief explanation and answer.