I have to match a pattern with the URL. I want the pattern to match the domain, and don't care about if it ends in a trailing slash or if it has querystring params, or any subdomains I want only to accept the protocols http or https.
Here is what I tried:
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;   
using Newtonsoft.Json;
public class Program
{
    public static void Main()
    {
        List<string>  inputs = new List<string>{
            "https://dotnetfiddle.net/UA6bCb"
        ,"http://www.test.ch/de-ch/apps/weve?anlassId=236601"
        ,"https://www.test.ch/de-ch/apps/weve?anlassId=236601"
        ,"http://test.ch/de-ch/apps/weve?anlassId=236601"
        ,"https://test.ch/de-ch/apps/weve?anlassId=236601"
                ,"https://test.chn/de-ch/apps/weve?anlassId=236601"
                ,"https://www.test.chn/de-ch/apps/weve?anlassId=236601"
                ,"https://test.ch/de-ch/"
                ,"https://test.ch/de-ch"
                ,"https://test.ch/"
                ,"https://test.ch"
                ,"https:test.ch"
        };
    
        Test(inputs);
        
    }
    public static void Test(List<string> inputs)
    {
        var regexString=  @"http(s)?://?([\w-]+\.)?test.ch(/[\w- ;,./?%&=]*)?";
        foreach(var input in inputs){
        var matches = Regex.Match(input,regexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            
            if(matches.Success){
                Console.WriteLine("{0} matches {1}", input, regexString);
            }
            else{
                    Console.WriteLine("NO MATCH for {0}", input);
            }
        
        
        }
    }
}
This returns
NO MATCH: https://dotnetfiddle.net/UA6bCb
Match: http://www.test.ch/de-ch/apps/weve?anlassId=236601
Match: https://www.test.ch/de-ch/apps/weve?anlassId=236601
Match: http://test.ch/de-ch/apps/weve?anlassId=236601
Match: https://test.ch/de-ch/apps/weve?anlassId=236601
Match: https://test.chn/de-ch/apps/weve?anlassId=236601
Match: https://www.test.chn/de-ch/apps/weve?anlassId=236601
Match: https://test.ch/de-ch/
Match: https://test.ch/de-ch
Match: https://test.ch/
Match: https://test.ch
NO MATCH: https:test.ch
The problem is that this solution matches https://test.chn/de-ch/apps/weve?anlassId=236601 and https://www.test.chn/de-ch/apps/weve?anlassId=236601
This should be false because the domain ends in chn.
I haven't been able to get the right regex.
Thanks for the help.
 
    