【发布时间】:2020-06-12 16:43:05
【问题描述】:
我有以下 json:
Data: {"Account":"d\\adm","Password":"cWExZjEiMTM="},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null}
我想使用 RegExp 将密码值替换为 ***。
cWExZjEiMTM= => ***
在我的示例中,我期待以下输出:
Data: {"Account":"d\\adm","Password":"***"},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null
我只有以下解决方案:
string invokeSpec = "\":{\"Account\":\"d\\\\adm\",\"password\":\"cWExZjEiMTM=\"},\"SqlServer\":{\"InstanceName\":\"\",\"MachineName\":\"MyMachine\",\"Port\":null}";
var pattern = "\\\"Password\\\":\\\"(?<pass>[^\\\"]*)\\\"";
var replaced = Regex.Replace(invokeSpec, pattern, "\"Password\":\"***\"", RegexOptions.IgnoreCase);
":{"Account":"d\\adm","Password":"***"},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null}
【问题讨论】:
-
使用 JSON 解析器。不要尝试使用正则表达式解析 JSON。
-
@Amy 我必须在进入反序列化模块之前记录所有内容
-
我不明白这个问题到底是什么。看来您已经有了一个可行的解决方案。那么,您对有效答案有何期望?
-
您可以使用 JObject.Parse 将其解析为对象。然后你可以尝试修改它。
-
@Dialectus 我正在寻找仅替换密码值。现在我要替换
"Password":"Value"。我的意思是我想从我的替换文字中踢"Password"