I would like to mask the email passed in the maskEmail function. I'm currently facing a problem wherein the asterisk * is not repeating when i'm replacing group 2 and and 4 of my pattern.
Here is my code:
fun maskEmail(email: String): String {
return email.replace(Regex("(\\w)(\\w*)\\.(\\w)(\\w*)(@.*\\..*)$"), "$1*.$3*$5")
}
Here is the input:
tom.cat@email.com
cutie.pie@email.com
captain.america@email.com
Here is the current output of that code:
t*.c*@email.com
c*.p*@email.com
c*.a*@email.com
Expected output:
t**.c**@email.com
c****.p**@email.com
c******.a******@email.com
Edit: I know this could be done easily with for loop but I would need this to be done in regex. Thank you in advance.