text = Regex.Replace(text, @"(?i)(?<=<img\b[^>]*?src=\s*(['""]?))([^'""]*/)+(?=[^'""/]+\1)", "/Images/");
//text 爲HTML代碼段
///////////////////////////////////////////////////////////////////////////////////////////以下是獲取HTML代碼段中所有<img>標籤裏的src屬性裏的路徑
// 定義正則表達式用來匹配 img 標籤
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImg.Matches(text);
int i = 0;
string[] UrlList = new string[matches.Count];
// 取得匹配項列表
foreach (Match match in matches)
{
UrlList[i++] = match.Groups["imgUrl"].Value;
}
C#中使用 正則表達式 替換img中src路徑但保留圖片名
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.