using System; using System.IO; using System.Text.RegularExpressions; partial void Main() { var line = "Keeping the line intact, replace me, but not you, keep this too.\n\nRetaining me, replace one, but not two, ignore three."; var res = Regex.Replace(line, @"replace (?.*), but not (?.*),", m => "replace " + m.Groups["source"].Value.Replace("o", "1") + ", but not " + m.Groups["source"].Value + ","); Console.WriteLine(res); }