sergio_101

sergio_101

Pattern matching against a string

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!

Most Liked

mudasobwa

mudasobwa

Creator of Cure

Indeed. We call it Matching Dragon.

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.

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

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
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...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement