I need to remove a set of special characters (ie, []'?!+-.,
) 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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.