This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Special characters in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Special characters in Java" Watch "Special characters in Java" New topic
Author

Special characters in Java

rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
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

Joined: Oct 13, 2005
Posts: 38075
    
  22
Please tell us which regex you were using.
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
java.util.regex.pattern and declared something like this..
private static final Pattern objRegexAlphanumericPattern = Pattern.compile("^[ A-Za-z0-9 ]+$");
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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
 
jQuery in Action, 2nd edition
 
subject: Special characters in Java
 
Similar Threads
How to make text field to accept only alphabets.
How to make text field to accept only alphabets
how to make text field to accept only alphabets
connecting characters
Regular Expression in ruby