Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Remove all the special characters using java.util.regex

843789Dec 22 2009 — edited Dec 24 2009
Hi,

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 21 2010
Added on Dec 22 2009
15 comments
13,462 views