using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class Test { public static void Main() { var line = "ID is PK_myid some more words\nID is PK_ my id some more words"; var pattern = @"\bID\s+is\s+(\w+(?:\s+\w+)*?)\s+some\b"; var output = Regex.Matches(line, pattern) .Cast() .Select(x => x.Groups[1].Value); foreach (var s in output) Console.WriteLine(s); } }