我是靠谱客的博主 傲娇战斗机,这篇文章主要介绍RegexUtils(一个正则表达式验证工具类),现在分享给大家,希望可以做个参考。

用途:用来处理字符串的,判断其是否属于某种文本
功能:
1.验证电话号码:isTel
2.验证身份证号码18位:isIDCard18
3.验证手机号(简单):isMobileSimple
4.验证手机号(精确):isMobileExact
5.验证邮箱:isEmail
6.验证URL:isURL
7.验证汉字:isChinesec
8.验证yyyy-MM-dd格式的日期校验:isDate
9.验证IP地址:isIP
10.判断是否匹配正则:estimate

复制代码
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
public class RegexUtils { private RegexUtils() { } /** * 验证电话号码 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isTel(CharSequence input) { return estimate("^0\d{2,3}[- ]?\d{7,8}", input); } /** * 验证身份证号码18位 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIDCard18(CharSequence input) { return estimate("^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9Xx])$", input); } /** * 验证手机号(简单) * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileSimple(CharSequence input) { return estimate("^[1]\d{10}$", input); } /** * 验证手机号(精确) * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileExact(CharSequence input) { return estimate("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\d{8}$", input); } /** * 验证邮箱 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isEmail(CharSequence input) { return estimate("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", input); } /** * 验证URL * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isURL(CharSequence input) { return estimate("[a-zA-z]+://[^\s]*", input); } /** * 验证汉字 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isChinesec(CharSequence input) { return estimate("^[\u4e00-\u9fa5]+$", input); } /** * 验证yyyy-MM-dd格式的日期校验 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isDate(CharSequence input) { return estimate("^(?:(?!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)$", input); } /** * 验证IP地址 * * @param input 待验证文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIP(CharSequence input) { return estimate("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)", input); } /** * 判断是否匹配正则 * * @param regex 正则表达式 * @param input 要匹配的字符串 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean estimate(String regex, CharSequence input) { return input != null && input.length() > 0 && Pattern.matches(regex, input); } }

最后

以上就是傲娇战斗机最近收集整理的关于RegexUtils(一个正则表达式验证工具类)的全部内容,更多相关RegexUtils(一个正则表达式验证工具类)内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部