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
Post a Comment