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!
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.
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.
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…
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"
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.