Java implements mobile phone number and email account encryption

telephone number

I originally planned to use string interception to achieve this effect, but I recently read some knowledge about regularization, so I will use regularization to test the water. It is relatively simple and just go directly to the code.

        //手机号保留前3后4  中间4个*号显示
        String phone = "15645621235";
        System.out.println(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        //手机号保留前3后1   中间7个*号显示
        String phone1 = "17805631269";
        System.out.println(phone1.replaceAll("(\\d{3})\\d{7}(\\d{1})", "$1*******$2"));
 
        String idCard = "430529199607166698";
        System.out.println(idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"));

 

mailbox

/**
 * 手机号用****号隐藏中间数字
 *
 * @param phone
 * @return
 */
public static String settingphone(String phone) {
    String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    return phone_s;
}


/**
 * 邮箱用****号隐藏前面的字母
 *
 * @return
 */
public static String settingemail(String email) {
    String emails = email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
    return emails;
}

Guess you like

Origin blog.csdn.net/qq_41071754/article/details/110221119
Recommended