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
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
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
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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^.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.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]), " ")
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
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.
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?
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.owaisqayum
You are absolutely right … and i think its the best way to learn. Thanks for the enlightenment, really appreciated.
owaisqayum
Thanks for the clarification, really appreciated