日期:2014-05-18  浏览次数:20833 次

C# 正则取图片Url
取出如下html代码中图片的rul,src里面的内容


HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.3790.5029" name=GENERATOR></HEAD>
<BODY>
<P><IMG alt="" hspace=0 
src="D:\DM-Images\0c026216-a9cd-47c9-9ce2-62fb44cc90b3.jpg" align=baseline 
border=0><IMG alt="" hspace=0 
src="D:\DM-Images\1ac0faac-cea1-43c7-84c3-7fbcabef7bd4.jpg" align=baseline 
border=0><IMG alt="" hspace=0 
src="D:\DM-Images\5c72c41b-e8d7-4a97-ecd1-9d12144e54ec.jpg" align=baseline 
border=0></P></BODY></HTML>



------解决方案--------------------
C# code
     MatchCollection mc = Regex.Matches(htmlEditor2.Text, @"<IMG\s?src=(?<url>.*?)>");
            string s = "";
            foreach (Match m in mc)
            {
                s+= m.Groups["url"].Value;
            }

------解决方案--------------------
(?is)<IMG\b[^>]*?src=(["']?)(?<url>[^'"]*?)\1[^>]*?>
C# code

            MatchCollection mc = Regex.Matches(htmlEditor2.Text, @"(?is)<IMG\b[^>]*?src=([""']?)(?<url>[^'""]*?)\1[^>]*?>");
            List<string> s = new List<string>();
            foreach (Match m in mc)
            {
                s.Add(m.Groups["url"].Value);
            }