复制代码
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
38public 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; } }
转载于:https://www.cnblogs.com/bwlcool/p/8575363.html
最后
以上就是复杂人生最近收集整理的关于正则表达式判断身份证和提取生日的全部内容,更多相关正则表达式判断身份证和提取生日内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复