string str = "<img alt=\"\" src=\"/WebUI/Background/images/BG-header.jpg\" style=\"width:150px;height:120px; \" />"; Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None); str = re.Match(str).Value.Substring(re.Match(str).Value.LastIndexOf('/') + 1);//结果:BG-header.jpg
public string Image(string str) { string result = ""; Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None); MatchCollection mc = re.Matches(str); foreach (Match ma in mc) { result += ma.Value + ","; if (result.Length > 0) { break; } } return result.TrimEnd(','); } 这方法有问题,输出/WebUI/Background/images/BG-header.jpg
string str = "<img alt=\"\" src=\"/WebUI/Background/images/BG-header.jpg\" style=\"width:150px;height:120px; \" />";
Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None);
str = re.Match(str).Value.Substring(re.Match(str).Value.LastIndexOf('/') + 1);//结果:BG-header.jpg
public string Image(string str)
{
string result = "";
Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None);
MatchCollection mc = re.Matches(str);
foreach (Match ma in mc)
{
result += ma.Value + ",";
if (result.Length > 0)
{
break;
}
}
return result.TrimEnd(',');
} 这方法有问题,输出/WebUI/Background/images/BG-header.jpg
<img alt="" src="/WebUI/Background/images/BG-header.jpg" style="width:150px;height:120px; " />
前后有很多内容~
Regex reg = new Regex(@"(?i)<img[^>]*?src=(['""]?).*/((?:(?!\1).)*?)\1[^>]*?>");
Response.Write(reg.Match(str).Groups[2].Value);
//BG-header.jpg