1、创建配置文件:word.properties
关键词
联系电话
联系方式
联系人
联系
电子邮件
邮箱
代理机构
采购
采购人
邮箱
email
邮件
手机
手机号
座机
固定电话
电话
固话
2、StringFilter
package com.wxmp.utils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* @author Lee
* @date 2018年6月27日
* @function 加密工具
*/
public class StringFilter {
private static Logger log = LogManager.getLogger(StringFilter.class);
/**
* 将电话号、手机号、邮箱 加密
* @param content
*/
public static String encryptDigitContent(String content) {
log.info("======关键字读取==========");
List<String> keyList = new ArrayList<>();
Properties properties = new Properties();
try {
InputStreamReader in = new InputStreamReader(StringFilter.class.getResourceAsStream("/property/word.properties"), "UTF-8");
properties.load(in);
Enumeration<?> propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
keyList.add(propertyNames.nextElement().toString());
}
log.info("=========文档解析=========");
List<String> matchSectionList = new ArrayList<>();
Pattern p = null;
for(String s : keyList) {
p = Pattern.compile(s);
Matcher m = p.matcher(content);
while(m.find()) {
if((m.start()+30)>content.length()) {
matchSectionList.add(content.substring(m.start(),content.length()-1));
}else {
matchSectionList.add(content.substring(m.start(), m.start()+30));
}
}
}
log.info("==========文档替换==========");
for(String matchSection : matchSectionList) {
for(String matchCellphone : checkCellphone(matchSection)) {
content = content.replace(matchCellphone, replaceOfContent(matchCellphone));
}
for(String matchTelephone : checkTelephone(matchSection)) {
content = content.replace(matchTelephone, replaceOfContent(matchTelephone));
}
}
log.info("===========END=============");
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
/**
* 查询符合的手机号码
* @param content
*/
public static List<String> checkCellphone(String content){
List<String> cellPhoneList = null;
if(StringUtils.isNotBlank(content)) {
cellPhoneList = new ArrayList<>();
Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0|8])|(18[0,5-9]))\\d{8}");
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
cellPhoneList.add(matcher.group());
}
}
return cellPhoneList;
}
/**
* 查询符合的固定电话
* @param content
*/
public static List<String> checkTelephone(String content){
List<String> telephoneList = null;
if(StringUtils.isNotBlank(content)) {
telephoneList = new ArrayList<>();
Pattern pattern = Pattern.compile("(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)");
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
telephoneList.add(matcher.group());
}
}
return telephoneList;
}
/**
* 查询符合的邮箱
* @param content
*/
public static List<String> checkEmail(String content){
List<String> emailList = null;
if(StringUtils.isNotBlank(content)) {
emailList = new ArrayList<>();
Pattern pattern = Pattern.compile("\\w+(\\.\\w)*@\\w+(\\.\\w{2,3}){1,3}");
Matcher matcher = pattern.matcher(content);
while(matcher.find()) {
emailList.add(matcher.group());
}
}
return emailList;
}
/**
* 将 手机号 或 电话号 转化成 *
* @param content
*/
public static String replaceOfContent(String content) {
if(StringUtils.isNotBlank(content)) {
content = content.replaceAll("\\d|\\D", "*");
return content;
}
return "";
}
}