C#中使用 正则表达式 替换img中src路径但保留图片名

text = Regex.Replace(text, @"(?i)(?<=<img\b[^>]*?src=\s*(['""]?))([^'""]*/)+(?=[^'""/]+\1)", "/Images/");
//text 为HTML代码段



///////////////////////////////////////////////////////////////////////////////////////////以下是获取HTML代码段中所有<img>标签里的src属性里的路径
 // 定义正则表达式用来匹配 img 标签             
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串             
            MatchCollection matches = regImg.Matches(text);

            int i = 0;
            string[] UrlList = new string[matches.Count];

            // 取得匹配项列表             
            foreach (Match match in matches)
            {
                UrlList[i++] = match.Groups["imgUrl"].Value;
            }
posted @ 2016-10-05 01:05  林十八  阅读(525)  评论(0)    收藏  举报