一、正则表达式
package com.constants;
public class RegularConstants {
public static final String NUMBER = "^-?[0-9]+";
public static final String EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
public static final String PHONE = "^1([12345789][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";
public static final String PHONE_TWO = "^1\\d{10}$";
public static final String INTEGER_OR_FLOAT = "^[0-9]+=\\.{0,1}[0-9]{0,2}$";
public static final String NUMBER_LEN = "^\\d{n}$";
public static final String NUMBER_MIN_LEN = "^\\d{n,}$";
public static final String NUMBER_LEN_RANGE = "^\\d{m,n}$";
public static final String NUMBER_LEN_UNSIGNED_CONTAINS_ZERO = "^(0|[1-9][0-9]*)$";
public static final String FLOAT_TWO_LEN = "^[0-9]+(.[0-9]{2})?$";
public static final String FLOAT_LEN_RANGE = "^[0-9]+(.[0-9]{1,3})?$";
public static final String NUMBER_UNSIGNED = "^\\+?[1-9][0-9]*$";
public static final String NUMBER_NOT_UNSIGNED = "^\\-[1-9][]0-9\"*$";
public static final String STRING_THREE_LEN = "^.{3}$";
public static final String STRING_ALL_EN_CHARS = "^[A-Za-z]+$";
public static final String STRING_UPPER_EN_CHARS = "^[A-Z]+$";
public static final String STRING_LOWER_EN_CHARS = "^[a-z]+$";
public static final String STRING_ALL_EN_CHARS_AND_NUMBER = "^[A-Za-z0-9]+$";
public static final String STRING_ALL_EN_CHARS_AND_UNDER_LINE = "^\\w+";
public static final String PASSWORD = "^[a-zA-Z]\\w{5,17}$";
public static final String SMS_VALIDATE_CODE_NUM = "^\\d{6}$";
public static final String VALIDATE_CODE_NUM_SIX = "^\\d{6}$";
public static final String CHINESE = "^[\\u4e00-\\u9fa5]{0,}$";
public static final String URL = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
public static final String TEL = "^(\\(\\d{3,4}-)|\\d{3.4}-)?\\d{7,8}$";
public static final String ID_CARD = "^\\d{15}|\\d{18}$";
public static final String MONTH = "^(0?[1-9]|1[0-2])$";
public static final String DAY = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
public static final String EMPTY_LINE = "\\n[\\s| ]*\\r";
public static final String HTML_TAG = "<(.*)>(.*)<\\/(.*)>|<(.*)\\/>";
public static final String TRIM = "(^\\s*)|(\\s*$)";
}
二、示例
@ApiModelProperty(value = "手机号", required = true)
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = RegularConstants.PHONE, message = "手机号码格式错误")
private String phone;
@ApiModelProperty(value = "密码", required = true)
@Pattern(regexp = RegularConstants.PASSWORD, message = "密码格式错误,密码必须以字母开头,长度在6~18之间,只能包含字符、数字和下划线")
private String password;
@ApiModelProperty(value = "验证码", required = true)
@Pattern(regexp = RegularConstants.SMS_VALIDATE_CODE_NUM, message = "验证码格式错误,验证码必须为4位数字")
private String validateCode;