Use [^A-Za-z0-9].

Note: removed the space since that is not typically considered alphanumeric.

Answer from Mirek Pluta on Stack Overflow
Discussions

Java regex: check if word has non alphanumeric characters - Stack Overflow
I think a simple regex could not just solve that. Lets imagine it is english :) 2011-03-31T21:08:01.267Z+00:00 ... Save this answer. ... Show activity on this post. It's 2016 or later and you should think about international strings from other alphabets than just Latin. The frequently cited [^a-zA-Z] will not match in that case. There are better ways in Java ... More on stackoverflow.com
🌐 stackoverflow.com
March 2, 2016
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
java - Regex for checking if a string is strictly alphanumeric - Stack Overflow
How can I check if a string contains only numbers and alphabets ie. is alphanumeric? More on stackoverflow.com
🌐 stackoverflow.com
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
🌐
Baeldung
baeldung.com › home › java › java string › check if a string contains non-alphanumeric characters
Check if a String Contains Non-Alphanumeric Characters | Baeldung
January 8, 2024 - Let’s consider a simple use case, where the application must accept only English digits and alphabet characters. To achieve this, we use regex [^a-zA-Z0-9] to identify a non-alphanumeric character:
🌐
Coderanch
coderanch.com › t › 608379 › java › java-regex-count-alphanumeric-characters
java regex to count non alphanumeric characters in a String [Solved] (Beginning Java forum at Coderanch)
For example: a1b2c3$3# should return ... appreciated. Thanks, Aditya ... You could have a Regex for a non-alphanumeric character then use find() in a loop to see how many times it comes back....
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - The string can be easily filtered using the ReGex [^a-zA-Z0-9 ]. Java · // Java program to remove non-alphanumeric // characters from a string class GFG { // Main driver method public static void main(String args[]) { // Input string String ...
🌐
javaspring
javaspring.net › blog › java-regex-check-if-word-has-non-alphanumeric-characters
Java Regex: How to Check if a Word Has Non-Alphanumeric Characters – Fixing Common Pattern Mistakes
Define a regex pattern to match any non-alphanumeric character. Use Matcher.find() to check if such a character exists in the input. The goal is to match any character that is not alphanumeric.
Find elsewhere
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › remove-non-alphanumeric-characters-java-string
Java alphanumeric patterns: How to remove non-alphanumeric characters from a Java String | alvinalexander.com
April 18, 2019 - Here's a sample Java program that shows how you can remove all characters from a Java String other than the alphanumeric characters (i.e., a-Z and 0-9). As you can see, this example program creates a String with all sorts of different characters in it, then uses the replaceAll method to strip all the characters out of the String other than the patterns a-zA-Z0-9. The code essentially deletes every other character it finds in the string, leaving only the alphanumeric characters.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
String Contains Non-Alphanumeric Characters in Java - Java Code Geeks
July 14, 2023 - We can define a regular expression pattern that matches any non-alphanumeric character and then use the Pattern and Matcher classes from the java.util.regex package to find matches in the input string.
🌐
w3resource
w3resource.com › java-exercises › re › java-re-exercise-21.php
Java - Remove all non-alphanumeric characters from a string
March 16, 2026 - Original string: Java Exercises ... Ex@^%&%(ercis*&)&es After removing all non-alphanumeric characters from the said string: Exercises ... Write a Java program to filter a string to only include letters and digits using regex replacement....
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 ~]
🌐
Quora
quora.com › What-regex-will-match-until-the-first-non-alphanumeric-character
What regex will match until the first non-alphanumeric character? - Quora
Answer (1 of 2): It depends somewhat on what you actually want to match. Do you need to include certain letters in Unicode, or just a-z? If you're just going for a-z and 0-9, you could do something like ^[a-zA-Z0-9]*, which would match "ABCD" in your example. If you want to include whitespace a...
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java array › removing all non-alphabetic characters from string array in java
Removing All Non-alphabetic Characters From String Array in Java | Baeldung
November 18, 2024 - We use the replacePatttern() method of Apache Commons Lang, which expects the input string, the regex pattern to look for, and the String to replace it with where we find the pattern.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-check-string-is-alphanumeric-or-not-using-regular-expression
How to check string is alphanumeric or not using Regular Expression - GeeksforGeeks
July 12, 2025 - Create a regular expression to check string is alphanumeric or not as mentioned below: regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$"; Where: ^ represents the starting of the string · (?=.*[a-zA-Z]) represents the alphabets from a-z, A-Z ...
🌐
Quora
quora.com › How-can-I-remove-all-non-alphanumeric-characters-from-a-string-in-Java
How to remove all non-alphanumeric characters from a string in Java - Quora
Answer: Here you go! Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), ...
🌐
RegExr
regexr.com › 3anqj
Match till non-alphanumeric
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 7 )
A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct. Backslashes within string literals in Java source code are interpreted as required by The Java™ Language Specification as either Unicode escapes (section 3.3) ...