18位 2代身份证
(^[1-9]\\d{5})([1-9]\\d{3})((0[1-9])|(10|11|12))(([0-2][1-9])|30|31)(\\d{3})([0-9Xx]$)
18位 2代身份证 xxxxxx yyyy MM dd xxx x-->校验码 [1-6]: [1-9]\\d{5} //省市区 [7-14]: ([1-9]\\d{3})((0[1-9])|(10|11|12))(([0-2][1-9])|30|31) //出生年月日 [15-17]: \\d{3} //同一地址码所标示的区域范围内对同年同月同日生人编订的顺序码, [17]性别,奇男偶女, [18]: \\d{3})([0-9Xx] //校验码 [1-9]\\d(5}:不能0开头,匹配后续5位 闰年不能禁止29+
15位 1代身份证
(^[1-9]\\d{5})(\\d{2})((0[1-9])|(10|11|12))(([0-2][1-9])|30|31)(\\d{3}$)
1代 15位 xxxxxx yy MM dd xxx [1-6]: [1-9]\d{5} //省市区, [7-12]: (\\d{2})((0[1-9])|(10|11|12)) //出生年月日 [12-15]: \\d{3} //同一地址码所标示的区域范围内对同年同月同日生人编订的顺序码, [15]性别,奇男偶女, //闰年不能禁止29+
栗子:
String id = "311253208001311249";//2代
String id1 = "311253000131124";//1代
Boolean matches3 = id.matches("(^[1-9]\\d{5}) //省市区
([1-9]\\d{3})((0[1-9])|(18|11|12))(([0-2][1-9])|30|31)//年月日
(\\d{3}) //顺序码
([0-9Xx]$)"); //标识码
Boolean matches4 = id1.matches("(^[1-9]\\d{5})
(\\d{2})((0[1-9])|(10|11|12))(([0-2][1-9])|30|31)
(\\d{3}$)");
System.out.println(matches3);//true
System.out.println(matches4);//true