营业执照统一社会信用代码Java正则表达式

常用正则表达式验证工具
本文介绍了一款用于验证手机号、电话号码、身份证号码、营业执照注册号等常见信息的正则表达式工具。该工具提供了多种验证方法,确保输入信息的准确性与合规性。
/**
 * <P>ValidatorUtils</P>
 * 
 * @version 1.0
 * @author wangpf
 * @date 2018年8月8日 下午3:11:27
 */
public class ValidatorUtils {
    
    /** 
     * 手机号验证 
     * @param  phone
     * @return 验证通过返回true 
     */  
    public static boolean isMobile(String phone) {
    	if(StringUtils.isEmpty(phone)) {
    		return false;
    	}
    	
    	String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if(phone.length() != 11) {
            return false;
        }else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }
    
    /**
     * 电话号码验证
     * @param phone
     * @return 验证通过返回true
     */
    public static boolean isPhone(String phone) {
        Pattern p1 = null, p2 = null;
        Matcher m = null;
        boolean b = false;
        p1 = Pattern.compile("^[0][0-9]{2,3}-?[0-9]{5,10}$");  // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
        if (phone.length() > 9) {
           m = p1.matcher(phone);
           b = m.matches();
        } else {
           m = p2.matcher(phone);
           b = m.matches();
        }
        return b;
    }
    
    /**  
     * 我国公民的身份证号码特点如下
     * 1.长度18位
     * 2.第1-17号只能为数字
     * 3.第18位只能是数字或者x
     * 4.第7-14位表示特有人的年月日信息
     * 请实现身份证号码合法性判断的函数,函数返回值:
     * 1.如果身份证合法返回0
     * 2.如果身份证长度不合法返回1
     * 3.如果第1-17位含有非数字的字符返回2
     * 4.如果第18位不是数字也不是x返回3
     * 5.如果身份证号的出生日期非法返回4
     */
    public static boolean validator(String id) {
        String str = "[1-9]{2}[0-9]{4}(19|20)[0-9]{2}"
                + "((0[1-9]{1})|(1[1-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))"
                + "[0-9]{3}[0-9x]{1}";
        Pattern pattern = Pattern.compile(str);
        return pattern.matcher(id).matches();
    }
    
    /**
     * 营业执照 统一社会信用代码(15位)
     * @param license
     * @return
     */
    public static boolean isLicense15(String license) {
        if(StringUtils.isEmpty(license)) {
        	return false;
        }
        if(license.length() != 15) {
			return false;
		}
        
        String businesslicensePrex14 = license.substring(0,14);// 获取营业执照注册号前14位数字用来计算校验码
        String businesslicense15 = license.substring(14, license.length());// 获取营业执照号的校验码
        char[] chars = businesslicensePrex14.toCharArray();
        int[] ints = new int[chars.length];
        for(int i=0; i<chars.length;i++) {
            ints[i] = Integer.parseInt(String.valueOf(chars[i]));
        }
        getCheckCode(ints);
        if(businesslicense15.equals(getCheckCode(ints)+"")) {// 比较 填写的营业执照注册号的校验码和计算的校验码是否一致
            return true;
        }
        return false;
    }
 
    /**
     * 获取 营业执照注册号的校验码
     * @param ints
     * @return
     */
    private static int getCheckCode(int[] ints) {
        if(null != ints && ints.length > 1) {
            int ti = 0;
            int si = 0;// pi|11+ti
            int cj = 0;// (si||10==0?10:si||10)*2
            int pj = 10;// pj=cj|11==0?10:cj|11
            for (int i=0;i<ints.length;i++) {
                ti = ints[i];
                pj = (cj % 11) == 0 ? 10 : (cj % 11);
                si = pj + ti;
                cj = (0 == si % 10 ? 10 : si % 10) * 2;
                if (i == ints.length-1) {
                    pj = (cj % 11) == 0 ? 10 : (cj % 11);
                    return pj == 1 ? 1 : 11 - pj;
                }
            }
        }// end if
        return -1;
    }
    
    /**
     * 营业执照 统一社会信用代码(18位)
     * @param license
     * @return
     */
    public static boolean isLicense18(String license) {
    	if(StringUtils.isEmpty(license)) {
        	return false;
        }
		if(license.length() != 18) {
			return false;
		}
		
		String regex = "^([159Y]{1})([1239]{1})([0-9ABCDEFGHJKLMNPQRTUWXY]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-90-9ABCDEFGHJKLMNPQRTUWXY])$";
		if (!license.matches(regex)) {
			return false;
		}
		String str = "0123456789ABCDEFGHJKLMNPQRTUWXY";
		int[] ws = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 };
		String[] codes = new String[2];
		codes[0] = license.substring(0, license.length() - 1);
		codes[1] = license.substring(license.length() - 1, license.length());
		int sum = 0;
		for (int i = 0; i < 17; i++) {
			sum += str.indexOf(codes[0].charAt(i)) * ws[i];
		}
		int c18 = 31 - (sum % 31);
		if (c18 == 31) {
			c18 = 'Y';
		} else if (c18 == 30) {
			c18 = '0';
		}
		if (str.charAt(c18) != codes[1].charAt(0)) {
			return false;
		}
		return true;
	}
}

 

### 中国统一社会信用代码脱敏正则表达式Java适用) 中国统一社会信用代码由18位数字、字母组合构成,通常格式为:前2位为登记管理部门代码,接下来是9位主体标识码(组织机构代码),最后是4位登记管理机关行政区划码和1位校验码。为在Java环境中实现脱敏,可以使用正则表达式匹配该格式,并将其中部分字段替换为星号(*)以实现数据脱敏。 一种常见的脱敏策略是保留前6位和后4位,中间8位使用星号替代。适用于该策略的正则表达式为: ```regex ^(\w{6})(\w{8})(\w{4})$ ``` 在Java中,可以使用如下代码实现脱敏逻辑: ```java public class CreditCodeDesensitizer { public static String desensitizeCreditCode(String creditCode) { if (creditCode == null || !creditCode.matches("^[A-Za-z0-9]{18}$")) { throw new IllegalArgumentException("统一社会信用代码必须为18位字母或数字"); } return creditCode.replaceAll("^([A-Za-z0-9]{6})([A-Za-z0-9]{8})([A-Za-z0-9]{4})$", "$1********$3"); } public static void main(String[] args) { String creditCode = "91310115MA1K3YJ123"; String maskedCode = desensitizeCreditCode(creditCode); System.out.println("原始信用代码:" + creditCode); System.out.println("脱敏后信用代码:" + maskedCode); } } ``` 该正则表达式匹配18位的字母和数字组合,并将其中的第7到14位进行隐藏。这种方式在数据展示时保留了部分信息以供识别,同时保护了敏感字段[^3]。 正则表达式的设计应确保输入数据的合法性,例如检查是否为18位、是否包含非法字符等。此外,脱敏策略也可以根据业务需求调整,如保留前4位和后2位,中间替换为星号等。 ###
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值