0

I have to match 2 urls, the first one comes from MySQL db and the second one comes from an Html page. If I compare both as string

var match = Regex.Match(href.Attributes["href"].Value, testString, RegexOptions.IgnoreCase);

the match.Success = false. Both strings are like this : myUrl/rollcontainer-weiß but the match.Success is still false.

I tried to add HttpUtility.HtmlEncode to check both strings and I get : myUrl/rollcontainer-wei&#233 for the first one and myUrl/rollcontainer-wei&ß for the second one.

How can I have a match.Success = true in this case ?

Shrikant Mavlankar
  • 1,145
  • 1
  • 8
  • 17
Gun
  • 501
  • 8
  • 27

1 Answers1

1

Try this function, for example.

static void Main(string[] args)
{
    bool test = Test("http://myUrl.com/rollcontainer-Wei&ß", "http://myUrl.com/rollcontainer-wei&ß");

}

public static bool Test(string url1, string url2)
{
    Uri uri1 = new Uri(HttpUtility.HtmlDecode(url1)); 
    Uri uri2 = new Uri(HttpUtility.HtmlDecode(url2));

    var result = Uri.Compare(uri1, uri2,
        UriComponents.Host | UriComponents.PathAndQuery,
        UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase);

    return result == 0;
}
  • Cool! It works! I tried it but without the HtmlDecode in the Uri ! – Gun Jul 11 '16 at 09:46
  • FYI There is also `WebUtility.HtmlDecode` (in `System.Net`). Available since .Net 4.0. –  Jul 11 '16 at 10:18