I have this small piece of code
String[] words = {"{apf","hum_","dkoe","12f"};
for(String s:words)
{
if(s.matches("[a-z]"))
{
System.out.println(s);
}
}
Supposed to print
dkoe
but it prints nothing!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Welcome to Java’s misnamed
.matches()method… It tries and matches ALL the input. Unfortunately, other languages have followed suit 🙁If you want to see if the regex matches an input text, use a
Pattern, aMatcherand the.find()method of the matcher:If what you want is indeed to see if an input only has lowercase letters, you can use
.matches(), but you need to match one or more characters: append a+to your character class, as in[a-z]+. Or use^[a-z]+$and.find().