【发布时间】:2016-09-01 09:30:29
【问题描述】:
我正在尝试使用正则表达式检查密码是否为字母数字 但我没有得到我期望的结果。下面的代码有什么问题?
boolean passwordOnlyAlphaNumericCheck = false;
Pattern patternAlphaNumericCheck = Pattern.compile("^[a-zA-Z0-9]$");
Matcher matcherAlphaNumericCheck = patternAlphaNumericCheck.matcher(login.getPassword());
if(matcherAlphaNumericCheck.find())
passwordOnlyAlphaNumericCheck = true;
感谢您的帮助
【问题讨论】:
-
"^[a-zA-Z0-9]+$"或"^[a-zA-Z0-9]*"- 你忘记了量词。您可以删除^和$并使用.matches()。 -
@WiktorStribiżew 它在我使用
login.getPassword().matches("[0-9a-zA-Z]*");时有效,但login.getPassword().matches("[0-9a-zA-Z]*");和login.getPassword().matches("[0-9a-zA-Z]");之间有什么区别 -
查看更多解释。