前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java正则表达式验证格式 手机、邮箱、字符串

Java正则表达式验证格式 手机、邮箱、字符串

原创
作者头像
大师级码师
修改2021-10-29 10:16:10
2.3K0
修改2021-10-29 10:16:10
举报
文章被收录于专栏:大师级码师
手机号码验证
代码语言:javascript
复制
    public static boolean isMobileNO(String mobiles) {
        try {
            Pattern p = Pattern
                    .compile("(13[0-9]|14[57]|15[012356789]|18[02356789])\\d{8}");
            Matcher m = p.matcher(mobiles);
            return m.matches();
        } catch (Exception e) {
            return false;
        }
    }
验证邮箱地址是否正确
代码语言:javascript
复制
    public static boolean checkEmail(String email) {
        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);
            return matcher.matches();
        } catch (Exception e) {
            return false;
        }
    }
检测字符串中是否包含汉字
代码语言:javascript
复制
    public static boolean checkChinese(String sequence) {
        final String format = "[//u4E00-//u9FA5//uF900-//uFA2D]";
        boolean result = false;
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(sequence);
        result = matcher.find();
        return result;
    }
检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
代码语言:javascript
复制
   public static boolean checkNickname(String sequence) {
        final String format = "[^//u4E00-//u9FA5//uF900-//uFA2D//w-_]";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(sequence);
        return !matcher.find();
    } 
获取中间有*号的手机号
代码语言:javascript
复制
   public static String getPhonePass(String phone) {
        if (null == phone || "".equals(phone) || phone.length() < 11) {
            return "";
        }

        String passA = phone.substring(0, 3);
        String passB = phone.substring(phone.length() - 3, phone.length());
        return passA + "*****" + passB;
    } 
获取中间有*号的身份证号
public static String getPidPass(String pid) { if (null == pid || "".equals(pid) || pid.length() < 18) { return ""; } String passA = pid.substring(0, 3); String passB = pid.substring(pid.length() - 3, pid.length()); return passA + "*****" + passB; }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 手机号码验证
  • 验证邮箱地址是否正确
  • 检测字符串中是否包含汉字
  • 检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
  • 获取中间有*号的手机号
  • 获取中间有*号的身份证号
  • public static String getPidPass(String pid) { if (null == pid || "".equals(pid) || pid.length() < 18) { return ""; } String passA = pid.substring(0, 3); String passB = pid.substring(pid.length() - 3, pid.length()); return passA + "*****" + passB; }
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档