Fl4m3Ph03n1x

Fl4m3Ph03n1x

Bitstring pattern matching end of string

Background

We have a simple string of variable size and we want the final 4 characters of the string. Our goal is to achieve this via pattern matching. No special reason, just trying to learn.

Code

This is what we have tried thus far:

<< _, ext :: bytes-4 >> = "my_file_name.exs"
<< _, ".", ext :: bytes-3 >> = "my_file_name.exs"

Trying both lines of code in IEx fails.

Questions

  1. Is it possible to pattern match a bitstring at the end of the string? (we know it works at the start)
  2. Are we doing something wrong? If so, what?

Marked As Solved

hauleth

hauleth

No, not exactly. If you know how many last characters you want to match you can hack it though:

str = "my_file_name.exs"
len = byte_size(str)
<<_::binary-size(len - 4), ?., ext::binary>> = str

But if you want extension, which is can be more than 3 characters (we aren’t using DOS anymore), then Path.extname/1 is the way to go.

Also Liked

LostKobrakai

LostKobrakai

Only way to do it with pattern matching is reversing, which in itself might not be trivial. For your usecase there’s Path.extname though.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement