I want to remove multiple occurrences of special characters like \" \", \"-\", \"!\", \"_\" from my java string by a sing
\" \"
\"-\"
\"!\"
\"_\"
Note that \1 is a backreference to the contents matched with the first capturing group. To actually match one or more any characters from the character class, just use a + quantifier:
\1
+
[\\s!-]+
So, use
str = str.replaceAll("[\\s!-]+","_");
See IDEONE demo