正则验证 邮箱 手机 IP地址等等

本文详细介绍并提供了多个正则表达式的实例,包括手机号、邮箱、QQ号、固定电话、日期格式、IP地址等的验证方法,是进行数据格式校验的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每天一篇之记录一波正则表达式验证

判断手机是否正确

public static boolean isMobile(String mobile){
        if(mobile==null||mobile.isEmpty())
        {
            return false;
        }
        String check = "^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(mobile);
        return matcher.matches();
    }

判断邮箱是否正确

public static boolean isEmail(String email){
        if(email==null||email.isEmpty())
        {
            return false;
        }
        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);
        return matcher.matches();
    }

QQ号验证

public static boolean isQQ(String qq){
        if(qq==null||qq.isEmpty())
        {
            return false;
        }
        String check = "^[1-9][0-9]{4,12}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(qq);
        return matcher.matches();
    }

固定电话验证

public static boolean isTelphone(String str){
        if(str==null||str.isEmpty())
        {
            return false;
        }
        String check = "^[0][1-9]{2,3}-[0-9]{5,10}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(str);
        return matcher.matches();
    }

字符串是否符合 格式为2017-12-25 的日期

public static boolean isTrueDay(String str){
        if(str==null||str.isEmpty())
        {
            return false;
        }
        String check = "\\d{4}\\-(0?[1-9]|[1][012])\\-(0?[1-9]|[12][0-9]|3[01])";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(str);
        return matcher.matches();
    }

是否正确ip

public static boolean isIP(String ip)
    {
        if(ip==null||ip.isEmpty())
        {
            return false;
        }
        String check = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])(\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)){3}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(ip);
        return matcher.matches();
    }

获取日期格式为 yyyy-MM-dd 的昨天

public static String getYesterday()
    {
        Date date=new Date();
        //Calendar calendar =new GregorianCalendar();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);
        date = calendar.getTime();
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(date);
        return dateString;
    }

就暂时记录这么多,,,欢迎大家找茬

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值