Best way to replace Urls in .Net application I have an application developed in ASP.NET Core C#, where according to a configuration it must replace certain urls of a text when an http request is made and the text must be returned as soon as possible in response, the text can change on each request. 

At this moment the code I have is working well, but I would like you to help me check the code to improve the speed of replacement of the urls.

string r = contenidoRespuesta; //contenidoRespuesta can be html, json or javascript content
string dominio="example.com";
int puertoHttp=81,puertoHttps=444;           
foreach (var p in grupo.Paginas.Where(p =&gt; p.Proxy))
{

    Uri uri = new Uri(p.URL);
    string pattern1 = string.Format("(?&lt;=https://){0}(:(443)(?!\\d+))?(?!:)", uri.Host);//generates a regular expression to replace urls that start with "https"
    string pattern2 = string.Format("(?&lt;=http://){0}(:(80)(?!\\d+))?(?!:)", uri.Host);//generates a regular expression to replace urls that start with "http"
    string pattern3 = string.Format("(?&lt;!%2F|\\.|http(s)?(://)){0}(:((80)|(443))(?!\\d+))?(?!:)", uri.Host);//generates a regular expression to replace urls that start with "//"


    r = Regex.Replace(r, pattern1,uri.Host.Replace(".", string.Empty) + "." +dominio + ":" + puertoHttps, RegexOptions.IgnoreCase);// replace https://www.page.com for https://wwwpagecom.example.com
    r = Regex.Replace(r, pattern2,uri.Host.Replace(".", string.Empty) + "." + dominio + ":" + puertoHttp, RegexOptions.IgnoreCase);// replace http://www.page.com for http://wwwpagecom.example.com
    r = Regex.Replace(r, pattern3, uri.Host.Replace(".", string.Empty) + "." + dominio + ":" +(esquema == "https"? puertoHttps.ToString(): puertoHttp.ToString()));// replace //www.page.com for https://wwwpagecom.example.com
}
return r;//return the responsd with new urls
Since you just getting the host url, remove the dots, then append it as a subdomain to the given domain with the approprate protocols (Https or Http). You can do this : 

public IEnumerable&lt;string&gt; GetRoutedUrls(IEnumerable&lt;string&gt; urls, string domain, int httpPort, int httpsPort)
{
    if(urls == null) { throw new ArgumentNullException(nameof(urls)); }

    if (string.IsNullOrEmpty(domain)) { throw new ArgumentNullException(nameof(domain)); }

    foreach (var url in urls)
    {
        if (string.IsNullOrEmpty(url)) { yield return null; }

       if(Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri uri))
        {
            yield return uri.Scheme == "https" ? $"https://{uri.Host.Replace(".", string.Empty)}.{domain}:{httpsPort}" : $"http://{uri.Host.Replace(".", string.Empty)}.{domain}:{httpPort}";
        }
    }
}


usage : 

var urls = GetRoutedUrls(grupo.Paginas.Where(p =&gt; p.Proxy).Select(x=&gt; x.URL), "example.com", 81, 444)


UPDATE

After clarifying things, I think you don't need Regex since you're replacing an exact match of urls. Regex would be more useful if you want to search for unknown inputs.

To give you an example of that, if we want to get every single url in the response we can use

Regex.Matches(response, @"(https?://)(.*)(\d)", RegexOptions.IgnoreCase)


This would get every single url that has specified a port (e.g. https://example.com:443). Then, we can do stuff on them. 

In your method, is different a bit. You already have defined the urls, and you want to check for these urls in your response, if they exist, you want to change them by appending them as subdomain to the given domain, and giving them a new port as well. 

Unless if there is any special cases that is not covered or mentioned in your post (which forced you to use Regex in first place), I think it would be a straight forward approach if we just use string.Replace directly. 

public string ReplaceResponseUrls(IEnumerable&lt;string&gt; urls, string response, string domain, int httpPort, int httpsPort)
{
    if (urls == null) { throw new ArgumentNullException(nameof(urls)); }

    if (string.IsNullOrEmpty(response)) { throw new ArgumentNullException(nameof(response)); }

    if (string.IsNullOrEmpty(domain)) { throw new ArgumentNullException(nameof(domain)); }

    var responseStringBuilder = new StringBuilder(response);

    foreach (var url in urls)
    {
        if (string.IsNullOrEmpty(url)) { continue; } // skip to the next url

        if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri uri))
        {
            responseStringBuilder.Replace(
                $"{uri.Scheme}://{uri.Host}:{uri.Port}", // old url
                $"{uri.Scheme}://{uri.Host.Replace(".", string.Empty)}.{domain}:{(uri.Scheme == "https" ? httpsPort : httpPort)}" // new url
                );
        }
    }

    return responseStringBuilder.ToString();
}


Try this approach first, do some tests on it, and let me know if you need any help with it.