廢話不多說了,直接給大家貼java代碼了,代碼有所注釋,寫的不好,還請各位大家多多關照。
代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package com.alibaba.uyuni.common.util; import java.util.Random; public class GeneratePassword { /** * 生成隨機密碼 * @param pwd_len * 生成的密碼的總長度 * @return 密碼的字符串 */ 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 { /** * 驗證Email * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表郵件服務商 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkEmail(String email) { String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?" ; return Pattern.matches(regex, email); } /** * 驗證***號碼 * @param idCard 居民***號碼15位或18位,最后一位可能是數字或字母 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkIdCard(String idCard) { String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}" ; return Pattern.matches(regex,idCard); } /** * 驗證手機號碼(支持國際格式,+86135xxxx...(中國內地),+00852137xxxx...(中國香港)) * @param mobile 移動、聯通、電信運營商的號碼段 *<p>移動的號段:134(0-8)、135、136、137、138、139、147(預計用于TD上網卡) *、150、151、152、157(TD專用)、158、159、187(未啟用)、188(TD專用)</p> *<p>聯通的號段:130、131、132、155、156(世界風專用)、185(未啟用)、186(3g)</p> *<p>電信的號段:133、153、180(未啟用)、189</p> * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkMobile(String mobile) { String regex = "(\\+\\d+)?1[3458]\\d{9}$" ; return Pattern.matches(regex,mobile); } /** * 驗證固定電話號碼 * @param phone 電話號碼,格式:國家(地區)電話代碼 + 區號(城市代碼) + 電話號碼,如:+8602085588447 * <p><b>國家(地區) 代碼 :</b>標識電話號碼的國家(地區)的標準國家(地區)代碼。它包含從 0 到 9 的一位或多位數字, * 數字之后是空格分隔的國家(地區)代碼。</p> * <p><b>區號(城市代碼):</b>這可能包含一個或多個從 0 到 9 的數字,地區或城市代碼放在圓括號—— * 對不使用地區或城市代碼的國家(地區),則省略該組件。</p> * <p><b>電話號碼:</b>這包含從 0 到 9 的一個或多個數字 </p> * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkPhone(String phone) { String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$" ; return Pattern.matches(regex, phone); } /** * 驗證整數(正整數和負整數) * @param digit 一位或多位0-9之間的整數 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkDigit(String digit) { String regex = "\\-?[1-9]\\d+" ; return Pattern.matches(regex,digit); } /** * 驗證整數和浮點數(正負整數和正負浮點數) * @param decimals 一位或多位0-9之間的浮點數,如:1.23,233.30 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkDecimals(String decimals) { String regex = "\\-?[1-9]\\d+(\\.\\d+)?" ; return Pattern.matches(regex,decimals); } /** * 驗證空白字符 * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkBlankSpace(String blankSpace) { String regex = "\\s+" ; return Pattern.matches(regex,blankSpace); } /** * 驗證中文 * @param chinese 中文字符 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkChinese(String chinese) { String regex = "^[\u4E00-\u9FA5]+$" ; return Pattern.matches(regex,chinese); } /** * 驗證日期(年月日) * @param birthday 日期,格式:1992-09-03,或1992.09.03 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkBirthday(String birthday) { String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}" ; return Pattern.matches(regex,birthday); } /** * 驗證URL地址 * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80 * @return 驗證成功返回true,驗證失敗返回false */ 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); } /** * <pre> * 獲取網址 URL 的一級域名 * http://www.zuidaima.com/share/1550463379442688.htm ->> zuidaima.com * </pre> * * @param url * @return */ 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(); } /** * 匹配中國郵政編碼 * @param postcode 郵政編碼 * @return 驗證成功返回true,驗證失敗返回false */ public static boolean checkPostcode(String postcode) { String regex = "[1-9]\\d{5}" ; return Pattern.matches(regex, postcode); } /** * 匹配IP地址(簡單匹配,格式,如:192.168.1.1,127.0.0.1,沒有匹配IP段的大小) * @param ipAddress IPv4標準地址 * @return 驗證成功返回true,驗證失敗返回false */ 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隨機密碼生成并和郵箱、手機匹配的相關內容,希望對大家有所幫助。