I am trying to mask email address in the following different ways.
Mask all characters except first three and the ones follows the @ symbol. This expression works fine.
(?<=.{3}).(?=[^@]*?@)
abcdefgh@gmail.com -> abc*****@gmail.com
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?
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 @
. You could use a negated character class [^\s@]
matching a non whitespace char except an @. Then assert what is on the right is that negated character class 3 times followed by matching the @ sign.
In the replacement use *
[^\s@](?=[^@\s]*[^@\s]{3}@)
[^\s@]
Negated character class, match a non whitespace char except @
(?=
Positive lookahead, assert what is on the right is
[^@\s]*
Match 0+ times a non whitespace char except @
[^@\s]{3}
Match 3 times a non whitespace char except @
@
Match the @ )
Close lookahead If there can be only a single @ in the email address, you could for example make use of a finite quantifier in the positive lookbehind:
(?<=(?<!\S)[^\s@]{0,1000})[^\s@](?=[^@\s]*[^@\s]{3}@[^\s@]+\.[a-z]{2,}(?!\S))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.