首页 > 编程语言 > 详细

java.util.regex.Pattern正则表达式写验证器示例

时间:2019-03-24 18:48:10      阅读:139      评论:0      收藏:0      [点我收藏+]
import java.util.regex.Pattern;
/** * 校验器:利用正则表达式校验邮箱、手机号等 * */

public class Validator {
/** * 正则表达式:验证用户名 */ //public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";   public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{2,20}$";
/** * 正则表达式:验证密码 */   public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
/** * 正则表达式:验证手机号 */ // 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}$";
/** * 正则表达式:验证邮箱 */   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,}$";
/** * 校验用户名 * * @param username * @return 校验通过返回true,否则返回false */   public static boolean isUsername(String username) {     return Pattern.matches(REGEX_USERNAME, username);     }
/** * 校验密码 * * @param password * @return 校验通过返回true,否则返回false */   public static boolean isPassword(String password) {     return Pattern.matches(REGEX_PASSWORD, password);     }
/** * 校验手机号 * * @param mobile * @return 校验通过返回true,否则返回false */   public static boolean isMobile(String mobile) {     return Pattern.matches(REGEX_MOBILE, mobile);     }
/** * 校验邮箱 * * @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));     } }

  

java.util.regex.Pattern正则表达式写验证器示例

原文:https://www.cnblogs.com/xiaohuomiao/p/10589387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!