个人信息打码
Posted 高木子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人信息打码相关的知识,希望对你有一定的参考价值。
package com; import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { //个人信息打码 private static String getEncrypt(String number){ //电话 if (number != null && number.length() == 11) { return number.replaceAll("(\\\\d{3})\\\\d{4}(\\\\d{4})", "$1****$2"); } //身份证号码 if (number != null && number.length() == 18) { return number.replaceAll("(\\\\d{4})\\\\d{12}(\\\\w{2})","$1************$2"); } else if(number != null && number.length() == 15){ return number.replaceAll("(\\\\d{4})\\\\d{9}(\\\\w{2})","$1*********$2"); }else{ return number; } } private static String getEncryptName(String name){ String regex = "[\\\\u4E00-\\\\u9FA5]{2,30}(?:·[\\\\u4E00-\\\\u9FA5]{2,5})*"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(name); boolean isMatch = m.matches(); StringBuilder sb = new StringBuilder(); String names=""; if (isMatch) {//说明是中文姓名 if (name.contains("·")) { String[] strNames = name.split("·"); for (int i = 0; i < strNames.length-1; i++) { if (i!=strNames.length) { names = strNames[i].replaceAll("[\\\\u4e00-\\\\u9fa5]", "*"); } sb.append(names).append("·"); } return sb.append(strNames[strNames.length-1]).toString(); } else { String[] strNames = name.split(""); sb.append(strNames[1]); for (int i = 2; i < strNames.length; i++) { names = strNames[i].replaceAll("[\\\\u4e00-\\\\u9fa5]", "*"); sb.append(names); } return sb.toString(); } } else {//说明是英文 if (name.contains(" ")) { String[] strNames = name.split(" "); for (int i = 0; i < strNames.length-1; i++) { if (i!=strNames.length) { names = strNames[i].replaceAll("^[A-Za-z]+$", "*"); } sb.append(names).append(" "); } return sb.append(strNames[strNames.length-1]).toString(); } } return name; } /** * @param args */ public static void main(String[] args) { System.out.println(getEncrypt("411421995412541201")); System.out.println(getEncrypt("13595245120")); System.out.println(getEncryptName("张三")); } }
结果:
以上是关于个人信息打码的主要内容,如果未能解决你的问题,请参考以下文章
《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段