using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Linq; public class Test { public static void Main() { var pat = @"(?m)(?:(\A)|^(?!\A))(.*\b\s+(\d+)\r?\n)"; var s = @"10 25 32 44 56 88 102 127 135 14510 25 32 44 56 88 102 127 135 145"; var res = Regex.Replace(s, pat, m => m.Groups[1].Success ? $"0 {m.Groups[2].Value}{m.Groups[3].Value} " : $"{m.Groups[2].Value}{m.Groups[3].Value} "); Console.WriteLine(res); } }