0
我需要檢查一個字符串是否包含鏈接並將其打印出來。到目前爲止,這是我的代碼使用正則表達式獲取鏈接字符串
string strRegex = @"(http://\w+\.\w+\.\w+)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"An http://w.facebook.me http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ahttp vhttp b... ../ .. extraordinary day dawns with each new day.";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
Console.WriteLine(myMatch.Value);
}
}
輸出:
http://w.facebook.me
http://www.w3.org
輸出應該
http://w.facebook.me
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd
在我正則表達式我不想再添\w+
,因爲我沒有知道一個鏈接中會出現多少斜線和多長的單詞。
那麼我該如何做得更好呢?我怎麼能不知道鏈接將會有多長鏈接?
謝謝
正則表達式只返回http://www.w3.org我需要完整的鏈接http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd –
在正則表達式中的小錯字。它現在應該工作 –