2016-07-11 11 views
0

2つのURLに一致する必要があります。最初のものはMySQLデータベースから来ており、2番目のものはHtmlページからのものです。私は、文字列c#Htmlの特殊文字に一致しません

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

match.Success = false.両方の文字列がthis : myUrl/rollcontainer-weißに似ていますがmatch.Successはまだ偽であるとしても比較する場合。

HttpUtility.HtmlEncodeを追加して両方の文字列を確認しようとしました。最初の文字はmyUrl/rollcontainer-wei&#233、もう1文字はmyUrl/rollcontainer-wei&ßです。

この場合、match.Success = trueはどうすればよいですか?

+0

この回答はどうですか(http://stackoverflow.com/a/18331804) –

+0

Uri.Compareを使用すると-1が返されます。 – Gun

+0

'var match = Regex.Match(href.Attributes [" href "])を試してください。値、Regex.Escape(HttpUtility.HtmlDecode(testString))、RegexOptions.IgnoreCase);' –

答えて

1

たとえば、この機能を試してください。

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; 
} 
+0

クール!できます!私はそれを試しましたが、UriのHtmlDecodeはありませんでした! – Gun

+0

FYI 'WebUtility.HtmlDecode'(' System.Net'にあります)もあります。 NET 4.0以降で利用可能です。 –

関連する問題