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
- Is it possible to pattern match a bitstring at the end of the string? (we know it works at the start)
- Are we doing something wrong? If so, what?
Marked As Solved
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.
5
Also Liked
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.
3
Popular in Questions
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
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
I would like to know what is the best IDE for elixir development?
New
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
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
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









