using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Linq; public class Test { public static void Main() { var userDic = new Dictionary { {"399969178745962506", "One"}, {"104729417217032192", "Two"} }; var p = @"<@!?(\d+)>"; var s = "<@399969178745962506> hello to <@!104729417217032192>"; Console.WriteLine( Regex.Replace(s, p, m => userDic.ContainsKey(m.Groups[1].Value) ? userDic[m.Groups[1].Value] : m.Value ) ); Console.WriteLine( Regex.Replace(s, @"(<@!?)(\d+)>", m => userDic.ContainsKey(m.Groups[2].Value) ? $"{m.Groups[1].Value}{userDic[m.Groups[2].Value]}>" : m.Value ) ); } }