Use [^A-Za-z0-9].
Note: removed the space since that is not typically considered alphanumeric.
Answer from Mirek Pluta on Stack OverflowUse [^A-Za-z0-9].
Note: removed the space since that is not typically considered alphanumeric.
Try
return value.replaceAll("[^A-Za-z0-9]", "");
or
return value.replaceAll("[\\W]|_", "");
Java regex: check if word has non alphanumeric characters - Stack Overflow
java - Regex to remove all non-Alphanumeric characters with universal language support? - Stack Overflow
java - Regex for checking if a string is strictly alphanumeric - Stack Overflow
removing all non-letter characters from a string? ((using regex))
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
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 ~]
Considering you want to check for ASCII Alphanumeric characters, Try this:
"^[a-zA-Z0-9]*$". Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false.
public boolean isAlphaNumeric(String s){
String pattern= "^[a-zA-Z0-9]*$";
return s.matches(pattern);
}
If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html
In order to be unicode compatible:
^[\pL\pN]+$
where
\pL stands for any letter
\pN stands for any number