sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor do a pattern match against a string. This is exactly the first step i needs to take for my first phoenix project.
in my project, I am receiving a ton of strings that look like:
<li>Kiwi Jr. (33)</li>
<li>Deep State (29)</li>
<li>Piroshka (29)</li>
where he did something like:
iex(1)> string = "<li>Kiwi Jr. (33)</li>"
"<li>Kiwi Jr. (33)</li>"
iex(2)> "<li>"<>artist<>"("<>playcount<>"</li>" = string
** (ArgumentError) the left argument of <> operator inside a match should be always a literal binary as its size can't be verified, got: artist
obviously, i am missing something .. I would like to end up with:
artist = Kiwi Jr.
play count = 33
anyone see where i am putting it in the ditch?
thanks!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
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
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
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
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
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 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
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
- #umbrella
- #full-time-contract
- #elixirconf-us
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Unless you know exactly how many characters your artist has you’ll need to resort to using regex or some other tool for parsing the string. Pattern matches cannot do what you seem to be looking for.
blatyo
That error is complaining about this part of your code specifically:
artist <> "("The way matching on binaries works is that:
Because artist and playcount are not at the end of the match, it’ll fail because you’re don’t match the second or third rule.
Hence @LostKobrakai’s suggestion
mudasobwa
Almost true
In 99% of cases it would go through pattern match, making the code faster than
Regex.sergio_101
okay, this is assuming that the data matches the length requirement in line 2?
in the example i saw, it was something simple like:
I’ll give this a shot.
Thanks!
mudasobwa
Nope. The code above generates 1001 functions handling all possible combinations of lengths for
artistandnumin the intervals1..100and1..10respectively. Plus one sink-all clause in the lengths are not in these intervals.One cannot pattern-match the binary of arbitrary length in the middle, but a match to the binary of the explicit length is allowed.
NobbZ
Close, but wrong
it generates a single function with 1001 heads.
mudasobwa
Indeed. We call it Matching Dragon.
sergio_101
okay.. so, my next question. is there something more “functional” about doing it this way, rather than a straight pattern match? i have spent the past 30 years in the OOP world. I used Lisp maybe 30 years ago, but didn’t know enough to really make the distinction back then.
I know I initially asked about doing it with a pattern match, just because i saw that in a course, and EVERY TIME i need to do regex, i need to look at the docs..
Thanks!
mudasobwa
You probably meant “rather than a straight regex.” Well, it depends . In most cases
Regexis just fine. Also, if you are after parsing the (contrived example) ISO8601 representation of a date, you might extractyear,monthanddaystraight away:There is no silver bullet.
dimitarvp
If these are guaranteed to be small HTML pieces I’d parse them with Floki or Meeseks and then apply a simpler regex on the text to get the two pieces of data you require.
Regex for HTML or XML is a hard “NO!” even if you do a two-days educational throwaway project.