Android Java - String.replaceAll to replace specific characters (regular expression)

I need to remove some special “special” characters and replace them with an empty string if they appear. I am currently having a problem with regex, possibly with Java escaping. I can’t bring them all together, it just doesn’t work, I tried a lot! T_T

I am currently doing this one by one, which is a bit silly, but at the moment, at least it works, for example:

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; } 

These are all the characters I need to remove:

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

There is clearly something missing for me, I can’t understand how to do all this on one line. Help?

+7
java android regex
source share
2 answers

Your code does not work because .replaceAll("$", "") replaces the end of the line with an empty line. To replace the literal $ , you need to avoid it. The same problem is associated with deleting the pipe symbol.

All you have to do is put the characters you need to replace in the character class and apply quantifier + to improve performance, for example:

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

Note that inside the character class, most of the "special regular expression metacharacters" lose their special status, you only need to run the [ , ] , \ , hyphen (if it is not at the beginning / end of the character class) and a ^ (if this is the first character in a "positive" character class).

DEMO :

 String s = "-[]^/,'*:.!>< ~@ #$%+=?|\"\\()TEXT"; s = s.replaceAll("[-\\[\\]^/,'*:.!>< ~@ #$%+=?|\"\\\\()]+", ""); System.out.println(s); // => TEXT 
+6
source share

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

+2
source share

All Articles