简体   繁体   中英

Remove a set of special character from a String

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("[ \\]\\[.\\\\,+?-]+", "");
  • You will need to include all other unwanted characters such as [, ], +, ?, | etc in your character class.
  • It ie better to use a quantifier + here for better performance.
  • Remember to place an unescaped hyphen at first or last place in a character class.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM