java - Regex doesn't work in String.matches() -


i have small piece of code

string[] words = {"{apf","hum_","dkoe","12f"}; for(string s:words) {     if(s.matches("[a-z]"))     {         system.out.println(s);     } } 

supposed print

dkoe 

but prints nothing!!

welcome java's misnamed .matches() method... tries , matches input. unfortunately, other languages have followed suit :(

if want see if regex matches input text, use pattern, matcher , .find() method of matcher:

pattern p = pattern.compile("[a-z]"); matcher m = p.matcher(inputstring); if (m.find())     // match 

if want indeed see if input has lowercase letters, can use .matches(), need match 1 or more characters: append + character class, in [a-z]+. or use ^[a-z]+$ , .find().


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -