Regex in C # - how do I replace only one specific group in Match?

I am parsing text using a C # regex. I want to replace only one specific group for each match. Here is how I do it:

void Replace(){ string newText = Regex.Replace(File.ReadAllText(sourceFile), myRegex, Matcher, RegexOptions.Singleline); //....... } void string Matcher(Match m){ // how do I replace m.Groups[2] with "replacedText"? return ""; // I want to return m.Value with replaced m.Group[2] } 
+7
source share
6 answers

This should do it:

 string Matcher(Match m) { if (m.Groups.Count < 3) { return m.Value; } return string.Join("", m.Groups .OfType<Group>() //for LINQ .Select((g, i) => i == 2 ? "replacedText" : g.Value) .Skip(1) //for Groups[0] .ToArray()); } 

Example: http://rextester.com/DLGVPA38953

EDIT:. Although the above answer to your question is written, you can find images of zero width easier for your actual scenario:

 Regex.Replace(input, @"(?<=e)l+(?=o)", replacement) 

Example: http://rextester.com/SOWWS24307

+10
source

You can use MatchEvaluator : Try this.

 var replaced = Regex.Replace(text, pattern, m => m.Groups[1] + "AA" + m.Groups[3]); 

I found one post on stackoverflow related to this: watch This

+4
source

How about this?

  static string Matcher(Match m) { var group = m.Groups[2]; var startIndex = group.Index - m.Index; var length = group.Length; var original = m.Value; var prior = original.Substring(0, startIndex); var trailing = original.Substring(startIndex + length); return string.Concat(prior, "replacedText", trailing); } 
+4
source

Regex.Replace will always replace anything the regular expression provided matches .

You have two options:

  • Match only what you want to replace

    or

  • Insert the parts of the original string that was matched, but should be stored in the replacement string using capture groups.

You have not provided enough information to more accurately answer your question. For example. Do you really need a MatchEvaluator ? This is only necessary if you want to provide a separate replacement string for each match.

+2
source
  private string ReplaceText(string originalText) { string replacedText = null; Regex regEx = new Regex("[your expression here]"); Match match = regEx.Match(originalText); if (match.Success) { Group matchGroup = match.Groups[2]; //[first part of original string] + [replaced text] + [last part of original string] replacedText = $"{originalText.Substring(0, matchGroup.Index)}[Your replaced string here]{originalText.Substring(matchGroup.Index + matchGroup.Length)}"; } else replacedText = originalText; return replacedText; } 
0
source

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(); } 
0
source

All Articles