Skip to content

Commit 6fbe083

Browse files
committed
see 11/16 log
1 parent 7c5d9d4 commit 6fbe083

File tree

3 files changed

+178
-44
lines changed

3 files changed

+178
-44
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/ConstUtils.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,23 @@ public enum TimeUnit {
9090
/**
9191
* 正则:身份证号码15位
9292
*/
93-
public static final String REGEX_IDCARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
93+
public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
9494
/**
9595
* 正则:身份证号码18位
9696
*/
97-
public static final String REGEX_IDCARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
97+
public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
9898
/**
9999
* 正则:邮箱
100100
*/
101101
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
102102
/**
103103
* 正则:URL
104104
*/
105-
public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?";
105+
public static final String REGEX_URL = "[a-zA-z]+://[^\\s]*";
106106
/**
107107
* 正则:汉字
108108
*/
109-
public static final String REGEX_CHZ = "^[\\u4e00-\\u9fa5]+$";
109+
public static final String REGEX_ZH = "^[\\u4e00-\\u9fa5]+$";
110110
/**
111111
* 正则:用户名,取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位
112112
*/
@@ -119,4 +119,54 @@ public enum TimeUnit {
119119
* 正则:IP地址
120120
*/
121121
public static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
122+
123+
/*以下摘自http://tool.oschina.net/regex*/
124+
/**
125+
* 正则:双字节字符(包括汉字在内)
126+
*/
127+
public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\\x00-\\xff]";
128+
/**
129+
* 正则:空白行
130+
*/
131+
public static final String REGEX_BLANK_LINE = "\\n\\s*\\r";
132+
/**
133+
* 正则:QQ号
134+
*/
135+
public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}";
136+
/**
137+
* 正则:中国邮政编码
138+
*/
139+
public static final String REGEX_ZIP_CODE = "[1-9]\\d{5}(?!\\d)";
140+
/**
141+
* 正则:正整数
142+
*/
143+
public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\\d*$";
144+
/**
145+
* 正则:负整数
146+
*/
147+
public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\\d*$";
148+
/**
149+
* 正则:整数
150+
*/
151+
public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";
152+
/**
153+
* 正则:非负整数(正整数 + 0)
154+
*/
155+
public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\\d*|0$";
156+
/**
157+
* 正则:非正整数(负整数 + 0)
158+
*/
159+
public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\\d*|0$";
160+
/**
161+
* 正则:正浮点数
162+
*/
163+
public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";
164+
/**
165+
* 正则:负浮点数
166+
*/
167+
public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$";
168+
169+
/**
170+
* If u want more please visit http://toutiao.com/i6231678548520731137/
171+
*/
122172
}
Lines changed: 97 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.blankj.utilcode.utils;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.regex.Matcher;
36
import java.util.regex.Pattern;
47

58
import 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
}

utilcode/src/test/java/com/blankj/utilcode/utils/RegexUtilsTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.junit.Test;
44

5+
import java.util.Arrays;
6+
57
import static com.blankj.utilcode.utils.RegexUtils.*;
68
import static com.google.common.truth.Truth.assertThat;
79

@@ -67,8 +69,8 @@ public void testIsURL() throws Exception {
6769

6870
@Test
6971
public void testIsChz() throws Exception {
70-
assertThat(isChz("我")).isTrue();
71-
assertThat(isChz("wo")).isFalse();
72+
assertThat(isZh("我")).isTrue();
73+
assertThat(isZh("wo")).isFalse();
7274
}
7375

7476
@Test
@@ -97,4 +99,27 @@ public void testIsMatch() throws Exception {
9799
assertThat(isMatch("\\d?", "1")).isTrue();
98100
assertThat(isMatch("\\d?", "a")).isFalse();
99101
}
102+
103+
@Test
104+
public void testGetMatches() throws Exception {
105+
// 贪婪
106+
System.out.println(getMatches("b.*j", "blankj blankj"));
107+
// 懒惰
108+
System.out.println(getMatches("b.*?j", "blankj blankj"));
109+
}
110+
111+
@Test
112+
public void testGetSplits() throws Exception {
113+
System.out.println(Arrays.asList(getSplits("1 2 3", " ")));
114+
}
115+
116+
@Test
117+
public void testGetReplace() throws Exception {
118+
System.out.println(getReplaceFirst("1 2 3", " ", ", "));
119+
}
120+
121+
@Test
122+
public void testGetReplaceAll() throws Exception {
123+
System.out.println(getReplaceAll("1 2 3", " ", ", "));
124+
}
100125
}

0 commit comments

Comments
 (0)