本文实例讲述了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
|
package com.hooypay.tool; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Check { // 判断电话 public static boolean isTelephone(String phonenumber) { String phone = "0\\d{2,3}-\\d{7,8}" ; Pattern p = Pattern.compile(phone); Matcher m = p.matcher(phonenumber); return m.matches(); } // 判断手机号 public static boolean isMobileNO(String mobiles) { Pattern p = Pattern.compile( "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$" ); Matcher m = p.matcher(mobiles); return m.matches(); } // 判断邮箱 public static boolean isEmail(String email) { String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$" ; Pattern p = Pattern.compile(str); Matcher m = p.matcher(email); return m.matches(); } // 判断日期格式:yyyy-mm-dd public static boolean isValidDate(String sDate) { String datePattern1 = "\\d{4}-\\d{2}-\\d{2}" ; String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))" + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|" + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?" + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?(" + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?" + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))" ; if ((sDate != null )) { Pattern pattern = Pattern.compile(datePattern1); Matcher match = pattern.matcher(sDate); if (match.matches()) { pattern = Pattern.compile(datePattern2); match = pattern.matcher(sDate); return match.matches(); } else { return false ; } } return false ; } //验证金额 public static boolean isNumber(String str) { java.util.regex.Pattern pattern=java.util.regex.Pattern.compile( "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$" ); // 判断小数点后一位的数字的正则表达式 java.util.regex.Matcher match=pattern.matcher(str); if (match.matches()== false ) { return false ; } else { return true ; } } } |
希望本文所述对大家java程序设计有所帮助。