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
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
I’m posting this in response to Jose’s recent tweet (Cr. link) :
People are sleeping on Elixir for a coding harness:
Hot-code swappi...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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