using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class Test { public static void Main() { var markdown = "[url=https://stackoverflow.com/a b]SO[/url]"; var p = @"(?i)\[url=([^]]+)](.*?)\[/url]"; var result = Regex.Replace(markdown, p, x => $"[{x.Groups[2].Value}]({x.Groups[1].Value.Replace(" ","%20")})"); Console.WriteLine(result); } }