I need to protect the email addresses contained in a text. Ideally find a regular expression that could do it more effectively.
Example:
Hi: My Name is Alex and my mail is alexmail@domain.com but you can reply to alexreply@other.domain.com.
Desired output:
Hi: My Name is Alex and my mail is ale****@domain.com but you can reply to ale****@other.domain.com.
The logic is: keep first 3 characters and replace the rest with * until the @.
a@mail.com => a****@mail.com
ab@mail.com => ab****@mail.com
abc@mail.com => abc****@mail.com
abcd@mail.com => abc****@mail.com
abcde@mail.com => abc****@mail.com
Now, I made a function to protect a mail in this way, but when it is a text containing several emails then I can not use replaceAll.
public static String protectEmailAddress(String emailAddress) {
String[] split = emailAddress.split("@");
if (split[0].length() >= 3) {
split[0] = split[0].substring(0, 3);
}
emailAddress = StringUtils.join(split, "****@");
return emailAddress;
}
So basically what I need is a nice regex that work. Something similar to this but with another section of the mail, if possible.
Thanks...