Pattern match on end of a string/binary argument

Looking at this link in the Elixir getting started guide, it seems that it is impossible. The relevant section states:

However, we can match on the rest of the binary modifier:

iex> <<0, 1, x :: binary>> = <<0, 1, 2, 3>>
<<0, 1, 2, 3>>
iex> x
<<2, 3>>

The pattern above only works if the binary is at the end of <<>>. Similar results can be achieved with the string concatenation operator <>

iex> "he" <> rest = "hello"
"hello"
iex> rest
"llo"

Since Strings are binaries under the hood in Elixir, matching a suffix should be impossible for them as well.


using a more conventional definition of "pattern match":

String.match?(filename, ~r"_spec\.exs$")

Tags:

Elixir