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

Tags: , ,

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}).(?=[^@]*?@) -> abc*****

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

    Example : -> *****

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

Can someone throw pointers on this?


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 @.

Source: stackoverflow