I am having issue in masking the below strings. For Account Number, the last 4 digits should get masked if it’s in the range of 8 to 9 and for Credit Card Number the last 4 digits should masked if it’s in range of 12-19. Below is the format of (Input,Output)-
(“Biller code 3038 – Receipt No 3514616375 221414441406″,”Biller code 3038 – Receipt No 3514616375 xxxxxxxx1406”); //CCNo should get masked
(“Biller code 3038 – Receipt No 351461637 221414441406″,”Biller code 3038 – Receipt No xxxxx1637 xxxxxxxx1406”); //Both AccNo and CCNo should get masked
(“Biller code 3038 – Receipt No 35146 221414441406″,”Biller code 3038 – Receipt No 35146 xxxxxxxx1406”); //CCNo should get masked
(“Biller code 3038 – Receipt No 221414441406 3514616375″,”Biller code 3038 – Receipt No xxxxxxxx1406 3514616375”); CCNo should get masked
(“Biller code 3038 – Receipt No 221414441406 351461637″,”Biller code 3038 – Receipt No xxxxxxxx1406 xxxxx1637”); //Both AccNo and CCNo should get masked
(“Biller code 3038 – Receipt No 22141444140 351461637″,”Biller code 3038 – Receipt No 22141444140 xxxxx1637”); //AccNo should get masked
(“Biller code 3038 – Receipt No 22141444140 3514616375″,”Biller code 3038 – Receipt No 22141444140 3514616375”); //Both should not get masked
I tried the below regex and they work for all scenarios mentioned in the demo https://dotnetfiddle.net/ffgakc. But for these particular scenarios in question, it is not working. Expectation is already mentioned above and it is expected that all the scenarios in the demo as well as this particular one in questions should all work together.
public static string MaskIfContainCreditCardPanorAcctNumNew(this string value)
{
if (string.IsNullOrEmpty(value))
return value;
var maskedAccountNumber = value.MaskAccNo();
return maskedAccountNumber.MaskCCNo();
}
public static string MaskCCNo(this string value)
{
var a = Regex.Replace(value, @"(?:G(?!^)|(?<!d[ -]?)(?=(?:[ -]?d){12,19}(?![ -]?d)))d(?=(?:[ -]?d){4})([ -]?)", "x$1");
//Tried this too
//var a = Regex.Replace(value, @"(?<!d)d(?:[s-]?d){11,18}(?!d)", m => Regex.Replace(m.Value, @"d(?=(?:[s-]?d){4})", "x"));
return a;
}
public static string MaskAccNo(this string value)
{
var a = Regex.Replace(value, @"(?:G(?!^)|(?<!d[-]?)(?=(?:[-]?d){8,9}(?![-]?d)))d(?=(?:[-]?d){4})([-]?)", "x$1");
//Tried this too
// var a = Regex.Replace(value, @"(?<!d)d(?:[s-]?d){7,8}(?!d)", m => Regex.Replace(m.Value, @"d(?=(?:[s-]?d){4})", "x"));
return a;
}
Thanks
1