这将替换为 {n}:
string input = "When this Pokémon has 1/3 or less of its [HP]{mechanic:hp} remaining, its []{type:grass}-type moves inflict 1.5× as much [regular damage]{mechanic:regular-damage}.";
string pattern = @"\[([\s\w'-]*)\]\{([\s\w'-]*):([\s\w'-]*)\}";
MatchCollection matches = Regex.Matches(input, pattern);
for(int i = 0; i < matches.Count; i++)
{
input = input.Replace(matches[i].Groups[0].ToString(), "{" + i + "}");
}
Console.WriteLine(input);
输出:
"When this Pokémon has 1/3 or less of its {0} remaining, its {1}-type moves inflict 1.5× as much {2}."
如果您愿意,可以使用以下内容直接替换为跨度:
string input = "When this Pokémon has 1/3 or less of its [HP]{mechanic:hp} remaining, its []{type:grass}-type moves inflict 1.5× as much [regular damage]{mechanic:regular-damage}.";
//Note: Changed pattern to only have 2 match groups to simplify the replace
string pattern = @"\[([\s\w'-]*)\]\{([\s\w'-]*:[\s\w'-]*)\}";
MatchCollection matches = Regex.Matches(input, pattern);
for(int i = 0; i < matches.Count; i++)
{
string spanText = matches[i].Groups[1].ToString();
string spanClass = matches[i].Groups[2].ToString();
input = input.Replace(matches[i].Groups[0].ToString(), "<span class=\"" + spanClass + "\">" + spanText + "</span>");
}
Console.WriteLine(input);
输出:
"When this Pokémon has 1/3 or less of its <span class=\"mechanic:hp\">HP</span> remaining, its <span class=\"type:grass\"></span>-type moves inflict 1.5× as much <span class=\"mechanic:regular-damage\">regular damage</span>."