两组支架之间的替换词 [英] Replace word between two sets of brackets
本文介绍了两组支架之间的替换词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串:
"You have just received {{PowerUpName}} from {{PlayerName}}"
然后我收到一组参数(JSON格式):
I then receive a set of parameters (in JSON format):
{"PowerUpName":"Super Boost","PlayerName":"John"}
我试图找出如何我替换双括号,即{{PowerUpName}}用参数中的话。我想我需要使用正则表达式,但我不知道的表达应该是什么。我在C#的方式编码(而不能使用LINQ)。
I'm trying to work out how I replace the words within double brackets i.e. {{PowerUpName}} with a parameter. I guess I need to use regex but I have no idea what the expression should be. I'm coding in C# by the way (and can't use LINQ).
任何帮助将非常感激。
推荐答案
如果您要更换里面的 {{
和}}
符号,你不需要LINQ:
If you want to replace any words inside {{
and }}
symbols, you do not need LINQ:
// Input string
string str = "You have just received {{PowerUpName}} from {{PlayerName}}";
// Initializing sample dictionary object
var obj = new Dictionary<string,string>();
// Filling it out
obj.Add("PowerUpName", "Super Boost");
obj.Add("PlayerName", "John");
// Replacing the values with those in the dictionary
string output = Regex.Replace(str, "(?<=\\{\\{)(.*?)(?=\\}\\})", match => obj[match.Groups[1].Value]);
// Display result
Console.WriteLine(output);
结果:
Result:
You have just received {{Super Boost}} from {{John}}
请参阅示例程序。
这篇关于两组支架之间的替换词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文