我们假设您希望编辑文本字段中包含以下内容。你会用什么正则表达式?
对于我用过的电子邮件:
if(!email.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
etEmail.setError("Invalid Email Address");
}
但我如何与其他人一起去?
提前致谢!
答案 0 :(得分:2)
对于密码,您可以尝试:
if(!(password.length() >= 6) || !password.matches(".*?[A-Z].*") || !password.matches(".*?[a-z].*") || !password.matches(".*?[0-9].*") || !password.matches(".*?[\\W\\-_].*")) {
System.out.println("Password Invalid");
}
.*? - indicates that the regex may start with zero-or-more characters, before being followed by<br>
[A-Z] - a Capital Letter from 'A' to 'Z'
[a-z] - a lowercase letter from 'a' to 'z'
[0-9] - a digit from 0-9
[\\W\\-_] - a non-word character (not letter, digit or dash), a dash, or underscore
.* - indicates that after the special character (or whatever the requirement for that condition is), there may be zero-or-more characters.
这种方法可能看起来有点冗长,但肯定更容易阅读和理解。
答案 1 :(得分:0)
代表姓名: - if(!name.matches(&#34; [a-zA-Z0-9]&#34;)){ //在这里显示错误}
用于电子邮件:if(!Patterns.EMAIL_ADDRESS.matcher(target).matches()){ /在这里显示错误}
for mobile: - if(mobile.length&lt; 10){ //在这里显示错误}