You need to double-escape the \ character: "[^a-zA-Z0-9\\s]"
Java will interpret \s as a Java String escape character, which is indeed an invalid Java escape. By writing \\, you escape the \ character, essentially sending a single \ character to the regex. This \ then becomes part of the regex escape character \s.
You need to double-escape the \ character: "[^a-zA-Z0-9\\s]"
Java will interpret \s as a Java String escape character, which is indeed an invalid Java escape. By writing \\, you escape the \ character, essentially sending a single \ character to the regex. This \ then becomes part of the regex escape character \s.
You need to escape the \ so that the regular expression recognizes \s :
paragraphInformation = paragraphInformation.replaceAll("[^a-zA-Z0-9\\s]", "");
java - How to remove any non-alphanumeric characters? - Stack Overflow
java - Regex to remove all non-Alphanumeric characters with universal language support? - Stack Overflow
Trying to remove non-alphabetical characters from inputted string need help pls
How to remove non-alphanumeric characters in Java?
Use the not operator ^:
[^a-zA-Z0-9.\-;]+
This means "match what is not these characters". So:
StringUtils.replacePattern(input, "[^a-zA-Z0-9.\\-;]+", "");
Don't forget to properly escape the characters that need escaping: you need to use two backslashes \\ because your regex is a Java string.
You could negate your expression;
\p{Alnum}
By placing it in a negative character class:
[^\p{Alnum}]
That will match any non-alpha numeric characters, you could then replace those with "". if you wanted to allow additional characters you can just append them to the character class, e.g.:
[^\p{Alnum}\s]
will not match white space characters (\s).
If you where to replace
[^\p{Alnum}.;-]
with "", these characters will also be allowed: ., ; or -.
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 ~]
Hey guys! For class I'm trying to make a program to remove all non-alpha characters from a inputted string. Can you tell me why this isn't working?
For some reason when I input for example "testing test ! bla -o-" it outputs "testingtest!bla-o"
#include <iostream>using namespace std;int main() {
string inputStr;getline(cin, inputStr);for (int i = 0; i < inputStr.size(); i++){if (!isalpha(inputStr[i])){inputStr.replace(i, 1, "");}}cout << inputStr;return 0;}
Use ^ (matches at the beginning of the string) and $ (matches at the end) anchors:
s = s.replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", "");
Use:
s.replaceAll("^[^\\p{L}^\\p{N}\\s%]+|[^\\p{L}^\\p{N}\\s%]+$", "")
Instead of:
s.replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", "")
Where p{L} is any kind of letter from any language.
And p{N}is any kind of numeric character in any script.
For use in Latin-based scripts, when non-English languages are needed, like Spanish, for instance: éstas, apuntó; will in the latter become; stas and apunt. The former also works on non-Latin based languages.
For all Indo-European Languages, add p{Mn} for Arabic and Hebrew vowels:
s.replaceAll("^[^\\p{L}^\\p{N}^\\p{Mn}\\s%]+|[^\\p{L}^\\p{N}^\\p{Mn}\\s%]+$", "")
For Dravidian languages, the vowels may surround the consonant - as opposed to Semitic languages where they are "within" the character - like ಾ. For this use p{Me} instead. For all languages use:
s.replaceAll("^[^\\p{L}^\\p{N}^\\p{M}\\s%]+|[^\\p{L}^\\p{N}^\\p{M}\\s%]+$", "")
See regex tutorial for a list of Unicode categories