3

I have to validate a String to check if it contains a special character or not. This string may contain any number and words ( including unicode, ie: à, â,ô .. ) but should not accept any special characters ( ie: !,@,#,%,^ ... )

Sorry for my bad English.

Aashray
  • 2,753
  • 16
  • 22
user2090487
  • 107
  • 1
  • 3
  • 7

2 Answers2

2

This character class

[\p{L}\p{No}\p{Space}]

will include all characters which Unicode declares as either "letters", "numbers", or "whitespace characters". If you want to match a string against such a character class, you would write the following:

input.matches("[\\p{L}\\p{No}\\p{Space}]+")

For future reference, I have extracted all this information from the java.util.Pattern class. You should refer to that page for all your future interest in the Java regular expressions.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

You can try [\\p{L}\\s]+. as example. This will remove special characters.

   Pattern p=Pattern.compile("[\\p{L}\\s]+");
    Matcher m=p.matcher("hissd@");
    if(m.find()){
        System.out.println(m.group(0));//
    }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115