DEV Community


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

boyukbas profile image baris ・1 min read

Nice one

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]


EDIT: Although the above is the answer to your question as…

Discussion (0)

Editor guide