using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine("----------------------------TEST PAN--------------------------------------------------------");
TestCC("123456789123", "xxxxxxxx9123");
TestCC("1234-5678-9123-4567", "xxxx-xxxx-xxxx-4567");
TestCC("123123123123131For8640", "xxxxxxxxxxx3131For8640");
TestCC("123123123123131to123123123123177", "xxxxxxxxxxx3131toxxxxxxxxxxx3177");
TestCC("1234-5678-9123-4567-891", "xxxx-xxxx-xxxx-xxx7-891");
TestCC("1234 5678 9123 4567", "xxxx xxxx xxxx 4567");
TestCC("1234567", "1234567");
Console.WriteLine("-----------------------------TEST ACCOUNT NUM-------------------------------------------------------");
TestAccNo("12345678","xxxx5678");
TestAccNo("123456789","xxxxx6789");
TestAccNo("1234567","1234567");
Console.WriteLine("----------------------------TEST BOTH--------------------------------------------------------");
TestCCAndAccNo("Im paying 2700 dollars with my credit card(123456789012345) to my account(12345678)","Im paying 2700 dollars with my credit card(xxxxxxxxxxx2345) to my account(xxxx5678)");
TestCC("Im paying 2700 dollars with my credit card(123456789012345) to my account(12345678)","Im paying 2700 dollars with my credit card(xxxxxxxxxxx2345) to my account(12345678)");
public static void TestCC(string @value, string expected)
var result = @value.MaskCCNo();
result.Should().Be(expected);
Console.WriteLine($"{@value} => {result}");
public static void TestAccNo(string @value, string expected)
var result = @value.MaskAccNo();
result.Should().Be(expected);
Console.WriteLine($"{@value} => {result}");
public static void TestCCAndAccNo(string @value, string expected)
var result = @value.MaskIfContainCreditCardPanorAcctNumNew();
result.Should().Be(expected);
Console.WriteLine($"{@value} => {result}");
public static class MaskingExtensions
public static string MaskCCNo(this string value)
var a = Regex.Replace(value, @"(?<=(?<![\d-|\s])(?=(?:-?\d|\s){12,19}(?![\d-|\s]))[\d-|\s]*)\d(?!(?:-?\d|\s){0,3}(?![\d-|\s]))", "x");
public static string MaskAccNo(this string value)
var a = Regex.Replace(value, @"(?<=(?<![\d-|\s])(?=(?:-?\d|\s){8,9}(?![\d-|\s]))[\d-|\s]*)\d(?!(?:-?\d|\s){0,3}(?![\d-|\s]))", "x");
public static string MaskIfContainCreditCardPanorAcctNumNew(this string value)
if (string.IsNullOrEmpty(value))
var maskedAccountNumber = value.MaskAccNo();
return maskedAccountNumber.MaskCCNo();