owaisqayum

owaisqayum

Usage of String.replace?

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

Most Liked

axelson

axelson

Scenic Core Team

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.

Eiji

Eiji

@owaisqayum: You can use ^ (negation character) like:

sentence = "Hello, world ... 123 *** ^%&*())^% %%:>"
String.replace(sentence, ~r/[^0-9a-zA-Z ]/, " ")

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 not and you would have few interesting suggestions.

A CHARACTER NOT IN THE RANGE A-Z
[^a-z] Matches any characters except those in the range a-z.

/[^a-z]+/g
Anything but a-z.

al2o3cr

al2o3cr

In this case, the negation option for a character class will do what you want - ~r([^A-Z]) will match anything that isn’t A-Z (indicated by the leading ^.

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18272 126
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

vertexbuffer
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
RisingFromAshes
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement