public class OtherTest {
public static void main(String[] args) {
// 邮箱
// @出现一次 .最多2次
String repx = "\\w+@([\\w]+[\\w-]*)(\\.[\\w]+[-\\w]*)+";
String mail = "nvdiaosi@a.com.xx";
if (youxiang.matches(repx)) {
System.out.println("匹配成功");
} else {
System.out.println("匹配不成功!");
}
/**
* 验证手机号码
*
* 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147、182
* 联通号码段:130、131、132、136、185、186、145
* 电信号码段:133、153、180、189、177
*/
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,5-9])|(177))\\d{8}$";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(cellphone);
System.out.println(matcher.find());
/**
* 验证固话号码
*
*/
String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(telephone);
System.out.println(matcher.matches());
/*对QQ号码进行校验
要求5~15位,不能以0开头,只能是数字*/
String qq = "23412312";
String regex = "[1-9][0-9]{4,14}";//第一位1-9之间的数字,第二位0-9之间的数字,数字范围4-14个之间
//String regex2 = "[1-9]\\d{4,14}";//此句也可以
boolean flag = qq.matches(regex);
if(flag)
System.out.println("QQ号正确");
else
System.out.println("QQ号错误");
/**只能输入数字:"^[0-9]*$" */
String patternStr1 = "^\\d+$";
Pattern pattern1 = Pattern.compile(patternStr1);
String testStr1 = "89。6";
Matcher matcher1 = pattern1.matcher(testStr1);
System.out.println(matcher1.find());//false
/**只能输入n位的数字:"^\d{n}$" */
String patternStr2 = "^\\d{3}$";
Pattern pattern2 = Pattern.compile(patternStr2);
String testStr2 = "896";
Matcher matcher2 = pattern2.matcher(testStr2);
System.out.println(matcher2.find());//true
/**只能输入至少n位的数字:"^\d{n,}$" */
String patternStr3 = "^\\d{3,}$";
Pattern pattern3 = Pattern.compile(patternStr3);
String testStr3 = "89";
Matcher matcher3 = pattern3.matcher(testStr3);
System.out.println(matcher3.find());//false
/**只能输入[m,n]位的数字:。"^\d{m,n}$" */
String patternStr4 = "^\\d{3,5}$";
Pattern pattern4 = Pattern.compile(patternStr4);
String testStr4 = "895556";
Matcher matcher4 = pattern4.matcher(testStr4);
System.out.println(matcher4.find());//false
/**只能输入整数或者有两位小数的正实数:"^[0-9]+(\.[0-9]{2})?$" */
String patternStr5 = "^\\d+(\\.\\d{2})?$";
Pattern pattern5 = Pattern.compile(patternStr5);
String testStr5 = "89556";
Matcher matcher5 = pattern5.matcher(testStr5);
System.out.println(matcher5.find());//true
/**只能输入整数或者有1~3位小数的正实数:"^[0-9]+(\.[0-9]{1,3})?$" */
String patternStr6 = "^\\d+(\\.\\d{1,3})?$";
Pattern pattern6 = Pattern.compile(patternStr6);
String testStr6 = "8.9556";
Matcher matcher6 = pattern6.matcher(testStr6);
System.out.println(matcher6.find());//false
/**只能输入非零的正整数:"^\+?[1-9][0-9]*$" */
String patternStr7 = "^\\+?[1-9]\\d*$";
Pattern pattern7 = Pattern.compile(patternStr7);
String testStr7 = "+89556";
Matcher matcher7 = pattern7.matcher(testStr7);
System.out.println(matcher7.find());//true
/**只能输入非零的负整数:"^\-[1-9][0-9]*$" */
String patternStr8 = "^\\-[1-9]\\d*$";
Pattern pattern8 = Pattern.compile(patternStr8);
String testStr8 = "89556";
Matcher matcher8 = pattern8.matcher(testStr8);
System.out.println(matcher8.find());//false
/**只能输入长度为3的字符:"^.{3}$","."表示 任何字符,不需要转义*/
String patternStr9 = "^.{3}$";
Pattern pattern9 = Pattern.compile(patternStr9);
String testStr9 = "8a9";
Matcher matcher9 = pattern9.matcher(testStr9);
System.out.println(matcher9.find());//true
/** 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"*/
String patternStr10 = "^[a-z|A-Z]+$";
Pattern pattern10 = Pattern.compile(patternStr10);
String testStr10 = "uiA.";
Matcher matcher10 = pattern10.matcher(testStr10);
System.out.println(matcher10.find());//false
/**只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"*/
String patternStr11 = "^[A-Za-z0-9]+$";
Pattern pattern11 = Pattern.compile(patternStr11);
String testStr11 = "uiA23jk";
Matcher matcher11 = pattern11.matcher(testStr11);
System.out.println(matcher11.find());//true
/**只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$" */
String patternStr12 = "^\\w+$";
Pattern pattern12 = Pattern.compile(patternStr12);
String testStr12 = "uiA23jk_io";
Matcher matcher12 = pattern12.matcher(testStr12);
System.out.println(matcher12.find());//true
/**验证用户密码:以字母或下划线开头,只能包含字符、数字和下划线,长度在6~18之间:"^[a-zA-Z_]\w{5,17}$" */
String patternStr13 = "^[A-Za-z_]\\w{5,17}$";
Pattern pattern13 = Pattern.compile(patternStr13);
String testStr13 = "_90uiA23jk_io";
Matcher matcher13 = pattern13.matcher(testStr13);
System.out.println(matcher13.find());//true
/**只能输入汉字:"^[\u4e00-\u9fa5]{0,}$" */
String patternStr14 = "^[\\u4e00-\\u9fa5]+$";
Pattern pattern14 = Pattern.compile(patternStr14);
String testStr14 = "武即可汉";
Matcher matcher14 = pattern14.matcher(testStr14);
System.out.println(matcher14.find());//true
/**验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" */
String patternStr15 = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
Pattern pattern15 = Pattern.compile(patternStr15);
String testStr15 = "226461750@163.com";
Matcher matcher15 = pattern15.matcher(testStr15);
System.out.println(matcher15.find());//true
/**验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$" */
String patternStr16 = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w\\-\\./?%&=]*)?$";
Pattern pattern16 = Pattern.compile(patternStr16);
String testStr16 = "http://www.baidu.com";
Matcher matcher16 = pattern16.matcher(testStr16);
System.out.println(matcher16.find());//true
}
}
public static boolean isIDNumber(String IDNumber) {
if (IDNumber == null || "".equals(IDNumber)) {
return false;
}
// 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母)
String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
//假设18位身份证号码:41000119910101123X 410001 19910101 123X
//^开头
//[1-9] 第一位1-9中的一个 4
//\\d{5} 五位数字 10001(前六位省市县地区)
//(18|19|20) 19(现阶段可能取值范围18xx-20xx年)
//\\d{2} 91(年份)
//((0[1-9])|(10|11|12)) 01(月份)
//(([0-2][1-9])|10|20|30|31)01(日期)
//\\d{3} 三位数字 123(第十七位奇数代表男,偶数代表女)
//[0-9Xx] 0123456789Xx其中的一个 X(第十八位为校验值)
//$结尾
//假设15位身份证号码:410001910101123 410001 910101 123
//^开头
//[1-9] 第一位1-9中的一个 4
//\\d{5} 五位数字 10001(前六位省市县地区)
//\\d{2} 91(年份)
//((0[1-9])|(10|11|12)) 01(月份)
//(([0-2][1-9])|10|20|30|31)01(日期)
//\\d{3} 三位数字 123(第十五位奇数代表男,偶数代表女),15位身份证不含X
//$结尾
boolean matches = IDNumber.matches(regularExpression);
//判断第18位校验值
if (matches) {
if (IDNumber.length() == 18) {
try {
char[] charArray = IDNumber.toCharArray();
//前十七位加权因子
int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//这是除以11后,可能产生的11位余数对应的验证码
String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
int sum = 0;
for (int i = 0; i < idCardWi.length; i++) {
int current = Integer.parseInt(String.valueOf(charArray[i]));
int count = current * idCardWi[i];
sum += count;
}
char idCardLast = charArray[17];
int idCardMod = sum % 11;
if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
return true;
} else {
System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase() +
"错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
return false;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("异常:" + IDNumber);
return false;
}
}
}
return matches;
}