shahryarjb

shahryarjb

How to remove the string concerned from the original string

Hello, I have a string which is my Url, the string is:

125-1jump-test-for12-test

now I want to remove 125- from the string by this regex

  def get_number_of_url(url_string) do
    Regex.match?(~r/^(\d+)/, "#{url_string}")
  end

I need it to be without number before - DASH, how can I edit this, I want to remove the string which the regex finds and first DASH after that ?

for example:

1jump-test-for12-test

if there are 2 125-, how will I do ? like this:

125-1jump-test-for12-test-125-

Marked As Solved

david_ex

david_ex

I thought your edit meant you wanted to remove all of them, sorry.

In that case, it’s even simpler:

def strip_leading_number(string) when is_binary(string) do
  string |> String.replace(~r/^\d+-/, "")
end

Also Liked

david_ex

david_ex

There’s no difference: String.replace/4 proxies the call to Regex.replace/4. See code

FYI, when looking at the Elixir docs for a given function (e.g. Regex.replace/4), you can click on </> in the top right where the function head is displayed and it will take you to the relevant location in the source code.

david_ex

david_ex

Probably something like this:

def strip_leading_number(string) when is_binary(string) do
  case Regex.run(~r/^\d+-/, string) do
    [match] -> string |> String.replace(match, "")
    _ -> string
  end
end
und0ck3d

und0ck3d

@shahryarjb you could also use:

iex> string = "125-1jump-test-for12-test-125-"
iex> Regex.replace(~r/^\d+-/, string, "")
"1jump-test-for12-test-125-"

@david_ex do you know if there are any big differences between the two functions? (will try to check the source later today)

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ovidiubadita
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

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
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement