Remove all the special characters using java.util.regex
843789Dec 22 2009 — edited Dec 24 2009Hi,
How to remove the all the special characters in a String[] using regex, i have the following:-
public class RegExpTest {
private static String removeSplCharactersForNumber(String[] number) {
String number= null;
Matcher m = null;
Pattern p = Pattern.compile("\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\-\\{\\}\\|\\;\\\\\\'////\\,\\.\\?\\<\\>\\[\\]");
for (int i = 0; i < number.length; i++) {
m = p.matcher(number);
if (m.find()) {
number= m.replaceAll("");
}
}
System.out.println("Final Number is:::"+number);
return number;
}
public static void main(String args[]){
String[] str = {"raghav!@#$%^&*()_+"};
RegExpTest regExpTest = new RegExpTest();
regExpTest.removeSplCharactersForNumber(str);
}
}
This code is not working and m.find() is "false", here i want the output to be raghav for the entered string array, not only that it should remove all the special characters for a entered string[]. Is there a simple way to do this to remove all the special characters for a given string[]? More importantly the "spaces" (treated as a spl. character), should be removed as well. Please do provide a solution to this.
Thanks