Android常用正则表达式验证工具类(实例代码)
程序员文章站
2023-12-12 17:07:16
东西不多,但一般项目够用了。
public class regularutil {
//身份证
public static final...
东西不多,但一般项目够用了。
public class regularutil { //身份证 public static final string regex_id_card = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x)$"; //验证邮箱 public static final string regex_email = "^([a-z0-9a-z]+[-|\\.]?)+[a-z0-9a-z]@([a-z0-9a-z]+(-[a-z0-9a-z]+)?\\.)+[a-za-z]{2,}$"; //手机号 public static final string regex_phone = "0?(13|14|15|17|18)[0-9]{9}"; //汉字 public static final string regex_truename = "^[\\u4e00-\\u9fa5]*$"; //银行卡 public static final string banknumber="^([0-9]{16}|[0-9]{19})$"; //固话电话正则 public static final string tele= "([0-9]{3,4}-)?[0-9]{7,8}"; //密码强度验证 public static final string regex_pws="^(((?=.*[0-9])(?=.*[a-za-z])|(?=.*[0-9])(?=.*[^\\s0-9a-za-z])|(?=.*[a-za-z])(?=.*[^\\s0-9a-za-z]))[^\\s]+)$"; public static boolean isphone(string mobiles){ if (textutils.isempty(mobiles)){ return false; }else{ return mobiles.matches(regex_phone); } } public static boolean istel(string mobiles) { if (textutils.isempty(mobiles)) { return false; } else { return mobiles.matches(tele); } } public static boolean isidacard(string idcard){ if (textutils.isempty(idcard)) return false; else return idcard.matches(regex_id_card); } public static boolean isbankcard(string bankcard){ if (textutils.isempty(bankcard)) return false; else return bankcard.matches(banknumber); } public static boolean isemail(string email){ if (textutils.isempty(email)) return false; else return email.matches(regex_email); } public static boolean istruename(string name){ if (textutils.isempty(name)) return false; else return name.matches(regex_truename); } //验证密码强度 public static boolean ispsw(string psw) { //包含数字,字母,特殊符号其中至少两种 if (textutils.isempty(psw)) { return false; } else { //限制长度 if(psw.length()<6||psw.length()>16){ return false; } //matches():字符串是否在给定的正则表达式匹配 return psw.matches(regex_pws); } } }
总结
以上所述是小编给大家介绍的android常用正则表达式验证工具类(实例代码),希望对大家有所帮助