Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check whether the input string contains numbers,comma,colon

 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
In my project i want to check whether the user's input string contains numbers,commma,colon,double and single quotes and also any special characters
Iam getting the user input as a string.
Can anyone tell me how to check for this?

Thanks a lot.
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer Regex package along with tutorial

The pattern expression would be something like this : [^a-zA-Z]

i.e. any character other than a to z and A to Z
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Thanks a lot for your reply.
If you dont mind can you explain it with an example or sample code.
Thanks a lot
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General (the best place to ask about string handling questions).
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
If i java.util.regex class i need to give the pattern where the special characters will occur.Am i right?
But in my case, the input string is user defined one.
In that,at any place the special characters and numbers will occur.
How to check those?
Thanks in advance
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I can repharse your requirement, it will be " To check whether the user input contains anything else other than a to z or A to Z characters"

Here anything else are numbers,colon,double,special characters etc

So you create a pattern to get all the matches that does not contain a-z or A-Z. After you compile the pattern, the matches should return only those values that do not contain a-z / A-Z which will be special chars, numbers, colons etc.

So I suggested the pattern would be [^a-zA-Z]
^ symbol is not negation.

For concrete examples refer tutorial link / API reference
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santhana Lakshmi.S:
hi
If i java.util.regex class i need to give the pattern where the special characters will occur.Am i right?
But in my case, the input string is user defined one.
In that,at any place the special characters and numbers will occur.
How to check those?
Thanks in advance


Regular expression syntax allows you to specify the characters without worrying about their exact placement in the string. For instance the pattern [a-z]* matches zero or more lower case letters. It doesn't matter what order the letters are in.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do a switch statement...

like:



then you could,


[ July 17, 2006: Message edited by: Justin Fox ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic