Android实用的正则表达式工具

1、邮箱正则表达式
public boolean isEmail(String strEmail) {
    String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
    Pattern p = Pattern.compile(strPattern);
    Matcher m = p.matcher(strEmail);
    return m.matches();
}



2、验证手机格式
public static boolean isMobileNO(String mobiles) {
  /*
 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
 联通:130、131、132、152、155、156、185、186
 电信:133、153、180、189、(1349卫通) //177电信4G
 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
 */
    String telRegex = "[1][34578]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。

    if (TextUtils.isEmpty(mobiles))
        return false;
    else
        return mobiles.matches(telRegex);
}


3、验证车牌号
public static boolean isCarNo(String carNO) {

    String strPattern = "^[A-Z0-9]*[A-Z0-9]";
    if (TextUtils.isEmpty(carNO)) {
        return false;
    } else {
        return carNO.matches(strPattern);
    }
}

4、判断字符串是否是日期格式
public static boolean isValidDate(String str) {
    boolean convertSuccess = true;
    // 指定日期格式为四位年-两位月份-两位日期,注意yyyy-MM-dd区分大小写;
    SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
        // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2015-11-29会被接受,并转换成2015-11-01
        simpleDataFormat.setLenient(false);
        simpleDataFormat.parse(str);
    } catch (ParseException e) {
         e.printStackTrace();
        // 如果throw 就说明格式不对
        convertSuccess = false;
    }
    return convertSuccess;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值