Home > Software engineering >  how to mask email and phone number with regular expression with fixed length substitution
how to mask email and phone number with regular expression with fixed length substitution

Time:10-10

I am writing regEx for masking Email address and Phone number in Azure B2C Claims

My requirement is as follows,

For Email address, I need to display first two characters of the email unmasked and fixed 5 * (masked) irrespective of length remaining characters and unmasked "@domain.com" Expected Result (for example):

[email protected] ==> te*****@gmail.com

[email protected] ==>te*****@gmail.com

My regular expression is currently display first two characters and replacing remaining characters with *

RegEx: https://regex101.com/r/3xL2ht/1

For Mobile:

I need to display seven asterisk as masked characters irrespective of number digits. last 4 digits should be unmasked.

Expected Result (for example):

91-1234567890 ==> *******7890

919012345678 ==> *******5678

Current configuration is showing last 4 digits correctly, but preceding digits are replaced by * , if I have 12 digit, it shows eight asterisk and las 4 digit.

Current regEx : https://regex101.com/r/56DpA6/1

Please help me in sorting this out!

Thank you!!

CodePudding user response:

For your mail problem:

replace ([^@]{1,2})[^@]*(. ) with $1.....$2

  • search for the two first letters (not '@') and capture them
  • search for the rest in front of '@' (and forget it)
  • capture the rest (beginning at '@')

[EDIT] I think your other question can be solved very similar. Capture the last digits and concat them to your new (asterisk) string.

  • Related