本文實(shí)例講述了java實(shí)現(xiàn)的正則工具類。分享給大家供大家參考。具體如下:
這里實(shí)現(xiàn)的正則工具類適用于:正則電話號(hào)碼、郵箱、QQ號(hào)碼、QQ密碼、手機(jī)號(hào)
java代碼如下:
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
|
package com.zhanggeng.contact.tools; /** * RegexTool is used to regex the string ,such as : phone , qq , password , email . * * @author ZHANGGeng * @version v1.0.1 * @since JDK5.0 * */ public class RegexTool { /** * * @param phoneNum 傳入的參數(shù)僅僅是一個(gè)電話號(hào)碼時(shí),調(diào)用此方法 * @return 如果匹配正確,return true , else return else */ //如果傳進(jìn)來(lái)的是電話號(hào)碼,則對(duì)電話號(hào)碼進(jìn)行正則匹配 public static boolean regexPhoneNumber(String phoneNum){ //電話號(hào)碼匹配結(jié)果 boolean isPhoneNum_matcher = phoneNum.matches( "1[358]\\d{9}" ); //如果isPhoneNum_matcher is true , 則return true , else return false if (isPhoneNum_matcher) return true ; return false ; } /** * * @param email 傳入的參數(shù)僅僅是一個(gè)郵箱地址時(shí),調(diào)用此方法 * @return 如果匹配正確,return true , else return false */ //如果傳進(jìn)來(lái)的是郵箱地址,則對(duì)郵箱進(jìn)行正則匹配 public static boolean regexEmailAddress(String email){ //郵箱匹配結(jié)果 boolean isEmail_matcher = email.matches( "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,}){1,3}" ); //如果isEmail_matcher value is true , 則 return true , else return false if (isEmail_matcher) return true ; return false ; } /** * * @param phoneNum 傳入的電話號(hào)碼 * @param email 傳入的郵箱地址 * @return 如果匹配正確,return true , else return false */ public static boolean regexEmailAddressAndPhoneNum(String phoneNum , String email){ //電話號(hào)碼匹配結(jié)果 boolean isPhoneNum_matcher = phoneNum.matches( "1[358]\\d{9}" ); //郵箱匹配結(jié)果 boolean isEmail_matcher = email.matches( "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,}){1,3}" ); //matcher value is true , 則 return true , else return false if (isEmail_matcher && isPhoneNum_matcher){ return true ; } return false ; } /** * * @param qqNum 傳入的QQ * @return 如果匹配正確,return true, else return false */ public static boolean regexQQNumber(String qqNum){ //QQ號(hào)匹配結(jié)果 boolean isQQNum_matcher = qqNum.matches( "[1-9]\\d{2,11}" ); if (isQQNum_matcher) return true ; return false ; } /** * * @param pwd 傳入的是 密碼 * @return 如果匹配正確,滿足密碼規(guī)則,return true, else return false */ public static boolean regexPassWord(String pwd){ //密碼匹配結(jié)果 boolean isPassWord_matcher = pwd.matches( "[0-9a-zA-Z_@$@]{6,12}" ); if (isPassWord_matcher) return true ; return false ; } } |
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。