2 changed files with 173 additions and 51 deletions
			
			
		@ -0,0 +1,123 @@ | 
				
			|||||
 | 
					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("^\\w{2}\\d{7}$|^\\w{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; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 解析正则 | 
				
			||||
 | 
					     * @param input | 
				
			||||
 | 
					     * @return | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static IdCardRegexUtils parse(String input) { | 
				
			||||
 | 
					        if (input == null) { | 
				
			||||
 | 
					            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; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue