The code below will match and build a replacement string based on the groupValues โโcollection. This collection indicates the group index and text to replace it with a value.
Unlike other solutions, this does not require that the entire match be contained in groups, but only those parts of the correspondence 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(); }
Mr Universe
source share