Use java regular expression to replace the email and phone number part of the content with *

/**
 * Key information blocking, blocking emails and phone calls
 * @param content
 * @return
 */
public static String senstiveContent(String content,String type){

   // return empty if the content is null
   if(content == null){
      return "";
   }

   String replacedStr = "";// Replaced string
   String pattern = ""; // regular expression

   if(type.equals(CommonConst.SENSTIVE_TYPE_EMAIL)){
      // Judging the length before the email@ symbol, if it is less than 2 characters, keep one plaintext
      String emailPattern = "\\S+@\\S+\\.\\S+";
      // Construct the Pattern object
      Pattern p = Pattern.compile(emailPattern);
      // create a Matcher object
      Matches matches = p.matches (content);
      String patternStr = "";// matched string
      if(matcher.find()){
         patternStr = matcher.group();
      }
      // Determine the position of the @ symbol
      int index = patternStr.indexOf("@");
      if(index == -1){
         return content;
      }
      if(index < 2 ){
         // keep a plaintext
         pattern = "(\\S){1}\\S+(@\\S+)";
      }else{
         // Keep the first 2 plaintext and the last plaintext
         pattern = "(\\S){2}\\S+(\\S{1}@\\S+)";
      }
      // replace
      replacedStr = content.replaceAll(pattern,"$1****$2");
   }

   if(type.equals(CommonConst.SENSTIVE_TYPE_PHONE)){
      // There is an international call. Rules: Consecutive characters with more than 5 digits are regarded as phone numbers
      pattern = "([0-9]{4})([0-9]+)";
      // replace
      replacedStr = content.replaceAll(pattern,"$1****");
   }
   return replacedStr;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325882337&siteId=291194637