简体   繁体   English

正则表达式匹配和替换子字符串

[英]Regex for match and replace substring

We have logging functionality to log all the requests and now I want to hide some sensitive data from the request body. 我们具有记录所有请求的日志记录功能,现在我想从请求正文中隐藏一些敏感数据。

string body = null;

using (var reader = new StreamReader(request.InputStream))
{
    request.InputStream.Seek(0L, SeekOrigin.Begin);
    body = reader.ReadToEnd();
}

if (!string.IsNullOrWhiteSpace(body))
{
    _logger.NameValueLogger.Add("body", RemoveSensitiveData(body));
}

Example for Body content: 正文内容示例:

"Id=12345&Id=&Name=TestName&AddressLine1=1232+test+&AddressLine2=sdf&City=abcd" “ Id = 12345&Id =&Name = TestName&AddressLine1 = 1232 + test +&AddressLine2 = sdf&City = abcd”

Now in RemoveSensitiveData() I want to search for Name and replace the name value "TestName" to "*****" 现在在RemoveSensitiveData()中,我要搜索Name并将名称值“ TestName”替换为“ *****”

Also if there are any other fields I want to hide along with name, I have to replace them too. 另外,如果还有其他要与名称一起隐藏的字段,我也必须替换它们。

Can anyone please suggest best approach to handle this? 谁能建议最好的方法来解决这个问题?

This should do it: 应该这样做:

string RemoveSensitiveData(string value) => Regex.Replace(value, "((^|&)(Name|OtherSensitiveData)=)[^&]+", m => m.Groups[1].Value + "*****")

edit: added (^|&) ensure full match 编辑:添加(^|&)确保完全匹配

If you want to "mask" some parameter with a fixed amount of asterisks you may use a simple Regex.Replace with no MatchEvaluator: 如果要用固定数量的星号“屏蔽”某些参数,则可以使用不带MatchEvaluator的简单Regex.Replace

string RemoveSensitiveData(string value) => 
    Regex.Replace(value, "((?:&|^)(?:Name|OtherSensitiveData)=)[^&]+", "$1*****")

If you plan to replace with the same amount of asterisks use 如果您打算用相同数量的星号替换,请使用

string RemoveSensitiveData(string value) =>
    Regex.Replace(value, "((?:&|^)(?:Name|OtherSensitiveData)=)([^&]+)", m =>
        $"{m.Groups[1].Value}{new String('*', m.Groups[2].Value.Length)}")

Regex details 正则表达式详细信息

  • ((?:&|^)(?:Name|OtherSensitiveData)=) - Group 1: ((?:&|^)(?:Name|OtherSensitiveData)=) -组1:
    • (?:&|^) - a non-capturing group matching either a & char or start of string (it can be replaced with (?<![^&]) ) (?:&|^) - & char或字符串开头匹配的非捕获组(可以用(?<![^&])替换)
    • (?:Name|OtherSensitiveData) - a non-capturing group matching either Name or OtherSensitiveData substrings (?:Name|OtherSensitiveData) -与NameOtherSensitiveData子字符串匹配的非捕获组
    • = - an equal sign = -等号
  • ([^&]+) - Group 2: any 1+ chars other than & . ([^&]+) -组2: &以外的任何1+个字符。

See the C# demo: 参见C#演示:

var value = "Id=12345&Id=&Name=TestName&AddressLine1=1232+test+&AddressLine2=sdf&City=abcd";
Console.WriteLine(Regex.Replace(value, "((?:&|^)(?:Name|OtherSensitiveData)=)[^&]+", "$1*****"));
// => Id=12345&Id=&Name=*****&AddressLine1=1232+test+&AddressLine2=sdf&City=abcd
Console.WriteLine(Regex.Replace(value, "((?:&|^)(?:Name|OtherSensitiveData)=)([^&]+)", m =>
        $"{m.Groups[1].Value}{new String('*', m.Groups[2].Value.Length)}"));
// => Id=12345&Id=&Name=********&AddressLine1=1232+test+&AddressLine2=sdf&City=abcd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM