forked from luyan/epmet-cloud-lingshan
				
			
				 31 changed files with 381 additions and 81 deletions
			
			
		| @ -0,0 +1,27 @@ | |||||
|  | package com.epmet.commons.tools.enums; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * 唯一整件类型 | ||||
|  |  */ | ||||
|  | public enum IdCardTypeEnum { | ||||
|  | 
 | ||||
|  |     OTHERS("0", "其他"), | ||||
|  |     SFZH("1", "身份证号"), | ||||
|  |     PASSPORT("2", "护照"); | ||||
|  | 
 | ||||
|  |     private String type; | ||||
|  |     private String name; | ||||
|  | 
 | ||||
|  |     IdCardTypeEnum(String type, String name) { | ||||
|  |         this.type = type; | ||||
|  |         this.name = name; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public String getType() { | ||||
|  |         return type; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public String getName() { | ||||
|  |         return name; | ||||
|  |     } | ||||
|  | } | ||||
| @ -0,0 +1,133 @@ | |||||
|  | package com.epmet.commons.tools.utils; | ||||
|  | 
 | ||||
|  | import com.epmet.commons.tools.enums.IdCardTypeEnum; | ||||
|  | import lombok.AllArgsConstructor; | ||||
|  | import lombok.Data; | ||||
|  | import lombok.NoArgsConstructor; | ||||
|  | 
 | ||||
|  | import java.util.regex.Matcher; | ||||
|  | import java.util.regex.Pattern; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * 唯一整件正则工具 | ||||
|  |  */ | ||||
|  | public class IdCardRegexUtils { | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 15位身份证号的正则表达式 | ||||
|  |      */ | ||||
|  |     private static final Pattern PATTERN_15_ID = Pattern.compile("^\\d{6}(?<year>\\d{2})(?<month>0[1-9]|1[0-2])(?<day>[0-2][0-9]|3[0-1])\\d{2}(?<sex>\\d)$"); | ||||
|  |     /** | ||||
|  |      * 18位身份证号的正则表达式 | ||||
|  |      */ | ||||
|  |     private static final Pattern PATTERN_18_ID = Pattern.compile("^\\d{6}(?<year>\\d{4})(?<month>0[1-9]|1[0-2])(?<day>[0-2][0-9]|3[0-1])\\d{2}(?<sex>\\d)[0-9a-xA-X]$"); | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 9位护照 | ||||
|  |      */ | ||||
|  |     private static final Pattern PATTERN_9_PASSPORT = Pattern.compile("^[a-zA-Z]{2}\\d{7}$|^[a-zA-Z]{1}\\d{8}$"); | ||||
|  | 
 | ||||
|  |     private String inputText; | ||||
|  | 
 | ||||
|  |     private Matcher matcher; | ||||
|  | 
 | ||||
|  |     private IdCardTypeEnum idCardType; | ||||
|  | 
 | ||||
|  |     private IdCardRegexUtils(IdCardTypeEnum idCardType, Matcher matcher, String inputText) { | ||||
|  |         this.idCardType = idCardType; | ||||
|  |         this.matcher = matcher; | ||||
|  |         this.inputText = inputText; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 正则解析结果 | ||||
|  |      */ | ||||
|  |     @Data | ||||
|  |     @AllArgsConstructor | ||||
|  |     @NoArgsConstructor | ||||
|  |     public static class ParsedContent { | ||||
|  |         private String birthdayYear; | ||||
|  |         private String birthdayMonth; | ||||
|  |         private String birthdayDay; | ||||
|  |         private String sex; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * desc:校验输入的证件号是否合法 | ||||
|  |      * @param input | ||||
|  |      * @return | ||||
|  |      */ | ||||
|  |     public static boolean validateIdCard(String input){ | ||||
|  |         IdCardRegexUtils parse = IdCardRegexUtils.parse(input); | ||||
|  |         return parse != null; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 解析正则 | ||||
|  |      * @param input | ||||
|  |      * @return | ||||
|  |      */ | ||||
|  |     public static IdCardRegexUtils parse(String input) { | ||||
|  |         if (input == null || input.trim().length() == 0) { | ||||
|  |             return null; | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         if (input.length() == 15) { | ||||
|  |             Matcher matcher = PATTERN_15_ID.matcher(input); | ||||
|  |             if (matcher.matches()) { | ||||
|  |                 return new IdCardRegexUtils(IdCardTypeEnum.SFZH, matcher, input); | ||||
|  |             } | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         if (input.length() == 18) { | ||||
|  |             Matcher matcher = PATTERN_18_ID.matcher(input); | ||||
|  |             if (matcher.matches()) { | ||||
|  |                 return new IdCardRegexUtils(IdCardTypeEnum.SFZH, matcher, input); | ||||
|  |             } | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         if (input.length() == 9) { | ||||
|  |             Matcher matcher = PATTERN_9_PASSPORT.matcher(input); | ||||
|  |             if (matcher.matches()) { | ||||
|  |                 return new IdCardRegexUtils(IdCardTypeEnum.PASSPORT, matcher, input); | ||||
|  |             } | ||||
|  |         } | ||||
|  |         return null; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 获取解析结果 | ||||
|  |      * @return | ||||
|  |      */ | ||||
|  |     public ParsedContent getParsedResult() { | ||||
|  |         if (matcher == null || idCardType == null) { | ||||
|  |             return null; | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         if (IdCardTypeEnum.SFZH == idCardType) { | ||||
|  |             //是身份证号,可以解析
 | ||||
|  |             String year; | ||||
|  |             if (inputText.length() == 15) { | ||||
|  |                 // 15位身份证号,years前需要拼上19
 | ||||
|  |                 year = "19".concat(matcher.group("year")); | ||||
|  |             } else { | ||||
|  |                 year = matcher.group("year"); | ||||
|  |             } | ||||
|  |             String month = matcher.group("month"); | ||||
|  |             String day = matcher.group("day"); | ||||
|  |             String sex = matcher.group("sex"); | ||||
|  |             return new ParsedContent(year, month, day, sex); | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         // 其他类型暂时不可解析
 | ||||
|  |         return null; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     /** | ||||
|  |      * 获取类型枚举 | ||||
|  |      * @return | ||||
|  |      */ | ||||
|  |     public IdCardTypeEnum getTypeEnum() { | ||||
|  |         return idCardType; | ||||
|  |     } | ||||
|  | } | ||||
								
									Binary file not shown.
								
							
						
					
					Loading…
					
					
				
		Reference in new issue