本文實(shí)例為大家分享了java正則表達(dá)式工具類的具體代碼,供大家參考,具體內(nèi)容如下
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
|
import com.google.common.base.Strings; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 常用的正則表達(dá)式 * Created by tookbra on 2016/4/7. */ public class RegexUtils { /** * 判斷是否是正確的IP地址 * * @param ip * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isIp(String ip) { if (Strings.isNullOrEmpty(ip)) return false ; String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$" ; return ip.matches(regex); } /** * 判斷是否是正確的郵箱地址 * * @param email * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isEmail(String email) { if (Strings.isNullOrEmpty(email)) return false ; String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*" ; return email.matches(regex); } /** * 判斷是否含有中文,僅適合中國(guó)漢字,不包括標(biāo)點(diǎn) * @param text * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isChinese(String text) { if (Strings.isNullOrEmpty(text)) return false ; Pattern p = Pattern.compile( "[\u4e00-\u9fa5]" ); Matcher m = p.matcher(text); return m.find(); } /** * 判斷是否正整數(shù) * * @param number * 數(shù)字 * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isNumber(String number) { if (Strings.isNullOrEmpty(number)) return false ; String regex = "[0-9]*" ; return number.matches(regex); } /** * 判斷幾位小數(shù)(正數(shù)) * * @param decimal * 數(shù)字 * @param count * 小數(shù)位數(shù) * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isDecimal(String decimal, int count) { if (Strings.isNullOrEmpty(decimal)) return false ; String regex = "^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){" + count + "})?$" ; return decimal.matches(regex); } /** * 判斷是否是移動(dòng)手機(jī)號(hào)碼 * * @param phoneNumber * 移動(dòng)手機(jī)號(hào)碼 * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isMobilePhoneNumber(String phoneNumber) { if (Strings.isNullOrEmpty(phoneNumber)) return false ; String regex = "^((13[0-9])|(15[0-9])|(18[1-9]))\\d{8}$" ; return phoneNumber.matches(regex); } /** * 判斷是否是手機(jī)號(hào)碼 * * @param phoneNumber * 移動(dòng)手機(jī)號(hào)碼 * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean isPhoneNumber(String phoneNumber) { if (Strings.isNullOrEmpty(phoneNumber)) return false ; String regex = "^1\\d{10}$" ; return phoneNumber.matches(regex); } /** * 判斷是否含有特殊字符 * * @param text * @return boolean true,通過(guò),false,沒(méi)通過(guò) */ public static boolean hasSpecialChar(String text) { if (Strings.isNullOrEmpty(text)) return false ; if (text.replaceAll( "[a-z]*[A-Z]*\\d*-*_*\\s*" , "" ).length() == 0 ) { // 如果不包含特殊字符 return true ; } return false ; } private static boolean isChinese( char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true ; } return false ; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/YuyuanNo1/p/8034214.html