代码改变世界

正则获取图片路径

2010-06-24 11:04  stulife  阅读(599)  评论(0编辑  收藏  举报

      /// <summary>
        /// 正则图片路径
        /// </summary>
        /// <returns></returns>
        public static StringBuilder GetImgUrl(string text)
        {
            StringBuilder str = new StringBuilder();
            string pat = @"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>";

            Regex r = new Regex(pat, RegexOptions.Compiled);

            Match m = r.Match(text.ToLower());
            //int matchCount = 0;
            while (m.Success)
            {
                Group g = m.Groups[2];
                str.Append(g).Append(",");
                m = m.NextMatch();
            }
            return str.Replace("\"", "");
        }