我是靠谱客的博主 犹豫黑米,这篇文章主要介绍工具类之RegexUtils(正则工具类),现在分享给大家,希望可以做个参考。

正则表达式,相信接触过编程的人都知道,但是大部分人应该是每次用的时候现找,但对其语法应该只是一知半解,如果乘客们想要更好地了解正则,那么老司机也可以再另起一篇介绍正则中比较重要的几个知识点。好啦,本次是直接带给你们福利的,常用正则相关的应该都囊括在本工具类了,下面开始开车,请系好安全带。

站点

  • 正则相关→RegexUtils.java→Test
    复制代码
    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
    isMobileSimple : 验证手机号(简单) isMobileExact : 验证手机号(精确) isTel : 验证电话号码 isIDCard15 : 验证身份证号码15位 isIDCard18 : 验证身份证号码18位 isEmail : 验证邮箱 isURL : 验证URL isZh : 验证汉字 isUsername : 验证用户名 isDate : 验证yyyy-MM-dd格式的日期校验,已考虑平闰年 isIP : 验证IP地址 isMatch : 判断是否匹配正则 getMatches : 获取正则匹配的部分 getSplits : 获取正则匹配分组 getReplaceFirst : 替换正则匹配的第一部分 getReplaceAll : 替换所有正则匹配的部分

具体路线

复制代码
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import static com.blankj.utilcode.utils.ConstUtils.*; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2016/8/2 * desc : 正则相关工具类 * </pre> */ public class RegexUtils { private RegexUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * If u want more please visit http://toutiao.com/i6231678548520731137/ */ /** * 验证手机号(简单) * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileSimple(CharSequence input) { return isMatch(REGEX_MOBILE_SIMPLE, input); } /** * 验证手机号(精确) * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileExact(CharSequence input) { return isMatch(REGEX_MOBILE_EXACT, input); } /** * 验证电话号码 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isTel(CharSequence input) { return isMatch(REGEX_TEL, input); } /** * 验证身份证号码15位 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIDCard15(CharSequence input) { return isMatch(REGEX_ID_CARD15, input); } /** * 验证身份证号码18位 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIDCard18(CharSequence input) { return isMatch(REGEX_ID_CARD18, input); } /** * 验证邮箱 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isEmail(CharSequence input) { return isMatch(REGEX_EMAIL, input); } /** * 验证URL * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isURL(CharSequence input) { return isMatch(REGEX_URL, input); } /** * 验证汉字 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isZh(CharSequence input) { return isMatch(REGEX_ZH, input); } /** * 验证用户名 * <p>取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位</p> * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isUsername(CharSequence input) { return isMatch(REGEX_USERNAME, input); } /** * 验证yyyy-MM-dd格式的日期校验,已考虑平闰年 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isDate(CharSequence input) { return isMatch(REGEX_DATE, input); } /** * 验证IP地址 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIP(CharSequence input) { return isMatch(REGEX_IP, input); } /** * 判断是否匹配正则 * * @param regex 正则表达式 * @param input 要匹配的字符串 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMatch(String regex, CharSequence input) { return input != null && input.length() > 0 && Pattern.matches(regex, input); } /** * 获取正则匹配的部分 * * @param regex 正则表达式 * @param input 要匹配的字符串 * @return 正则匹配的部分 */ public static List<String> getMatches(String regex, CharSequence input) { if (input == null) return null; List<String> matches = new ArrayList<>(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); while (matcher.find()) { matches.add(matcher.group()); } return matches; } /** * 获取正则匹配分组 * * @param input 要分组的字符串 * @param regex 正则表达式 * @return 正则匹配分组 */ public static String[] getSplits(String input, String regex) { if (input == null) return null; return input.split(regex); } /** * 替换正则匹配的第一部分 * * @param input 要替换的字符串 * @param regex 正则表达式 * @param replacement 代替者 * @return 替换正则匹配的第一部分 */ public static String getReplaceFirst(String input, String regex, String replacement) { if (input == null) return null; return Pattern.compile(regex).matcher(input).replaceFirst(replacement); } /** * 替换所有正则匹配的部分 * * @param input 要替换的字符串 * @param regex 正则表达式 * @param replacement 代替者 * @return 替换所有正则匹配的部分 */ public static String getReplaceAll(String input, String regex, String replacement) { if (input == null) return null; return Pattern.compile(regex).matcher(input).replaceAll(replacement); } }

终点站

相信乘客们都没有见到相应的正则表达式,因为我做了import static com.blankj.utilcode.utils.ConstUtils.*;,相关常量都在这个常量工具类里,下面贴出相关代码。

复制代码
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
/******************** 正则相关常量 ********************/ /** * 正则:手机号(简单) */ public static final String REGEX_MOBILE_SIMPLE = "^[1]\d{10}$"; /** * 正则:手机号(精确) * <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p> * <p>联通:130、131、132、145、155、156、175、176、185、186</p> * <p>电信:133、153、173、177、180、181、189</p> * <p>全球星:1349</p> * <p>虚拟运营商:170</p> */ public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\d{8}$"; /** * 正则:电话号码 */ public static final String REGEX_TEL = "^0\d{2,3}[- ]?\d{7,8}"; /** * 正则:身份证号码15位 */ 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}$"; /** * 正则:身份证号码18位 */ 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])$"; /** * 正则:邮箱 */ public static final String REGEX_EMAIL = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"; /** * 正则:URL */ public static final String REGEX_URL = "[a-zA-z]+://[^\s]*"; /** * 正则:汉字 */ public static final String REGEX_ZH = "^[\u4e00-\u9fa5]+$"; /** * 正则:用户名,取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位 */ public static final String REGEX_USERNAME = "^[\w\u4e00-\u9fa5]{6,20}(?<!_)$"; /** * 正则:yyyy-MM-dd格式的日期校验,已考虑平闰年 */ public static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"; /** * 正则:IP地址 */ 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?)"; /************** 以下摘自http://tool.oschina.net/regex **************/ /** * 正则:双字节字符(包括汉字在内) */ public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\x00-\xff]"; /** * 正则:空白行 */ public static final String REGEX_BLANK_LINE = "\n\s*\r"; /** * 正则:QQ号 */ public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}"; /** * 正则:中国邮政编码 */ public static final String REGEX_ZIP_CODE = "[1-9]\d{5}(?!\d)"; /** * 正则:正整数 */ public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\d*$"; /** * 正则:负整数 */ public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\d*$"; /** * 正则:整数 */ public static final String REGEX_INTEGER = "^-?[1-9]\d*$"; /** * 正则:非负整数(正整数 + 0) */ public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\d*|0$"; /** * 正则:非正整数(负整数 + 0) */ public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\d*|0$"; /** * 正则:正浮点数 */ public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$"; /** * 正则:负浮点数 */ public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\d*\.\d*|-0\.\d*[1-9]\d*$"; /************** If u want more please visit http://toutiao.com/i6231678548520731137/ **************/

如果需要更多的正则表达式,看以上代码应该也能找到相关站点了吧,好了,终点站到了,如果对本次旅途满意的话,请给五星好评哦,毕竟老司机牺牲了很多时间才换来这么一份工具类,如果该工具类依赖其他工具类,都可以在我的Android开发人员不得不收集的代码(持续更新中)中找到。

最后

以上就是犹豫黑米最近收集整理的关于工具类之RegexUtils(正则工具类)的全部内容,更多相关工具类之RegexUtils(正则工具类)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部