今天遇到一个emoji表情需要做限制 找了好久 就简单的试了一下 贴出来大家自己看看
package com.huis.common.util; import org.apache.commons.lang3.StringUtils; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.util.regex.Pattern.*; public class ValidateUtils { /** * 验证字符串是否为空 */ public static boolean checkStr(String str) { if (StringUtils.isNotBlank(str)) { return true; } return false; } public static boolean checkusername(String username) { boolean flag=false; try { if( username.contains("[^\\u0000-\\uFFFF]")) { return false; } username = username.replaceAll("[^\\u0000-\\uFFFF]", ""); String check="[u4E00-u9FA5]{1,16}|[a-zA-Z]{1,32}"; Pattern regex = compile(check); Matcher matcher = regex.matcher(username); flag = matcher.matches(); }catch (Exception e){ flag=false; } return flag; } /** * 验证用户账号是否为空 */ public static boolean checkLong(Integer id) { String idStr = String.valueOf(id); return checkStr(idStr); } /** * 验证邮箱 * * @param email * @return */ public static boolean checkEmail(String email) { boolean flag = false; 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 = compile(check); Matcher matcher = regex.matcher(email); flag = matcher.matches(); } catch (Exception e) { flag = false; } return flag; } /** * 验证邮编 * * @param postCode * @return */ public static boolean checkPostCode(String postCode) { String reg = "[1-9]\\d{5}"; return matches(reg, postCode); } /** * 验证手机号码 * * @param mobile * @return */ // public static boolean checkMobile(String mobile) { // boolean flag = false; // try { // Pattern regx = compile("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[35678]|18[0-9]|19[89])\\d{8}$"); // Matcher matcher = regx.matcher(mobile); // flag = matcher.matches(); // } catch (Exception e) { // flag = false; // } // return flag; // } public static boolean checkMobile(String mobile) { boolean flag = false; try { Pattern regx = compile("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0-9]|18[0-9]|19[89])\\d{8}$"); Matcher matcher = regx.matcher(mobile); flag = matcher.matches(); } catch (Exception e) { flag = false; } return flag; } public static boolean isMobile(String mobile) { boolean flag = false; try { Pattern regx = compile("^\\d{11}$"); Matcher matcher = regx.matcher(mobile); flag = matcher.matches(); } catch (Exception e) { flag = false; } return flag; } /** * 验证密码 * @param password * @return */ public static boolean isPassword(String password) { if (StringUtils.isBlank(password)) { return false; } /*boolean matches = Pattern.matches("[a-zA-Z0-9_]{6,16}", password); if (matches) { return true; }*/ boolean matches = password.length()<6 || password.length()>12; if (matches) { return false; } //全是数字 matches = Pattern.matches("[0-9]*", password); if (matches) { return false; } //是否只包含数字,不包含字符串 matches = Pattern.matches("(.*?)\\d(.*?)", password); if (!matches) { return false; } /* //全是字母 matches = Pattern.matches("[A-Za-z]*", password); if (matches) { return false; } //全是数字 matches = Pattern.matches("[0-9]*", password); if (matches) { return false; } //只能是数字和字母 matches = Pattern.matches("[A-Za-z0-9]*", password); if (!matches) { return false; }*/ return true; } /** * 从html中的src属性中获取图片路径 */ public static Set<String> getImgStr(String htmlStr) { Set<String> pics = new HashSet<String>(); String img = ""; Pattern p_image; Matcher m_image; String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>"; p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE); m_image = p_image.matcher(htmlStr); while (m_image.find()) { // 得到<img />数据 img = m_image.group(); // 匹配<img>中的src数据 Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img); while (m.find()) { pics.add(m.group(1)); } } return pics; } public static boolean checkPhone(String str) { Pattern p1 = null,p2 = null; Matcher m = null; boolean b = false; p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的 p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的 if(str.length() >9) { m = p1.matcher(str); b = m.matches(); }else{ m = p2.matcher(str); b = m.matches(); } return b; } }
下面这是一个生成类
package com.huis.common.util; import java.security.SecureRandom; import java.util.Date; import java.util.Random; import org.apache.commons.lang.time.FastDateFormat; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * 验证码生成器 * * @see ------------------------------------------------------------------------ * -- ------------------------------------ * @see 可生成数字、大写、小写字母及三者混合类型的验证码 * @see 支持自定义验证码字符数量,支持自定义验证码图片的大小,支持自定义需排除的特殊字符,支持自定义干扰线的数量,支持自定义验证码图文颜色 */ public class DynamicCodeUtil { public final static Log logger = LogFactory.getLog(DynamicCodeUtil.class); private static SecureRandom random = new SecureRandom(); private static final char[] BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray(); /** * 验证码类型为仅数字,即0~9 */ public static final int TYPE_NUM_ONLY = 0; /** * 验证码类型为仅字母,即大小写字母混合 */ public static final int TYPE_LETTER_ONLY = 1; /** * 验证码类型为数字和大小写字母混合 */ public static final int TYPE_ALL_MIXED = 2; /** * 验证码类型为数字和大写字母混合 */ public static final int TYPE_NUM_UPPER = 3; /** * 验证码类型为数字和小写字母混合 */ public static final int TYPE_NUM_LOWER = 4; /** * 验证码类型为仅大写字母 */ public static final int TYPE_UPPER_ONLY = 5; /** * 验证码类型为仅小写字母 */ public static final int TYPE_LOWER_ONLY = 6; private DynamicCodeUtil() { } /** * 生成验证码字符串 * * @param type * 验证码类型,参见本类的静态属性 * @param length * 验证码长度,要求大于0的整数 * @param excludeString * 需排除的特殊字符(无需排除则为null) * @return 验证码字符串 */ public static String generateCode(int type, int length, String excludeString) { if (length <= 0) { return ""; } StringBuffer verifyCode = new StringBuffer(); int i = 0; Random random = new Random(); switch (type) { case TYPE_NUM_ONLY: while (i < length) { int t = random.nextInt(10); // 排除特殊字符 if (null == excludeString || excludeString.indexOf(t + "") < 0) { verifyCode.append(t); i++; } } break; case TYPE_LETTER_ONLY: while (i < length) { int t = random.nextInt(123); if ((t >= 97 || (t >= 65 && t <= 90)) && (null == excludeString || excludeString.indexOf((char) t) < 0)) { verifyCode.append((char) t); i++; } } break; case TYPE_ALL_MIXED: length = length > 17 ? length : 17; verifyCode.append(randomBase62(3) + FastDateFormat.getInstance("yyyyMMddhhmmss").format(new Date()) + randomBase62(length - 17)); break; case TYPE_NUM_UPPER: while (i < length) { int t = random.nextInt(91); if ((t >= 65 || (t >= 48 && t <= 57)) && (null == excludeString || excludeString.indexOf((char) t) < 0)) { verifyCode.append((char) t); i++; } } break; case TYPE_NUM_LOWER: while (i < length) { int t = random.nextInt(123); if ((t >= 97 || (t >= 48 && t <= 57)) && (null == excludeString || excludeString.indexOf((char) t) < 0)) { verifyCode.append((char) t); i++; } } break; case TYPE_UPPER_ONLY: while (i < length) { int t = random.nextInt(91); if ((t >= 65) && (null == excludeString || excludeString.indexOf((char) t) < 0)) { verifyCode.append((char) t); i++; } } break; case TYPE_LOWER_ONLY: while (i < length) { int t = random.nextInt(123); if ((t >= 97) && (null == excludeString || excludeString.indexOf((char) t) < 0)) { verifyCode.append((char) t); i++; } } break; } return verifyCode.toString(); } public static String randomBase62(int length) { byte[] randomBytes = new byte[length]; random.nextBytes(randomBytes); return encodeBase62(randomBytes); } public static String encodeBase62(byte[] input) { char[] chars = new char[input.length]; for (int i = 0; i < input.length; i++) { chars[i] = BASE62[(input[i] & 0xFF) % BASE62.length]; } return new String(chars); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { logger.debug(DynamicCodeUtil.generateCode(DynamicCodeUtil.TYPE_ALL_MIXED, 32, null)); } String verifycode = DynamicCodeUtil.generateCode(DynamicCodeUtil.TYPE_NUM_ONLY, 4, ""); System.out.println(verifycode); } }