Replace [^a-zA-Z0-9 -] with an empty string.

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");
Answer from Amarghosh on Stack Overflow
Discussions

javascript - Remove not alphanumeric characters from string
The following is the/a correct regex to strip non-alphanumeric chars from an input string: ... Note that \W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.: More on stackoverflow.com
🌐 stackoverflow.com
Using regex to remove non-alphabetical characters in a string.
if a pattern matches things you want to keep, like "all letters": [a-zA-Z] you can invert it with "^" to make it "not" those things: [^a-zA-Z] also, there are (depending on which language and which regex variant you're using) shortcuts like \s = a space (or tab or newline or...) \S = not a space \w = a word type character (i.e. a-z A-Z 0-9 etc) \W = not a word type character \d = a number \D = not a number More on reddit.com
🌐 r/learnprogramming
7
1
July 24, 2021
java - Regex to remove all non-Alphanumeric characters with universal language support? - Stack Overflow
I would like to use Pattern's compile method to do this. Such as String text = "Where? What is that, an animal? No! It is a plane."; Pattern p = new Pattern("*some regex here*"); String delim = p. More on stackoverflow.com
🌐 stackoverflow.com
Regex to remove all non alpha-numeric and replace spaces with +
I'm looking to use regex to try remove all non alpha-numeric characters from a string and replace spaces with a + All I want to permit is basically alphabetical words A-Z and + This is specifical... More on stackoverflow.com
🌐 stackoverflow.com
🌐
gosamples
gosamples.dev › tutorials › remove non alphanumeric characters from a string in go
🧽 Remove non-alphanumeric characters from a string in Go
May 19, 2022 - This is a classic example of removing non-alphanumeric characters from a string. First, we compile our regular expression that matches any character other than an English letter, number, or space. Then, we use the Regexp.ReplaceAllString() method to replace the matched non-alphanumeric characters ...
🌐
Regex101
regex101.com › r › jI5hK6 › 1
regex101: Remove Non-Alphanumeric Characters
Online regex tester and debugger. Test, explain, benchmark, and generate code for PCRE2, JavaScript, Python, Go, Java, .NET, and Rust.
🌐
Gopher Coding
gophercoding.com › remove-non-alphanumeric-chars
Remove All Non-Alphanumeric Characters · Gopher Coding
April 2, 2023 - <p>We often need to remove symbols and special characters from the strings we&rsquo;re using (<em>especially with currency!</em>). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in &ldquo;$1,000&rdquo; you can turn it into &ldquo;1000&rdquo;.</p> <p>We use the <code>regexp</code> package to do this, first building a regex with <code>.Compile()</code> then running the string through that regex with <code>.ReplaceAllString()</code>. Finally with display and compare both strings.</p>
🌐
Flexiple
flexiple.com › python › remove-non-alphanumeric-characters-python
Removing Non-Alphanumeric Characters in Python - Flexiple
March 21, 2024 - It includes only those characters in clean_text that are alphanumeric. The result is a string free of spaces, punctuation, and special characters. This approach is particularly useful for basic text-cleaning tasks in data processing and text analysis applications. Using regular expressions in Python offers a powerful and flexible approach to removing non-alphanumeric characters from strings.
🌐
Melvin George
melvingeorge.me › blog › remove-all-non-alphanumeric-characters-string-javascript
How to remove all the non-alphanumeric characters from a string using JavaScript? | MELVIN GEORGE
June 23, 2021 - // a string const str = "#HelloWorld123$%"; // regex expression to match all // non-alphanumeric characters in string const regex = /[^A-Za-z0-9]/g; ... This will remove all the non-alphanumeric characters from the string and replaces it with ...
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-non-alphanumeric-characters-from-string
Remove all non-alphanumeric Characters from a String in JS | bobbyhadz
The removeNonAlphanumeric() function takes a string as a parameter and removes all non-alphanumeric characters from the string. You can also use the \W special character to shorten your regex and remove all non-alphanumeric characters from a string.
🌐
Medium
medium.com › @shamzaibrahim7 › three-techniques-to-remove-non-alphanumeric-characters-for-palindrome-problem-9ab7a78d8490
Three Techniques to Remove Non-Alphanumeric Characters for Palindrome Problem | by Syed Hamza | Medium
March 27, 2023 - To use regular expressions to remove non-alphanumeric characters, we can define a regular expression pattern that matches all non-alphanumeric characters, and then use the re.sub() function to replace all matches with an empty string.
🌐
Techie Delight
techiedelight.com › home › java › remove non-alphanumeric characters from a string in c#
Remove non-alphanumeric characters from a string in C# | Techie Delight
3 weeks ago - We can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string. Replace the regular expression [^a-zA-Z0-9] with [^a-zA-Z0-9 _] to allow spaces and underscore character.
🌐
Dot Net Office
dotnetoffice.com › 2022 › 08 › remove-non-alphanumeric-characters-from.html
Remove non-alphanumeric characters from a string in C# - Dot Net Office
August 10, 2022 - In this article, we will see how to Remove non-alphanumeric characters from a string in C# 1. Using Regular Expression · private static void RemoveNoAlphaChar() { string str = "dotnetoffice16@gmail.com"; str = Regex.Replace(str, "[^a-zA-Z0-9]", String.Empty); Console.WriteLine(str); Console.ReadLine(); } Code language: C# (cs) the output will be like below, which doesn't contain @ and .(dot) dotnetoffice16gmailcom ·
🌐
Reddit
reddit.com › r/learnprogramming › using regex to remove non-alphabetical characters in a string.
r/learnprogramming on Reddit: Using regex to remove non-alphabetical characters in a string.
July 24, 2021 -

Hi, I am trying to remove non-alphabetical characters from the following string.

let string = "Anne, I vote more cars race Rome-to-Vienna"

I can get the result I am looking for with the following...

let newString = string.toLowerCase().split('').filter(el => el !== ' ' && el !== '-' && el !== ',').join('')

However I would prefer to use a much shorter piece of code using regex. I have gone over a few regex examples and tutorials but it still kind of confused me. Does anybody know the simplest way to remove the unwanted characters from the above string?

Thanks!!!

Top answer
1 of 2
4

The Java Pattern class, which is Java's implementation of regex, supports Unicode Categories, e.g. \p{Lu}. Since you want alphanumeric, that would be Categories L (Letter) and N (Number).

Since your example shows you also want to keep spaces, you need to include that. Let's use the Predefined Character Class \s, so you also get to keep newlines and tabs.

To find anything but the specified characters, use a Negation Character Class: [^abc]

All-in-all, that means [^\s\p{L}\p{N}]:

String output = input.replaceAll("[^\\s\\p{L}\\p{N}]+", "");
Where What is that an animal No It is a plane
Dónde Qué es eso un animal No Es un avión
Onde O que é isso um animal Não É um avião

Or see regex101.com for demo.


Of course, there are multiple ways to do it.

You could alternatively use the POSIX Character Class \p{Alnum}, and then enable UNICODE_CHARACTER_CLASS, using (?U).

String output = input.replaceAll("(?U)[^\\s\\p{Alnum}]+", "");
Where What is that an animal No It is a plane
Dónde Qué es eso un animal No Es un avión
Onde O que é isso um animal Não É um avião

Now, if you didn't want spaces, that could be simplified by using \P{xx} instead:

String output = input.replaceAll("(?U)\\P{Alnum}+", "");
WhereWhatisthatananimalNoItisaplane
DóndeQuéesesounanimalNoEsunavión
OndeOqueéissoumanimalNãoÉumavião
2 of 2
1

I am not an expert in all the languages of the world, however, your requirements could be met by doing this on a language specific basis:

Regex rgx = new Regex("[^a-zA-Z0-9 <put language specific characters to preserve here>]");
str = rgx.Replace(str, "");

I speak English and Korean, and can tell you that punctuation in Korean is identical to that used in English. As indicated above, you can add characters that should be preserved and not considered punctuation for a particular language. For example, let's say the tilde should not be considered punctuation. Then use the regex:

[^a-zA-Z0-9 ~]
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - Examples of non-alphanumeric characters: ‘!’, ‘{‘, ‘&’ ... Here the symbols ‘!’ and ‘@’ are non-alphanumeric, so we removed them. ... Here the symbols ‘_’, ‘{’, ‘}’ and ‘@’ are non-alphanumeric, so we removed them. ... Here, there’s no need to remove any characters because all of them are alphanumeric.
🌐
Uptimia
uptimia.com › home › questions › how to remove non-alphanumeric characters from a string in php?
How To Remove Non-Alphanumeric Characters From A String in PHP?
November 11, 2024 - The preg_replace function in PHP uses regular expressions to remove non-alphanumeric characters from a string. The regex pattern "[^A-Za-z0-9 ]" matches any character that is not a letter, number, or space.
🌐
PyTutorial
pytutorial.com › remove-non-alphanumeric-characters-in-python
PyTutorial | Remove Non-Alphanumeric Characters in Python
February 21, 2026 - We can use it to remove all non-alphanumeric characters. The pattern r'[^A-Za-z0-9]' matches any character not in the alphanumeric set. The ^ inside square brackets means "not". We replace matches with an empty string. import re def ...
🌐
Baeldung
baeldung.com › home › kotlin › kotlin strings › removing all non-alphanumeric characters in kotlin
Removing All Non-Alphanumeric Characters in Kotlin | Baeldung on Kotlin
March 19, 2024 - In order to remove all non-alphanumeric characters from a string, we can use regular expressions along with the replace() extension function. To be more specific, the following regex matches with all non-alphanumeric characters: