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
Indeed. We call it Matching Dragon.
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
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







