1
1
package com .blankj .utilcode .utils ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .regex .Matcher ;
3
6
import java .util .regex .Pattern ;
4
7
5
8
import static com .blankj .utilcode .utils .ConstUtils .*;
@@ -25,122 +28,178 @@ private RegexUtils() {
25
28
/**
26
29
* 验证手机号(简单)
27
30
*
28
- * @param string 待验证文本
31
+ * @param input 待验证文本
29
32
* @return {@code true}: 匹配<br>{@code false}: 不匹配
30
33
*/
31
- public static boolean isMobileSimple (String string ) {
32
- return isMatch (REGEX_MOBILE_SIMPLE , string );
34
+ public static boolean isMobileSimple (CharSequence input ) {
35
+ return isMatch (REGEX_MOBILE_SIMPLE , input );
33
36
}
34
37
35
38
/**
36
39
* 验证手机号(精确)
37
40
*
38
- * @param string 待验证文本
41
+ * @param input 待验证文本
39
42
* @return {@code true}: 匹配<br>{@code false}: 不匹配
40
43
*/
41
- public static boolean isMobileExact (String string ) {
42
- return isMatch (REGEX_MOBILE_EXACT , string );
44
+ public static boolean isMobileExact (CharSequence input ) {
45
+ return isMatch (REGEX_MOBILE_EXACT , input );
43
46
}
44
47
45
48
/**
46
49
* 验证电话号码
47
50
*
48
- * @param string 待验证文本
51
+ * @param input 待验证文本
49
52
* @return {@code true}: 匹配<br>{@code false}: 不匹配
50
53
*/
51
- public static boolean isTel (String string ) {
52
- return isMatch (REGEX_TEL , string );
54
+ public static boolean isTel (CharSequence input ) {
55
+ return isMatch (REGEX_TEL , input );
53
56
}
54
57
55
58
/**
56
59
* 验证身份证号码15位
57
60
*
58
- * @param string 待验证文本
61
+ * @param input 待验证文本
59
62
* @return {@code true}: 匹配<br>{@code false}: 不匹配
60
63
*/
61
- public static boolean isIDCard15 (String string ) {
62
- return isMatch (REGEX_IDCARD15 , string );
64
+ public static boolean isIDCard15 (CharSequence input ) {
65
+ return isMatch (REGEX_ID_CARD15 , input );
63
66
}
64
67
65
68
/**
66
69
* 验证身份证号码18位
67
70
*
68
- * @param string 待验证文本
71
+ * @param input 待验证文本
69
72
* @return {@code true}: 匹配<br>{@code false}: 不匹配
70
73
*/
71
- public static boolean isIDCard18 (String string ) {
72
- return isMatch (REGEX_IDCARD18 , string );
74
+ public static boolean isIDCard18 (CharSequence input ) {
75
+ return isMatch (REGEX_ID_CARD18 , input );
73
76
}
74
77
75
78
/**
76
79
* 验证邮箱
77
80
*
78
- * @param string 待验证文本
81
+ * @param input 待验证文本
79
82
* @return {@code true}: 匹配<br>{@code false}: 不匹配
80
83
*/
81
- public static boolean isEmail (String string ) {
82
- return isMatch (REGEX_EMAIL , string );
84
+ public static boolean isEmail (CharSequence input ) {
85
+ return isMatch (REGEX_EMAIL , input );
83
86
}
84
87
85
88
/**
86
89
* 验证URL
87
90
*
88
- * @param string 待验证文本
91
+ * @param input 待验证文本
89
92
* @return {@code true}: 匹配<br>{@code false}: 不匹配
90
93
*/
91
- public static boolean isURL (String string ) {
92
- return isMatch (REGEX_URL , string );
94
+ public static boolean isURL (CharSequence input ) {
95
+ return isMatch (REGEX_URL , input );
93
96
}
94
97
95
98
/**
96
99
* 验证汉字
97
100
*
98
- * @param string 待验证文本
101
+ * @param input 待验证文本
99
102
* @return {@code true}: 匹配<br>{@code false}: 不匹配
100
103
*/
101
- public static boolean isChz ( String string ) {
102
- return isMatch (REGEX_CHZ , string );
104
+ public static boolean isZh ( CharSequence input ) {
105
+ return isMatch (REGEX_ZH , input );
103
106
}
104
107
105
108
/**
106
109
* 验证用户名
107
110
* <p>取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位</p>
108
111
*
109
- * @param string 待验证文本
112
+ * @param input 待验证文本
110
113
* @return {@code true}: 匹配<br>{@code false}: 不匹配
111
114
*/
112
- public static boolean isUsername (String string ) {
113
- return isMatch (REGEX_USERNAME , string );
115
+ public static boolean isUsername (CharSequence input ) {
116
+ return isMatch (REGEX_USERNAME , input );
114
117
}
115
118
116
119
/**
117
120
* 验证yyyy-MM-dd格式的日期校验,已考虑平闰年
118
121
*
119
- * @param string 待验证文本
122
+ * @param input 待验证文本
120
123
* @return {@code true}: 匹配<br>{@code false}: 不匹配
121
124
*/
122
- public static boolean isDate (String string ) {
123
- return isMatch (REGEX_DATE , string );
125
+ public static boolean isDate (CharSequence input ) {
126
+ return isMatch (REGEX_DATE , input );
124
127
}
125
128
126
129
/**
127
130
* 验证IP地址
128
131
*
129
- * @param string 待验证文本
132
+ * @param input 待验证文本
130
133
* @return {@code true}: 匹配<br>{@code false}: 不匹配
131
134
*/
132
- public static boolean isIP (String string ) {
133
- return isMatch (REGEX_IP , string );
135
+ public static boolean isIP (CharSequence input ) {
136
+ return isMatch (REGEX_IP , input );
134
137
}
135
138
136
139
/**
137
- * string是否匹配regex
140
+ * 判断是否匹配正则
138
141
*
139
- * @param regex 正则表达式字符串
140
- * @param string 要匹配的字符串
142
+ * @param regex 正则表达式
143
+ * @param input 要匹配的字符串
141
144
* @return {@code true}: 匹配<br>{@code false}: 不匹配
142
145
*/
143
- public static boolean isMatch (String regex , String string ) {
144
- return !StringUtils .isEmpty (string ) && Pattern .matches (regex , string );
146
+ public static boolean isMatch (String regex , CharSequence input ) {
147
+ return input != null && input .length () > 0 && Pattern .matches (regex , input );
148
+ }
149
+
150
+ /**
151
+ * 获取正则匹配的部分
152
+ *
153
+ * @param regex 正则表达式
154
+ * @param input 要匹配的字符串
155
+ * @return 正则匹配的部分
156
+ */
157
+ public static List <String > getMatches (String regex , CharSequence input ) {
158
+ if (input == null ) return null ;
159
+ List <String > matches = new ArrayList <>();
160
+ Pattern pattern = Pattern .compile (regex );
161
+ Matcher matcher = pattern .matcher (input );
162
+ while (matcher .find ()) {
163
+ matches .add (matcher .group ());
164
+ }
165
+ return matches ;
166
+ }
167
+
168
+ /**
169
+ * 获取正则匹配分组
170
+ *
171
+ * @param input 要分组的字符串
172
+ * @param regex 正则表达式
173
+ * @return 正则匹配分组
174
+ */
175
+ public static String [] getSplits (String input , String regex ) {
176
+ if (input == null ) return null ;
177
+ return input .split (regex );
178
+ }
179
+
180
+ /**
181
+ * 替换正则匹配的第一部分
182
+ *
183
+ * @param input 要替换的字符串
184
+ * @param regex 正则表达式
185
+ * @param replacement 代替者
186
+ * @return 替换正则匹配的第一部分
187
+ */
188
+ public static String getReplaceFirst (String input , String regex , String replacement ) {
189
+ if (input == null ) return null ;
190
+ return Pattern .compile (regex ).matcher (input ).replaceFirst (replacement );
191
+ }
192
+
193
+ /**
194
+ * 替换所有正则匹配的部分
195
+ *
196
+ * @param input 要替换的字符串
197
+ * @param regex 正则表达式
198
+ * @param replacement 代替者
199
+ * @return 替换所有正则匹配的部分
200
+ */
201
+ public static String getReplaceAll (String input , String regex , String replacement ) {
202
+ if (input == null ) return null ;
203
+ return Pattern .compile (regex ).matcher (input ).replaceAll (replacement );
145
204
}
146
205
}
0 commit comments