owaisqayum
I have a sample string
sentence = "Hello, world ... 123 *** ^%&*())^% %%:>"
From this string, I want to only keep the integers, characters, and spaces. For that, I have used String. replace like this
a = String.replace(sentence, ~r"[:_!@#$%^&*:|,./]", " ")
Here it’s replacing the second argument with the third one that is an empty space.
Is there any way that we can use String.replace and focus on what to keep rather than what to throw away.
Thanks
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
:warning: Security advisory: Decimal DoS vulnerability
A vulnerability has been published for decimal where very large exponents can cau...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 9 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
owaisqayum
Thanks for the clarification, really appreciated
owaisqayum
You are absolutely right … and i think its the best way to learn. Thanks for the enlightenment, really appreciated.
axelson
Not super important but for clarity I’d like to point out that the caret character
^is not the pin operator in this case, it is acting as a regex negation operator.wolf4earth
I know that test.
Let me guess, you’re doing the Word Count exercise on exercism, right? I’m a mentor there, so I recognized this immediately.
Well, then let me enlighten you, what you’re looking for - especially for the latter special case tests - are Unicode Categories. I could of course give you the working regex but then why do the exercise at all?
Eiji
By default I’m going with really simple examples to not confuse people too much by providing more complex code samples.
If you are interested check what I have created here: regex101: build, test, and debug regex
I don’t know all edge-cases, but this one should work for you. If you have more requirements or have other questions about regular expressions please create a separate topic and feel free to ping me.
kip
The regexs here aren’t going to work well with Unicode input, nor match digits that aren’t the indo-arabic set of
0..9. And likely have issues with signs and exponents. None of these may matter in your use case of course. But if they do, then my ex_cldr_numbers library can help. UseCldr.Numbers.Parser.scan/2and then filter as you wish.Examples
owaisqayum
Thank you for your valuable responses and I found out that just using the pin operator can reverse the whole scenario. For example, if we have a test case
String.replace(b, ~r([^a-z 0-9]), " ")
Eiji
@owaisqayum: You can use
^(negation character) like:Here is my another post with many helpful examples:
On pages for testing regular expressions you may find some references. For example here:
at the bottom of page you can search by
notand you would have few interesting suggestions.al2o3cr
In this case, the negation option for a character class will do what you want -
~r([^A-Z])will match anything that isn’tA-Z(indicated by the leading^.