Reputation: 1874
I need to remove a set of special characters (i.e., []'?!+-.,
) from a string.
The typical exclusive solution replaceAll("[^a-zA-Z0-9]", "")
is not ok, because I just need to remove those characters, and also save text containing greek characters. For example:
public static void test_regex() {
ArrayList<String> tests = new ArrayList<>();
tests.add("------.");
tests.add("+[---].");
tests.add("------?");
tests.add("---]〛");
tests.add("A++[---].");
tests.add("AV[---]S");
for (String s : tests) {
String becomes = s.replaceAll("[.-\\\\,]", "");
System.out.println(s + " becomes <" + becomes + ">");
}
}
should give as a output
------. becomes <>
+[---]. becomes <>
------? becomes <>
---]〛 becomes <>
A++[---]. becomes <A>
AV[---] becomes <AV>
But I cant. I succeed to remove .
and -
with [.-]
, but then I add \\[
and breaks everything (also tried \\\\[
or \\\\\\[
) , also the .
which before worked is not working anymore.
Which way to escape each one of these characters?
Upvotes: 0
Views: 108
Reputation: 786359
You can use following regex replacement to get rid of all unwanted characters:
String becomes = s.replaceAll("[ \\]\\[.\\\\,+?-]+", "");
[, ], +, ?, |
etc in your character class. +
here for better performance.Upvotes: 4