我有一个字符串,其中“特殊区域”用花括号括起来:
{intIncG}/{intIncD}/02-{yy}
我需要在 {} 之间遍历所有这些元素,并根据它们的内容替换它们。在 C# 中执行此操作的最佳代码结构是什么?
我不能只进行替换,因为我需要知道每个“特殊区域 {}”的索引才能用正确的值替换它。
请您参考如下方法:
Regex rgx = new Regex( @"\({[^\}]*\})");
string output = rgx.Replace(input, new MatchEvaluator(DoStuff));
static string DoStuff(Match match)
{
//Here you have access to match.Index, and match.Value so can do something different for Match1, Match2, etc.
//You can easily strip the {'s off the value by
string value = match.Value.Substring(1, match.Value.Length-2);
//Then call a function which takes value and index to get the string to pass back to be susbstituted
}
