首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java正则表达式验证IP,邮箱,电话

Java正则表达式验证IP,邮箱,电话

作者头像
崔笑颜
发布2020-06-08 15:51:49
1.8K0
发布2020-06-08 15:51:49
举报

java中我们会常用一些判断如IP、电子邮箱、电话号码的是不是合法,那么我们怎么来判断呢,答案就是利用正则表达式来判断了,废话不多说,下面就是上代码。

1:判断是否是正确的IP

/**
         * 用正则表达式进行判断
         */
        public boolean isIPAddressByRegex(String str) {
            String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
            // 判断ip地址是否与正则表达式匹配
            if (str.matches(regex)) {
                String[] arr = str.split("\\.");
                for (int i = 0; i < 4; i++) {
                    int temp = Integer.parseInt(arr[i]);
                    //如果某个数字不是0到255之间的数 就返回false
                    if (temp < 0 || temp > 255) return false;
                }
                return true;
            } else return false;
        }

2:判断是否是正确的邮箱地址

/**
*正则表达式验证邮箱
*/
 public static boolean isEmail(String email) {
       if (email == null || "".equals(email)) return false;
            String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
           return email.matches(regex);
  }

3:判断是否是手机号码

/**
*正则表达式验证手机
*/
public static boolean orPhoneNumber(String phoneNumber) {
       if (phoneNumber == null || "".equals(phoneNumber))
            return false;
           String regex = "^1[3|4|5|8][0-9]\\d{8}$";
           return phoneNumber.matches(regex);
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档