All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.fangxinqian.operator.sdk.utils.RandomUtil Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package cn.fangxinqian.operator.sdk.utils;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author NieZhiLiang
 * @Email [email protected]
 * @GitHub https://github.com/niezhiliang
 * @Date 2019-10-09 10:17 上午
 */
public class RandomUtil {

    static String[] blend = new String[] { "0","1","2", "3", "4", "5", "6", "7", "8", "9",
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    };

    /**
     * 获取uuid不带-
     * @return
     */
    public static String getUUIDNoSlash() {
        return UUID.randomUUID().toString().replace("-","");
    }


    /**
     * 获取六位数字字母混合验证码
     * @return
     */
    public static String getBlendCode() {
        StringBuffer code = null;
        while (true) {
            code = new StringBuffer();
            for (int i = 0;i < 9;i++) {
                code.append(blend[new Random().nextInt(62)]);
            }
            if (chkCode(code.toString())) {
                break;
            }
        }
        return code.toString();
    }


    /**
     * 正则表达式 验证字符串必须带数字大写小写 最少4位
     * @param mobiles
     * @return
     */
    public static boolean chkCode(String mobiles) {
        Pattern pattern = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{4,}$");
        Matcher matcher = pattern.matcher(mobiles);
        boolean b = matcher.matches();
        return b;
    }

    public static void main(String[] args) {
        System.out.println(getBlendCode());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy