java从一段文本中识别出手机号/身份证号等敏感信息,隐藏处理,中文字符串匹配

1.手机号隐藏处理

     String content = "我是中国人手机号,中国码农";
        Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[34578]\\d{9})|(?:861[34578]\\d{9}))(?!\\d)");
        Matcher matcher = pattern.matcher(content);
        StringBuffer bf = new StringBuffer(128);
        while (matcher.find()) {
            content = content.replace(matcher.group(), matcher.group().substring(0,3)+"********");
        }

        System.out.println(content);

2.身份证隐藏处理

        String content = "我是中国人身份证号,中国码农";

        String regex ="([1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx])|([1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3})";

        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(content);
        while (matcher.find()){
            content = content.replace(matcher.group(), matcher.group().substring(0,6)+"********");
        }
        System.out.println(content);

 3.中文匹配:*   零次或多次匹配前面的字符或子表达式

  String content = "我是中国人,中国码农,身份证你是谁号码你好";
        Pattern pattern = Pattern.compile(".*身份证.*号码.*");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String group = matcher.group();
            System.out.println(group);
            //匹配上了
        }
        System.out.println(content);
    }

 

posted @ 2022-03-22 20:22  shadow321  阅读(1588)  评论(0编辑  收藏  举报