2013-08-20 31 views
2

我在數據庫中有一個總是有標籤的字段。也有很多的文字..只得到src值

例如:

Hey there.. whats up? 
<img src="http://cdn.theatlantic.com/static/infocus/ngpc112812/s_n01_nursingm.jpg" alt="" /> 
.. and this is my photo!.. 

我需要得到

SRC之間ONLY什麼

我想:

public string LinkPicCorrect(string path) 
{ 
    //string input = "[img]http://imagesource.com[/img]"; 
    string pattern = @"\<img>([^\]]+)\\/>"; 
    string result = Regex.Replace(path, pattern, m => 
    { 
     var url = m.Groups[1].Value; 
     // do something with url here 
     // return the replace value 
     return @"<img src=""" + url + @""" border=""0"" />"; 
    }, 
     RegexOptions.IgnoreCase); 

    result = "<img src='" + result + "'/>"; 

    return result; 
} 

但我有一個解析錯誤:

異常詳細信息:System.ArgumentException:解析 「\([^] +)\ />」 - 引用未定義的組名IMG。

雖然不知道是否我的代碼是在正確的道路......

回答

11

這個問題已經被問here

string matchString = Regex.Match(original_text, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value; 
+0

謝謝你,幫助了很多。 – thormayer

+0

如果有後SRC,沒有道具,你必須添加像ALT」之源後,這是行不通的。替換‘’與‘’ –

1

你可以試試這個正則表達式模式: -

string pattern= Regex.Match(path, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value; 
+0

@thormayer: - 我可以知道我的答案有什麼問題,因爲它既沒有被提出也沒有被接受,因爲其他答案只是我的答案的複製粘貼! :( –

+0

我的答案是不是你的複製/粘貼,但我引述一位已經回答問題的副本,請不要憤怒這樣的事情......這是一個Q/A網站,而不是一個排行榜。 – Atlasmaybe