donnadulcinea
donnadulcinea

Reputation: 1874

Remove a set of special character from a String

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

Answers (1)

anubhava
anubhava

Reputation: 786359

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.

Upvotes: 4

Related Questions