• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem using Pattern n Matcher

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have a problem where i need to put a validation on the text box that it contains only alphabets and numbers. For this i m using pattern and matcher.
I want this validation to be applied to the java file only and not on jsp page page.
My code looks like this

String securityAnswer="afj$%";
Pattern pattern = Pattern.compile("([a-z A-Z 0-9])");
Matcher matcher = pattern.matcher(securityAnswer);
boolean result = matcher.find();

How can i make sure that the text contains only a-z n 0-9 only. becoz whenever i enter any normal text with special characters it accepts on clicking the save button.
I want to write a pattern that returns true only if the securityAnswer string contains alphabets and numbers and not anything else.

Any help will be greatly appreciated.

Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The find() method is used to find() a match within the string. If you want to force the match to the whole string then you can use the matches() method instead.

BTW, you will also need to change your regex, as the way it is written, it will only match one letter.

Henry
 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a code snippet would be really helpful.
i m using

boolean matchFound = pattern.matches("(?i)[a-z 0-9]", "asfSD98");

but it is returning false.

if i use

boolean matchFound = pattern.matches("(?i)[a-z]", "asfSD");

then it returns true


Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are going to use regexes, you should at least get up to speed on them. It's well worth it to do so. See java.util.regex.Pattern class for more info.

if i use

boolean matchFound = pattern.matches("(?i)[a-z]", "asfSD");

then it returns true



I highly doubt that. Try it again, as there is no way this can return true.

Henry
 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey got the solution

I did some minor change in the code as follows:

Pattern pattern = Pattern.compile("([^a-z A-Z 0-9])");
String original = "%asfSD!@";
Matcher match = pattern.matcher(original);



Thanks for the help!
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gurvinder Singh wrote:Hey got the solution

I did some minor change in the code as follows:

Pattern pattern = Pattern.compile("([^a-z A-Z 0-9])");
String original = "%asfSD!@";
Matcher match = pattern.matcher(original);



Thanks for the help!




Hate to break the news to you -- but you really should learn regexes, if you are going to use them. All that change does is to make sure that the first character is a number, letter, or space. You can still have other characters, as long as it is not the first character (BTW, I am assuming that you are still using find()).

Henry
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not letter, number, or space?
 
Gurvinder Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Henry

I v tested this in number of ways by taking the input string as
the matcher.find() is returning the following results:

"%)Aas(fSD" returns true // it means the input string contains other than characters n digits
"Aas(fSD" returns true // it means the input string contains other than characters n digits
"AasfSD" returns false // it means the input string contains characters n digits
"asfSD" returns false // it means the input string contains characters n digits
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Not letter, number, or space?



Yea... sorry. Had a brain fart. Never mind.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic