package com.brainshare.common.utils;标签:return,正则表达式,Character,boolean,static,UnicodeBlock,工具,ub From: https://www.cnblogs.com/wllzbky/p/17641253.html
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式工具
*/
public class RegexUtil {
private static final String REGEX_ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)";
/**
* 身份证号校验
* @param idCard
* @return
*/
public static boolean isValidIdCard(String idCard) {
return Pattern.matches(REGEX_ID_CARD, idCard);
}
public static boolean isRightChar(char c) {
return isChinese(c) || isWord(c);
}
/**
* 昵称校验(10字符内中英文)
* @param nickName
* @return
*/
public static boolean isValidNickName(String nickName){
if (StringUtils.isEmpty(nickName)){
return false;
}
char[] chars = nickName.toCharArray();
Integer length=0;
for (char aChar : chars) {
boolean chineseFlag = isChinese(aChar);
boolean wordFlag = isWord(aChar);
if (!chineseFlag&&!wordFlag){
return false;
}
if (chineseFlag){
length+=2;
}
if (wordFlag){
length+=1;
}
if (length>=10){
return false;
}
}
return true;
}
/**
* 校验某个字符是否是a-z、A-Z、_、0-9
*
* @param c 被校验的字符
* @return true 代表符合条件
*/
public static boolean isWord(char c) {
String regEx = "[a-zA-Z]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher("" + c);
return m.matches();
}
/**
* 判定输入的是否是汉字
*
* @param c 被校验的字符
* @return true 代表是汉字
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
}