makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number from a URL on the target web page… here’s what I’ve written:
# url is in "https://xxyyzz.com/xxyyzz.383254/" format... goal is to extract 383254
def get_id_from_url(url), do: Regex.run(~r"\d+\/", url) |> Enum.at(0) |> Integer.parse |> elem(0)
This seems a bit clunky of a function to me for a simple integer extraction… is there a better way of going about this?
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hassan
How about
dimitarvp
Test it in
iex:You can abuse Elixir’s allowed syntax of pattern matching on a string suffix (you cannot pattern-match strings in the middle of the bigger string though, have that in mind). Not sure if I am not taking your example too literally but if I understood you correctly, that’s how I would approach the problem.
NobbZ
You can use
hdinstead ofEnum.at. that saves you some characters.Also you can use
String.to_integerinstead of piping throughInteger.parseandelem.But that’s the way to go.
makeitrein
Many thx for suggestions! Realized my URL example is a bit lacking… the xxyyzz is representative of any numbers and letters of unknown length, so just stripping non-digits won’t work, nor will the pattern matching (I think, awol from computer).
I guess the challenge is to find a series of numbers at the end of a string that starts with a period and ends with a slash… String.to_integer looks like it might be what I need, good enough for government work.
dimitarvp
Nope! We have enough borked gov’t systems. Let’s do better.
If you give us a few examples and/or explain the whole URL schemata then I can help you better.
makeitrein
Hah, fair game ^^
https://foster.com/death-pancake.1468/ === 1468
https://hkd33.net/mr-rogers101.690153/ === 690153
https://space-force911.gov/sauce-master.13257777/=== 13257777
Here’s a selection of random URLs… all begin with https, have a base domain, followed by the username of the person who submitted the domain followed by a period followed by the id of the post followed by a trailing slash…
Only the id of the post is relevant so we can ignore the base domain, username, period, and ending slash…
dimitarvp
So all URLs always end in a number plus a forward slash? No exceptions?
makeitrein
Correctamundo
dimitarvp
(EDIT 1: Account for invalid values.)
(EDIT 2: Trim empty strings when splitting.)
(EDIT 3: Included explanations.)
Test it:
Breaking it down:
~w(. /)equals[".", "/"](soString.splitis called with multiple separators).parts: 1000is used to prevent denial-of-service attacks, in case somebody manages to smuggle huge strings to your code.trim: trueremoves empty strings from the result. CheckString.splitdocs."https://foster.com/death-pancake.1468/" |> String.split(~w(. /), parts: 1000, trim: true)yields this:…so we are calling
List.laston it to give us the desirable piece of data.parse_idhas to also handle invalid data:String.splitreturns[],List.lastwould returnnil.String.splitreturns["single_invalid_url"],List.lastwould return"single_invalid_url".Both cases would make our internal function
parse_idto return:error. (Integer.parsewill return:errorif you supply it a string that does NOT start with an integer.)The
fetch_idinternal function uses function heads instead ofiforcaseto extract successful integer parsing and return it, or react to an:errorreturn value and just pass it down the line to your consumer code.One caveat: notice that
fetch_idmatches on{number, ""} when is_integer(number)which means the function will be called only if a full integer string is passed, namely “123” or “456” will succeed but “123xyz” will not. If you expect URLs like “https://whatever.man/1234abcd”, this code won’t work.makeitrein
Dang, that’s some good looking code! Copy and pasting in 3, 2, 1…