I am not able to do a replacement regex
for example I have the email
and I want to replace
f****e@g***l.com
I already got the start
(?<=.).(?=[^@]*?.@)|(?<=\@.).
Below a link from where I am testing
REGEX
With a bit of further tweaking on the pattern, you could achieve that:
"[email protected]".replaceAll("(?<=[^@])[^@](?=[^@]*?.[@.])", "*");
This will give you f****e@g***l.com
.
A possibly more efficient, and more readable solution might be finding the indexes of @
and .
,
and putting together the desired result from substrings:
int atIndex = email.indexOf('@');
int dotIndex = email.indexOf('.');
if (atIndex > 2 && dotIndex > atIndex + 2) {
String masked = email.charAt(0)
+ email.substring(1, atIndex - 1).replaceAll(".", "*")
+ email.substring(atIndex - 1, atIndex + 2)
+ email.substring(atIndex + 2, dotIndex - 1).replaceAll(".", "*")
+ email.substring(dotIndex - 1);
System.out.println(masked);
}
I found this: (?<=.)([^@])(?!@)(?=.*@)|(?<!@)([^@])(?!.*@)(?!\.)(?=.*\.)
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With