Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Special characters in Java  RSS feed

 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I validate a string to verify whether it can have only Alphabets + numbers + special characters and not only numbers and not only special characters..

I tried with the regec pattern. I am able to validate for only numbers but not for others. anyone advise
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us which regex you were using.
 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.regex.pattern and declared something like this..
private static final Pattern objRegexAlphanumericPattern = Pattern.compile("^[ A-Za-z0-9 ]+$");
 
Henry Wong
author
Sheriff
Posts: 22837
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The regex you are using only specifies letters and numbers -- which leads to the next question.... what "special" characters are you trying to specify.

Henry
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to check with the Pattern class or the Java™ Tutorials, but I think that will match the 26 letters (upper-case and lower-case) used in English and ten digits and ordinary spaces, but no punctuation or accented letters, for the whole of a line from the ^ to the $.

And Henry has beaten me to it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!