11package com .blankj .utilcode .utils ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ import java .util .regex .Matcher ;
36import java .util .regex .Pattern ;
47
58import static com .blankj .utilcode .utils .ConstUtils .*;
@@ -25,122 +28,178 @@ private RegexUtils() {
2528 /**
2629 * 验证手机号(简单)
2730 *
28- * @param string 待验证文本
31+ * @param input 待验证文本
2932 * @return {@code true}: 匹配<br>{@code false}: 不匹配
3033 */
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 );
3336 }
3437
3538 /**
3639 * 验证手机号(精确)
3740 *
38- * @param string 待验证文本
41+ * @param input 待验证文本
3942 * @return {@code true}: 匹配<br>{@code false}: 不匹配
4043 */
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 );
4346 }
4447
4548 /**
4649 * 验证电话号码
4750 *
48- * @param string 待验证文本
51+ * @param input 待验证文本
4952 * @return {@code true}: 匹配<br>{@code false}: 不匹配
5053 */
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 );
5356 }
5457
5558 /**
5659 * 验证身份证号码15位
5760 *
58- * @param string 待验证文本
61+ * @param input 待验证文本
5962 * @return {@code true}: 匹配<br>{@code false}: 不匹配
6063 */
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 );
6366 }
6467
6568 /**
6669 * 验证身份证号码18位
6770 *
68- * @param string 待验证文本
71+ * @param input 待验证文本
6972 * @return {@code true}: 匹配<br>{@code false}: 不匹配
7073 */
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 );
7376 }
7477
7578 /**
7679 * 验证邮箱
7780 *
78- * @param string 待验证文本
81+ * @param input 待验证文本
7982 * @return {@code true}: 匹配<br>{@code false}: 不匹配
8083 */
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 );
8386 }
8487
8588 /**
8689 * 验证URL
8790 *
88- * @param string 待验证文本
91+ * @param input 待验证文本
8992 * @return {@code true}: 匹配<br>{@code false}: 不匹配
9093 */
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 );
9396 }
9497
9598 /**
9699 * 验证汉字
97100 *
98- * @param string 待验证文本
101+ * @param input 待验证文本
99102 * @return {@code true}: 匹配<br>{@code false}: 不匹配
100103 */
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 );
103106 }
104107
105108 /**
106109 * 验证用户名
107110 * <p>取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位</p>
108111 *
109- * @param string 待验证文本
112+ * @param input 待验证文本
110113 * @return {@code true}: 匹配<br>{@code false}: 不匹配
111114 */
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 );
114117 }
115118
116119 /**
117120 * 验证yyyy-MM-dd格式的日期校验,已考虑平闰年
118121 *
119- * @param string 待验证文本
122+ * @param input 待验证文本
120123 * @return {@code true}: 匹配<br>{@code false}: 不匹配
121124 */
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 );
124127 }
125128
126129 /**
127130 * 验证IP地址
128131 *
129- * @param string 待验证文本
132+ * @param input 待验证文本
130133 * @return {@code true}: 匹配<br>{@code false}: 不匹配
131134 */
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 );
134137 }
135138
136139 /**
137- * string是否匹配regex
140+ * 判断是否匹配正则
138141 *
139- * @param regex 正则表达式字符串
140- * @param string 要匹配的字符串
142+ * @param regex 正则表达式
143+ * @param input 要匹配的字符串
141144 * @return {@code true}: 匹配<br>{@code false}: 不匹配
142145 */
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 );
145204 }
146205}
0 commit comments