因为有个需求,是post rss,然后获取内容,显示提要,在rss的response中,图片位置是相对的位置,所以必须要替换为完整的路径,从网上找了点资料,用正则表达式完成此功能,以下是c#的代码:
// 图片地址正确的为:www.xxx.com/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg
string s = @"(?i)(?<=src[\s]*?=[\s]*?(['""]))([^'""]+?)(?=\1)";
string imgUrl = "<img src = \"/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg\" width='1' />";
string replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");
// 以下为测试代码
imgUrl = "<img src = '/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg' width='1' />";
replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");
imgUrl = "<img src='/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg' width='1' />";
replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");
imgUrl = "<img src=\"/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg\" width='1' />";
replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");
imgUrl = "<img src = \"/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg\" width='1' />";
replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");
imgUrl = "<img src = \"/uploads/siteid_000014/userid_015422/ideas-to-promote-jewelry-party-resized-600.jpg\" width='1' />";
replacedImgUrl = Regex.Replace(imgUrl, s, "www.xxx.com$2");