using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class Test { public static void Main() { var s = "wordA wordB wordC wordB \n wordD"; var pattern = @"(?<=wordA.*?)wordB(?=.*?wordD)"; var result = Regex.Replace(s, pattern, "<<<$&>>>", RegexOptions.Singleline); Console.WriteLine(result); } }