开发者

Regex for java's String.matches method?

Basically my question is this, why is:

String word = "unauthenticated";
word.matches("[a-z]");

returning false? (Developed in java1.6)

Basicall开发者_开发百科y I want to see if a string passed to me has alpha chars in it.


The String.matches() function matches your regular expression against the whole string (as if your regex had ^ at the start and $ at the end). If you want to search for a regular expression somewhere within a string, use Matcher.find().

The correct method depends on what you want to do:

  1. Check to see whether your input string consists entirely of alphabetic characters (String.matches() with [a-z]+)
  2. Check to see whether your input string contains any alphabetic character (and perhaps some others) (Matcher.find() with [a-z])


Your code is checking to see if the word matches one character. What you want to check is if the word matches any number of alphabetic characters like the following:

word.matches("[a-z]+");


with [a-z] you math for ONE character.

What you’re probably looking for is [a-z]*

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜