使用正則表達式:
Regex regex = new Regex(@"\s((?:\S+)\.(?:\S+))");
string string1 = "This is my string http://www.google.co.uk and that was my url.";
string string2 = "this is my string google.gr and that was my url.";
var string1WithoutURI = regex.Replace(string1, ""); // Output: "This is my string and that was my url."
var string2WithoutURI = regex.Replace(string2, ""); // Output: "this is my string and that was my url."
// Regex.Replace(string1, @"\s((?:\S+)\.(?:\S+))", ""); // This can be used too to avoid having to declare the regex.
if (string1WithoutURI == string2WithoutURI)
{
// Do what you want with the two strings
}
解釋正則表達式\s((?:\S+)\.(?:\S+))
\s
將匹配任何空白字符
2.((?:\S+)\.(?:\S+))
將匹配的URL,直到下一次空格字符
2.1。(?:\S+)
將匹配任何非空白字符而不捕獲組(與?:)
2.2。\.
將匹配字符「。」,因爲它將始終存在於一個url中
2.3。(?:\S+))
再次,將無需再次捕獲組(與匹配任何非空白字符?:)來獲取點之後的所有內容。
這應該是訣竅...
你能詳細說明你認爲的一個匹配嗎? 「http:// www.google.co.uk」與「google.gr」匹配嗎? – Rob
如果在一個字符串中的所有文本字符串兩個文本匹配,則其視爲匹配。 的String1 = 「**這是我的字符串** ** http://www.google.co.uk,那是我的網址**」 String2的=「**這是我的字符串** google.gr **,這是我的網址**「 – johnsmith6
可能的重複[只從域名獲得URL?](http:// stackoverflow。com/questions/2154167/get-just-the-domain-name-from-a-url) –