The below code will take a match and construct a replacement string based on the groupValues collection. This collection specifies a Group index and the text to replace it's value with.
Unlike other solutions, it does not require the entire match to be contained in groups, only the parts of the match that you want to have in groups.
public static string ReplaceGroups(Match match, Dictionary<int, string> groupValues)
{
StringBuilder result = new StringBuilder();
int currentIndex = 0;
int offset = 0;
foreach (KeyValuePair<int, string> replaceGroup in groupValues.OrderBy(x => x.Key))
{
Group group = match.Groups[replaceGroup.Key];
if (currentIndex < group.Index)
{
result.Append(match.Value.Substring(currentIndex, group.Index - match.Index - currentIndex));
}
result.Append(replaceGroup.Value);
offset += replaceGroup.Value.Length - group.Length;
currentIndex = group.Index - match.Index + group.Length;
}
return result.ToString();
}