首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

正则表达式截取字符串中得图片地址和连接地址

2012-08-26 
求一个正则表达式截取字符串中得图片地址和连接地址img width226 height164 altcaiyu width22

求一个正则表达式截取字符串中得图片地址和连接地址
<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); } 

热点排行