Skip to Main Content

Java Programming

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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Regular expression: check for the presence of special characters.

807589Oct 24 2008 — edited Oct 29 2008
I have the following requirement:

I need to check for the presence of the following characters in a keyword: @, #, > if any of these characters are present, then they need to be stripped off, before going further. Please let me know the regular expression to check for these characters.

Comments

807589 Oct 24 2008 — edited on Oct 24 2008
something like
String text = text.replaceAll("[@#>]", "");
807589 Oct 28 2008
I am trying to extend the same logic for the following characters:

.,‘“?!@#%^&*()-~<>[]{}\+=`©® . here is the code fragment:
Pattern kValidator = Pattern.compile("[\\.,\\‘\\“?!@#%^&*()-~<>[]{}\\+=\\`©®]");
Matcher kMatcher = kValidator.matcher(keyWord);
		
if (kMatcher.find(0)) {
keyWord = keyWord.replaceAll("[.,\\‘\\“?!@#%^&*()-~<>[]{}\\+=\\`©®]", " ");
}
I get the following error. This error is from the weblogic command window. I dont understand these special characters.
Error: 

28 Oct 2008 12:27:48 | INFO  | SearchController   | Exception while fetching search results in controller:Unclosed character class near index
39
[\.,\&#915;Çÿ\&#915;Ç£?!@#%^&*()-~<>[]{}\+=\`&#9516;&#8976;&#9516;«]
                                       ^
java.util.regex.PatternSyntaxException: Unclosed character class near index 39
[\.,\&#915;Çÿ\&#915;Ç£?!@#%^&*()-~<>[]{}\+=\`&#9516;&#8976;&#9516;«]
                                       ^
        at java.util.regex.Pattern.error(Pattern.java:1650)
        at java.util.regex.Pattern.clazz(Pattern.java:2199)
        at java.util.regex.Pattern.sequence(Pattern.java:1727)
        at java.util.regex.Pattern.expr(Pattern.java:1687)
        at java.util.regex.Pattern.compile(Pattern.java:1397)
        at java.util.regex.Pattern.<init>(Pattern.java:1124)
        at java.util.regex.Pattern.compile(Pattern.java:817)
807589 Oct 28 2008 — edited on Oct 28 2008
The cause of your problem is that you didn't save the file in the right charset encoding.

But there are much more 'special characters' than only those you have shown. Can't you just do it the reverse way and define a group of allowed characters and use regex to strip out the other characters using ^ ?

E.g.
string.replaceAll("[^\\w\\s]", "");
which allows aplhanumeric (word) characters (incl underscore) and spaces only.
807589 Oct 29 2008
To avoid the encoding problem, you can use Unicode escapes for any characters that are outside the seven-bit ASCII range. However, you also need to escape hyphens and square brackets within a character class--that's what the error message was about. Ironically, those characters that you did escape didn't need it, because they lose their special meanings in character classes.
keyWord = keyWord.replaceAll("[.,\u2018\u201C?!@#%^&*()\\-~<>\\[\\]{}+=`\u00A9\u00AE]", "");
But that's just FYI; if BalusC's suggestion will work for you, you should definitely do it that way. Whichever way you go, there's no need to call find() before doing the replacement; if there are no matches, replaceAll() will simply return the original string.
807589 Oct 29 2008
Hey, the solution worked. Thanks a bunch!!!
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 26 2008
Added on Oct 24 2008
5 comments
533 views