11

I need to remove some specific "special" characters and replace them with empty string if they show up. I am currently having a problem with the regex, probably with the Java escaping. I can't put them all together, it just doesn't work, I tried a lot! T_T

Currently I am doing it one by one which is kinda silly, but for now at least it works, like that :

public static String filterSpecialCharacters(String string) {
    string = string.replaceAll("-", "");
    string = string.replaceAll("\\[", "");
    string = string.replaceAll("\\]", "");
    string = string.replaceAll("\\^", "");
    string = string.replaceAll("/", "");
    string = string.replaceAll(",", "");
    string = string.replaceAll("'", "");
    string = string.replaceAll("\\*", "");
    string = string.replaceAll(":", "");
    string = string.replaceAll("\\.", "");
    string = string.replaceAll("!", "");
    string = string.replaceAll(">", "");
    string = string.replaceAll("<", "");
    string = string.replaceAll("~", "");
    string = string.replaceAll("@", "");
    string = string.replaceAll("#", "");
    string = string.replaceAll("$", "");
    string = string.replaceAll("%", "");
    string = string.replaceAll("\\+", "");
    string = string.replaceAll("=", "");
    string = string.replaceAll("\\?", "");
    string = string.replaceAll("|", "");
    string = string.replaceAll("\"", "");
    string = string.replaceAll("\\\\", "");
    string = string.replaceAll("\\)", "");
    string = string.replaceAll("\\(", "");
    return string;
}

Those are all the character I need to remove:

- [ ] ^ / , ' * : . ! > < ~ @ # $ % + = ? | " \ ) (

I am clearly missing something, I can't figure out how to put it all in one line. Help?

3
  • Put inside a character class: [<>!~@^()....] Commented Mar 20, 2016 at 10:10
  • stackoverflow.com/questions/16887607/… Commented Mar 20, 2016 at 10:54
  • It looks very much like string = string.replaceAll("\\p{Punct}+", ""). See Java regex reference: \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~. However, I see you have a bit fewer symbols defined in your class. Commented Feb 9, 2017 at 7:32

2 Answers 2

11

Your code does not work in fact because .replaceAll("$", "") replaces an end of string with empty string. To replace a literal $, you need to escape it. Same issue is with the pipe symbol removal.

All you need to do is to put the characters you need to replace into a character class and apply the + quantifier for better performance, like this:

string = string.replaceAll("[-\\[\\]^/,'*:.!><~@#$%+=?|\"\\\\()]+", "");

Note that inside a character class, most "special regex metacharacters" lose their special status, you only have to escape [, ], \, a hyphen (if it is not at the start/end of the character class), and a ^ (if it is the first symbol in the "positive" character class).

DEMO:

String s = "-[]^/,'*:.!><~@#$%+=?|\"\\()TEXT";
s = s.replaceAll("[-\\[\\]^/,'*:.!><~@#$%+=?|\"\\\\()]+", "");
System.out.println(s); // => TEXT
Sign up to request clarification or add additional context in comments.

4 Comments

The pipe (|) also needed escaping in his original code (but not in yours).
Yes, added a note on that to the answer.
I meant to say he should have escaped it, but didn't.
It looks very much like string = string.replaceAll("\\p{Punct}+", ""). See Java regex reference: \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~. However, I see you have a bit fewer symbols defined in your class.
6

Use these codes

String REGEX = "YOUR_REGEX";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(yourString);
yourString = m.replaceAll("");

UPDATE :

Your REGEX looks something like

String REGEX = "-|\\[|\\]|\\^|\\/|,|'|\\*|\\:|\\.|!|>|<|\\~|@|#|\\$|%|\\+|=\\?|\\||\\\\|\\\\\\\\|\\)|\\(";

SAPMLE :

String yourString = "#My (name) -is @someth\ing"";
//Use Above codes
Log.d("yourString",yourString);

OUTPUT

enter image description here

8 Comments

How does this answer the question?
first of all thank you for taking your time and trying to help me. appreciate it a lot ! 2ndly, -> String REGEX = "\"[^/,'*:<>!~@#$%^&()+=?()\\\"|!\[#$-]+\""; Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(string); string = m.replaceAll(""); - this doesn't work.
can you post your current string.
Are you sure that you are using valid regex?
@ShreeKrishna I did found this helpful! it's certainly another good way to achieve my desired goal, thanks a lot for your time in providing an answer! appreciate it a lot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.