sergio_101

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!

Showing Posts 1 to 10

LostKobrakai

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

blatyo

Conduit Core Team

That error is complaining about this part of your code specifically:

artist <> "("

The way matching on binaries works is that:

  • it can match a binary literal like “(” because it knows its size
  • it can bind to a variable, where the length is specified
  • it can bind the rest of the binary to a variable, when it appears at the end of the match

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

mudasobwa

Creator of Cure

Almost true :slight_smile:

defmodule Matcher do
  for artist_len <- 1..100, num_len <- 1..10 do
    def li(<<
            "<li>",
            artist :: binary-size(unquote(artist_len)),
            " (",
            num :: binary-size(unquote(num_len)),
            ")</li>"
        >>), do: {artist, num}
  end


    # last resort clause
    def li(input),
      do: Regex.scan(
        ~r"<li>(.*?)\s*\((.*?)\)</li>", input,
        capture: :all_but_first
      )
end

Matcher.li("<li>Kiwi Jr. (33)</li>")
#⇒ {"Kiwi Jr.", "33"}

In 99% of cases it would go through pattern match, making the code faster than Regex.

sergio_101

sergio_101 OP

okay, this is assuming that the data matches the length requirement in line 2?

in the example i saw, it was something simple like:

iex(6)> s = "categories:1"
"categories:1"
iex(7)> "categories:"<>index=s
"categories:1"
iex(8)> index
"1"

I’ll give this a shot.

Thanks!

mudasobwa

mudasobwa

Creator of Cure

Nope. The code above generates 1001 functions handling all possible combinations of lengths for artist and num in the intervals 1..100 and 1..10 respectively. 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

NobbZ

Close, but wrong :wink: it generates a single function with 1001 heads.

mudasobwa

mudasobwa

Creator of Cure

Indeed. We call it Matching Dragon.

sergio_101

sergio_101 OP

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

mudasobwa

Creator of Cure

You probably meant “rather than a straight regex.” Well, it depends . In most cases Regex is just fine. Also, if you are after parsing the (contrived example) ISO8601 representation of a date, you might extract year, month and day straight away:

<<
    year :: binary-size(4), "-",
    month :: binary-size(2), "-",
    day :: binary-size(2)>> = "2019-03-20"
year
#⇒ "2019"

There is no silver bullet.

dimitarvp

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.

Where Next? Top

Trending in Questions Top

katta
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
peck
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
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
Alvinkariuki
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

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
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
mudasobwa
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
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
budgie
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews