日期:2014-05-17  浏览次数:20520 次

求一个正则表达式截取字符串中得图片地址和连接地址
<img width="226" height="164" alt="caiyu" width="226" height="164" alt="caiyu" src="http://localhost:4677/UserDir/Images/3c7e9eec047148e9196b673998b898a5.jpg" />应用开发测试<img alt="" src="http://localhost:4677/UserDir/Images/cebd0017aa6b743e4b90a70e.jpg" />应用开发测试<a href="http://localhost:4677/UserDir/Documents/f在区域自动气象站WEB平台中的应用(英文).pdf">/UserDir/Documents/f在区域自动气象站WEB平台中的应用(英文).pdf</a>


实现效果:获得
href地址:http://localhost:4677/UserDir/Documents/f在区域自动气象站WEB平台中的应用(英文).pdf 
图片地址:http://localhost:4677/UserDir/Images/3c7e9eec047148e9196b673998b898a5.jpg
http://localhost:4677/UserDir/Images/cebd0017aa6b743e4b90a70e.jpg
并且能够将其存入数组

------解决方案--------------------
C# code

 string html = @"<img width=""226"" height=""164"" alt=""caiyu"" width=""226"" height=""164"" alt=""caiyu"" src=""http://localhost:4677/UserDir/Images/3c7e9eec047148e9196b673998b898a5.jpg"" />应用开发测试<img alt="""" src=""http://localhost:4677/UserDir/Images/cebd0017aa6b743e4b90a70e.jpg"" />应用开发测试<a href=""http://localhost:4677/UserDir/Documents/f在区域自动气象站WEB平台中的应用(英文).pdf"">/UserDir/Documents/f在区域自动气象站WEB平台中的应用(英文).pdf</a>";
            var regimg = new Regex(@"(?<=src\="").*?(?="")", RegexOptions.IgnoreCase);
            var reghref = new Regex(@"(?<=href\="").*?(?="")", RegexOptions.IgnoreCase);
            List<string> src = new List<string>();
            List<string> href = new List<string>();
            foreach (Match r in regimg.Matches(html)) { src.Add(r.Value); }
            foreach (Match r in reghref.Matches(html)) { href.Add(r.Value); }