owaisqayum

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

Showing Posts 1 to 9

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 ^.

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.

owaisqayum

owaisqayum OP

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

Assertion with == failed
     code:  assert WordCount.count("co-operative") == expected
     left:  %{"co" => 1, "operative" => 1}
     right: %{"co-operative" => 1}
     stacktrace:
       test/word_count_test.exs:35: (test)```

here the word is co-operative and when I use the pin operator, it opts the "-" between the co-operative and which should be read as a single word. What is the better approach in this case?

Now the regex expression looks like 

String.replace(b, ~r([^a-z 0-9]), " ")

Thanks
kip

kip

ex_cldr Core Team

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. Use Cldr.Numbers.Parser.scan/2 and then filter as you wish.

Examples

# Scan a string in a locale-sensitive fashion and extract numbers
iex> Cldr.Number.Parser.scan "Hello, world ... 123 *** ^%&*()-72.5)^% %%: 123.00>"
["Hello, world ... ", 123, " *** ^%&*()", -72.5, ")^% %%: ", 123.0, ">"]

# Scan a string in a locale-sensitive fashion and extract numbers - in the "de" locale
# Note the use of the "," as the decimal separator
iex> Cldr.Number.Parser.scan "Hello, world ... 123 *** ^%&*()-72.5)^% %%: 123,00>", locale: "de"
["Hello, world ... ", 123, " *** ^%&*()", -72.5, ")^% %%: ", 123.0, ">"]
Eiji

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. :wink:

wolf4earth

wolf4earth

I know that test. :grinning_face_with_smiling_eyes:

Let me guess, you’re doing the Word Count exercise on exercism, right? I’m a mentor there, so I recognized this immediately. :wink:

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? :winking_face_with_tongue:

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.

owaisqayum

owaisqayum OP

You are absolutely right … and i think its the best way to learn. Thanks for the enlightenment, really appreciated.

owaisqayum

owaisqayum OP

Thanks for the clarification, really appreciated

— All posts loaded —

Where Next? Top

Trending in Discussions Top

sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
SyntaxSorcerer
This is the start of my Elixir learning journal/journey. I plan to share my journey in this thread and am open to all feedback, construct...
New
bartblast
This is where the projects in the Hologram Showcase get posted: apps built with Hologram and libraries made for it. New ones go up usuall...
New
karolina.curiosum
Hi everyone! This is our fourth edition. Last year, 1,000+ developers took part. The more of you who answer, the more useful the results...
New
mandar1045
I’ve been fixing race condition / crash bugs in an open source org, for a few months now and almost all of them had the same problem. The...
New
fredwu
Over the years I’ve tried dozens of time tracking and invoicing apps, and Harvest was the only one I liked. Primarily due to its relative...
New
New

Other Trending Topics Top

GenericJam
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...
261 13183 112
New
mudasobwa
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews