bingyimeiling

可以是纯数字,也可以是纯字母,也可以是数字+字母,1-20 位

^[0-9a-zA-Z]{1,20}$

[a-z0-9A-Z]表示是字母或数字

{1, 20}表示重复出现1~20次

^表示从字符串头开始匹配

$表示匹配到字符串末尾

如果不加^和$字符串中如果有符合条件的串也会被匹配

验证是否是有效的手机号:

public static boolean isValidPhone(String phone) {
if (!isEmpty(phone)) {
Pattern pattern = Pattern.compile("^((1[0-9]))\\d{9}$");
Matcher matcher = pattern.matcher(phone);
return matcher.find();
}
return false;
}

验证是否是有效的邮箱

public static boolean isValidEmail(String email) {
if (!isEmpty(email)) {
Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
Matcher matcher = pattern.matcher(email);
return matcher.find();
}
return false;
}

验证是否是数字

public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}

 

相关文章: