zoukankan      html  css  js  c++  java
  • java.util.regex.Pattern正则表达式写验证器示例

    import java.util.regex.Pattern;<br>
    /**
    * 校验器:利用正则表达式校验邮箱、手机号等
    *
    */<br><br>
    public class Validator {
    <br>/**
    * 正则表达式:验证用户名
    */
      //public static final String REGEX_USERNAME = "^[a-zA-Z]\w{5,17}$";
      public static final String REGEX_USERNAME = "^[a-zA-Z]\w{2,20}$";
    <br>
    /**
    * 正则表达式:验证密码
    */
      public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
    <br>
    /**
    * 正则表达式:验证手机号
    */
    // public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\D])|(18[0,5-9]))\d{8}$";
      public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\D])|(17[0-9])|(18[0-9]))\d{8}$";
    <br>
    /**
    * 正则表达式:验证邮箱
    */
      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,}$";
    <br>
    /**
    * 校验用户名
    *
    * @param username
    * @return 校验通过返回true,否则返回false
    */
      public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
        }
    <br>
    /**
    * 校验密码
    *
    * @param password
    * @return 校验通过返回true,否则返回false
    */
      public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
        }
    <br>
    /**
    * 校验手机号
    *
    * @param mobile
    * @return 校验通过返回true,否则返回false
    */
      public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
        }
    <br>
    /**
    * 校验邮箱
    *
    * @param email
    * @return 校验通过返回true,否则返回false
    */
      public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
        }
     
      public static void main(String[] args) {
         String username = "fdsdfsdj";
         System.out.println(Validator.isUsername(username));
     
        }
     
    }
  • 相关阅读:
    【docker】win10安装docker教程
    【大数据】hive 删除临时文件 .hive-staging_hive
    【PostgreSql】生成数据字典
    【python3】基于scrapyd + scrapydweb 的可视化部署
    【python3】将视频转换为代码视频
    博客转移,永久退出博客园
    对dataframe中某一列进行计数
    解决mac上matplotlib中文无法显示问题
    在Jupyter notebook中使用特定虚拟环境中的python的kernel
    ubuntu18.04里更新系统源和pip源
  • 原文地址:https://www.cnblogs.com/funljq/p/10709250.html
Copyright © 2011-2022 走看看