Java判断一段话中是否有电话号码

判断一段话中是否有电话号码,并将其进行隐藏:

    public static String hidPhone(String string) {
        Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[34578]\\d{9})|(?:861[34578]\\d{9}))(?!\\d)");
        Matcher matcher = pattern.matcher(string);
        StringBuffer bf = new StringBuffer(64);
        while (matcher.find()) {
//           bf.append(matcher.group()).append(",");
//            System.out.println(matcher.group());
            string = string.replace(matcher.group(), matcher.group().substring(0,3)+"********");
        }
        
//        int len = bf.length();
//        if (len > 0) {
//            bf.deleteCharAt(len - 1);
//        }
//        System.out.println(bf.toString());
    
        return string;
    }

1 个评论

这么6

要回复文章请先登录注册

Template error, template file not found