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
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










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…