例子:
string result = "";
string str = "测试文字<img src=\"/upload/2016-05/2016052314264421.jpg\" alt=\"\" title=\"\" />测试文字<img title=\"\" src=\"/upload/2016-05/2016052314262994.png\" alt=\"\" />测试文字<img src=\"/upload/2016-05/2016052314262994.jpg\" alt=\"\" title=\"\" />测试文字";
Regex reg = new Regex("(i?)<img.*? src=\"?(.*?\\.(jpg|gif|bmp|bnp|png))\".*? />"); //定义正则表达式
MatchCollection mc = reg.Matches(str); //在内容中匹配与正则表达式匹配的字符
foreach (Match m in mc) //循环匹配到的字符
{
int srcIndex = m.Value.IndexOf("src=\"");
string strSrc = m.Value.Substring(srcIndex);
int firstIndex = strSrc.IndexOf("\"");
int lastIndex = strSrc.IndexOf("\"", firstIndex + 1);
string src = strSrc.Substring(firstIndex + 1, lastIndex - firstIndex - 1);
string srcPath = Server.MapPath(src);
result += srcPath + "<br/>";
//Response.Write(m.Value + "<br/>");
}
Response.Write(result);