正则表达式判断身份证和提取生日
public class IDCardUtil { public static boolean isValidIdCard(String idCard) { if (idCard == null) { return false; } Pattern p = Pattern.compile("(\\d{17}[0-9a-zA-Z]|\\d{14}[0-9a-zA-Z])"); return p.matcher(idCard).matches(); } public static String getBirthday(String idCard) { if (idCard == null) { return null; } Pattern p1 = Pattern.compile("\\d{6}(\\d{8}).*"); // 用于提取出生日字符串 Pattern p2 = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");// 用于将生日字符串进行分解为年月日 Matcher matcher = p1.matcher(idCard); if (matcher.find()) { String birthday = matcher.group(1); Matcher matcher2 = p2.matcher(birthday); if (matcher2.find()) { StringBuilder sb = new StringBuilder(); sb.append(matcher2.group(1)); sb.append('-'); sb.append(matcher2.group(2)); sb.append('-'); sb.append(matcher2.group(3)); return sb.toString(); } } return null; } }
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03