java之Pattern.compile相关正则表达式
-
java之Pattern.compile相关正则表达式
1.验证邮箱地址是否正确:String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
import java.util.regex.Matcher;//jar包 import java.util.regex.Pattern;//jar包 public static boolean checkEmail(String email) { boolean flag = false; try
{ String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher(email); flag = matcher.matches(); } catch (Exception e) { flag = false; } return flag; }
}
2.验证手机号码:String check="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"
3.校验密码长度:Pattern.compile("^\\w{6,20}$")//表示长度要在6至20之间