我目前正在尝试对 JSON 字符串进行正则表达式替换,如下所示:
String input = "{\"`####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\",\"Answer_Options2\": \"not a monkey\"}";
a 目标是查找并替换所有关键字段以`####`开头的值字段
我目前有这个:
static Regex _FieldRegex = new Regex(@"`####`\w+" + ".:.\"(.*)\",");
static public string MatchKey(string input)
{
MatchCollection match = _encryptedFieldRegex.Matches(input.ToLower());
string match2 = "";
foreach (Match k in match )
{
foreach (Capture cap in k.Captures)
{
Console.WriteLine("" + cap.Value);
match2 = Regex.Replace(input.ToLower(), cap.Value.ToString(), @"CAKE");
}
}
return match2.ToString();
}
现在这行不通了。我自然猜想,因为它会选择整个 `####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\" 作为匹配项并替换它。我只想替换match.Group[1]
字符串上的单个匹配项。
归根结底,JSON 字符串需要看起来像这样:
String input = "{\"`####`Answer_Options11\": \"CATS AND CAKE\",\"`####`Answer_Options\": \"CAKE WAS A LIE\",\"Answer_Options2\": \"not a monkey\"}";
知道怎么做吗?