在 C# 中,我想替换所有敏感数字并用 **** 替换/掩码,但还有一些其他数字我不想替换,例如电话号码,它是一个常量。
这就是我现在正在做的事情
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, "****");
结果是
这是您的敏感数字****,如有任何疑问请致电****
我的问题是如何排除 CONSTANTPHONENUMBER 以便它不会被替换。
想要的结果
“这是您的敏感数字****,如果您有任何疑问请拨打 CONSTANTPHONENUMBER”
您可以将 Regex.Escape 与替换一起使用。下面是代码
var regex = new Regex(Regex.Escape("sentitiveNumber"));
var newText = regex.Replace("Your String Here...", "****", 1);