henrique-marcomini-m
I have the following code:
defp has_char_in_string?(value), do: Regex.match?(~r/[^\d]/, value)
def somefun(arg) do
case has_char_in_string?(arg) do
true -> foo()
false -> bar()
end
end
And I really want to keep this regex within this module and do not externalize this logic. But I also want to use pattern matching or guards instead of a case. Is this even possible? And if it is, how?
Thanks in advance
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
No, there is no way to make this regex or even something semantically equivalent into a guardsave predicate.
peerreynders
Probably not what you are looking for:
or
al2o3cr
It’s possible, especially if the regular expression is simple - you can translate the regex into its corresponding finite state machine and represent that with pattern matching:
Here, the regex
[^\d]translates to a state machine that stays incall_baras long as each character is0-9, actually calls bar given an empty string, and calls foo otherwise.If the regex doesn’t involve backreferences or lookaheads (so it’s a theory-of-languages regular expression), it’s always possible to do this.
HOWEVER
The example above is a good example of how this approach obfuscates what should have been code like this:
I’m very curious what’s motivating the preference for pattern matching here; it’s not the right tool for the job.
hauleth
TBH I would say that Regex is overkill there. And your pattern match can be improved:
The problem with regular expressions the use backtracking engine (and PCRE is such engine) is that it can explode to
O(n^2)with some expressions (namely nested wildcard matches). And such cases have been spotted on the wild with pretty simple expressions.henrique-marcomini-m
Well I think there is no pretty solution here. Though Hauleth presented something that would work ,I now see that the best way is through a well crafted regex. Thank you all for the answers.
kip
I have a package called ex_cldr_unicode that includes guards for
These all operate on the Unicode character classes so it covers what passes for a digit in a more complete sense. It might help or give you some ideas.
Note that it works on code points since there is a limited set of underlying functions that can be used in guards.
There is another bunch of functions that might be helpful, including
Cldr.Unicode.alphanumeric?/1which will return a boolean and also uses the full Unicode definitions (not just Latin1):mudasobwa
The above might be written as:
dkuku
is there a way to use guard clauses from erlang ??
I just found this in erlang masterclass course from kent university and I’m trying to solve the problems in elixir - looks similiar as op
peerreynders
https://hexdocs.pm/elixir/guards.html
e.g.
dkuku
thanks @peerreynders - I found a workaround which probably also can be used:
(I’m using double quoted strings for that)