Skip to content
Advertisement

Regular expression to mask email except the three characters before the domain

I am trying to mask email address in the following different ways.

  1. Mask all characters except first three and the ones follows the @ symbol. This expression works fine.

    (?<=.{3}).(?=[^@]*?@)

    abcdefgh@gmail.com -> abc*****@gmail.com

  2. Mask all characters except last three before @ symbol.

    Example : abcdefgh@gmail.com -> *****fgh@gmail.com

    I am not sure how to check for @ and do reverse match.

Can someone throw pointers on this?

Advertisement

Answer

Maybe you could do a positive lookahead:

.(?=.*...@)

See the online Demo

  • . – Any character other than newline.
  • (?=.*...@) – Positive lookahead for zero or more characters other than newline followed by three characters other than newline and @.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement