HideDataUtil.java 1.6 KB
package com.yohoufo.user.helper;

import com.yohoufo.common.utils.UserInfoHiddenHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HideDataUtil {

    public static boolean isEmail(String string) {
        if (string == null)
            return false;
        //String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        String regEx1 = "^([\\w]+[-|\\.]?)+[\\w]@([\\w]+(-[\\w]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (m.matches())
            return true;
        else
            return false;
    }


    /**
     * 只显示@前面的首位和末位
     */
    public static String hideEmail(String email) {
        if(StringUtils.isBlank(email)) {
            return email;
        }
        //email =  email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
        email =  email.replaceAll("([\\S\\s]{3}?)([\\S\\s]+)(@\\w+\\.[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)?)", "$1****$3");
        return email;
    }

    public static void main(String args[]){
        String str="ff.f.wwwr3@163.com.com";
        System.out.println(isEmail(str));
        System.out.println(hideEmail(str));
        str = "123";
        System.out.println(UserInfoHiddenHelper.isPhone(str));
        System.out.println(UserInfoHiddenHelper.hidePhoneNo(str));

        System.out.println(str.substring(0,1)+"****"+str.substring(str.length()-1));
    }

}