废话不多说了,直接给大家贴java代码了,代码有所注释,写的不好,还请各位大家多多关照。
代码如下所示:
package com.alibaba.uyuni.common.util;
import java.util.Random;
public class GeneratePassword {
public static String genRandomNum(int pwd_len) {
// 26*2个字母+10个数字
final int maxNum = 62;
int i; // 生成的随机数
int count = 0; // 生成的密码的长度
char[] str = { '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','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < pwd_len) {
// 生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为62-1
if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count++;
}
}
return pwd.toString();
}
public static void main(String[] args) {
System.out.println(genRandomNum(6));//
}
}
package com.alibaba.uyuni.common.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
public static boolean checkEmail(String email) {
String regex = "\w+@\w+\.[a-z]+(\.[a-z]+)?";
return Pattern.matches(regex, email);
}
public static boolean checkIdCard(String idCard) {
String regex = "[1-9]\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex,idCard);
}
public static boolean checkMobile(String mobile) {
String regex = "(\+\d+)?1[3458]\d{9}$";
return Pattern.matches(regex,mobile);
}
public static boolean checkPhone(String phone) {
String regex = "(\+\d+)?(\d{3,4}\-?)?\d{7,8}$";
return Pattern.matches(regex, phone);
}
public static boolean checkDigit(String digit) {
String regex = "\-?[1-9]\d+";
return Pattern.matches(regex,digit);
}
public static boolean checkDecimals(String decimals) {
String regex = "\-?[1-9]\d+(\.\d+)?";
return Pattern.matches(regex,decimals);
}
public static boolean checkBlankSpace(String blankSpace) {
String regex = "\s+";
return Pattern.matches(regex,blankSpace);
}
public static boolean checkChinese(String chinese) {
String regex = "^[u4E00-u9FA5]+$";
return Pattern.matches(regex,chinese);
}
public static boolean checkBirthday(String birthday) {
String regex = "[1-9]{4}([-./])\d{1,2}\1\d{1,2}";
return Pattern.matches(regex,birthday);
}
public static boolean checkURL(String url) {
String regex = "(https?://(w{3}\.)?)?\w+\.\w+(\.[a-zA-Z]+)*(:\d{1,5})?(/\w*)*(\??(.+=.*)?(&.+=.*)?)?";
return Pattern.matches(regex, url);
}
public static String getDomain(String url) {
Pattern p = Pattern.compile("(?<=http://|\.)[^.]*?\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
// 获取完整的域名
// Pattern p=Pattern.compile("[^//]*?\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
return matcher.group();
}
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\d{5}";
return Pattern.matches(regex, postcode);
}
public static boolean checkIpAddress(String ipAddress) {
String regex = "[1-9](\d{1,2})?\.(0|([1-9](\d{1,2})?))\.(0|([1-9](\d{1,2})?))\.(0|([1-9](\d{1,2})?))";
return Pattern.matches(regex, ipAddress);
}
}
以上所述是小编给大家分享的Java随机密码生成并和邮箱、手机匹配的相关内容,希望对大家有所帮助。



