【正则表达式】判断身份证号
作者:互联网
//假设18位身份证号码:41000119910101123X 410001 19910101 123X //^开头 //11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", //37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: //"青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"}; //\\d{4} 4位数字 0101(前六位省市县地区) //(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 //$结尾 public boolean isIDNumber(String IDNumber) { if (IDNumber == null || "".equals(IDNumber)||IDNumber.length()>18) { return false; } //1.判断是否为18位 if(IDNumber.length()>=18||IDNumber.length()==15) { // 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母) String regularExpression = "(^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)" + "\\d{4}(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}$)"; 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; }else { if(IDNumber.length()>=10) { String regularExpression = "(^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4}(18|19|20)\\d{2}\\d{0,}$)"; boolean matches = IDNumber.matches(regularExpression); return matches; }else if(IDNumber.length()>=6&&IDNumber.length()<10){ String regularExpression = "(^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4}([0-9]{0,})$)"; boolean matches = IDNumber.matches(regularExpression); return matches; }else { return false; } } }
标签:11,10,判断,20,正则表达式,18,身份证号,IDNumber,length 来源: https://www.cnblogs.com/su-zhifei/p/11102664.html