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)

und0ck3d

und0ck3d

nice! thanks for the tip!

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
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
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement