How to match some digits but exclude some constant(also digits) from Regular expression in C#

110 Views Asked by At

In C#, I would like to replace all sensitive digits and replace/mask with **** but there are some other digits I would not like to replace such as phone number which is a constant.

This is what I am doing now


String original = "this is your sensitive digit 12345 , if you have any question please call CONSTANTPHONENUMBER";
String pattern = @"[0-9]{4,}";
String result =  Regex.Replace(original, pattern, "****");

the result is

this is your sensitive digit **** , if you have any question please call ****

My question is how can I exclude that CONSTANTPHONENUMBER so that it wont be replaced.

The desired result

"this is your sensitive digit **** , if you have any question please call CONSTANTPHONENUMBER"

4

There are 4 best solutions below

0
NPras On BEST ANSWER

Simplest one I could think of is to use negative lookahead surrounded by \b (word boundary). Also with multiple exclusions:

var str = "Digit 1 is 12345, please call 111222 or 334455";
var pat = @"\b(?!111222|334455)\b\d{4,}";
System.Console.WriteLine(Regex.Replace(str, pat, "****"));
// Digit 1 is ****, please call 111222 or 334455
2
Zohaib Waqar On

You can use Regex.Escape followed by Replace. Below is the code

var regex = new Regex(Regex.Escape("sensitiveNumber"));
var newText = regex.Replace("Your String Here...", "****", 1);

Here we are using overload of Regex.Replace. And specifying 1 which means no of times to replace. so it will replace only one time occurrence of string mentioned in Escape.

For Example

  1. Scenario - 1

    var regex = new Regex(Regex.Escape("o"));

    var newText = regex.Replace("Hello World", "****", 1);

    Output : Hell**** World

  2. Scenario - 2

    var regex = new Regex(Regex.Escape("o"));

    var newText = regex.Replace("Hello World", "****", 2);

    Output : Hell**** W****rld

9
Prasad Telkikar On

You can try below,

    Regex rgx = new Regex(@"\d{4,}\d");
    string replacedstar = rgx.Replace( input, "*****", 1 );

I used .Replace() method from Regex class. Last int parameter will update digit only once.

.net fiddle


As per the latest requirement, I couldn't think of Regex. Here is some hacky way.

    var split = input.Split("please call"); //Split into two strings
    StringBuilder sb = new StringBuilder();
    if(split.Length == 2)
    {
        Regex rgx = new Regex(@"\d{4,}\d");
        string replacedstar = rgx.Replace( split[0], "*****"); //Mask all the sensitive pins    
        return replacedstar + "please call" + string.Join("", split.Skip(1).ToList()); //Merge it to original
    }
    return input; //Otherwise return as it is.
2
Sebastian Schumann On

I would just replace all digits using the MatchEvaluator:

var newText = Regex.Replace(original, @"[+]?\d+", Evaluator);


string Evaluator(Match match) =>
    match.Value == "CONSTANTPHONENUMBER" ? "CONSTANTPHONENUMBER" : "****";

DEMO

The MatchEvaluator is a function and you can check for your phone number.