夜的第七章

ASP.NET、C#、.NET开发资源、Javascript等等为主的开发备注收藏注意警告等的一个个人博客。

« mysql远程链接数据库PowerDesigner 15 貌似已经出来了…… »

c#通用正则解析代码,抓取数据无需再写正则


 /// <summary>
        /// 返回匹配多个的集合值
        /// </summary>
        /// <param name="start">开始html tag</param>
        /// <param name="end">结束html tag</param>
        /// <param name="html">html</param>
        /// <returns></returns>
        public static IList<string> GetHtmls(string start, string end, string html)
        {
            IList<string> list = new List<string>();
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?){1}", start, end);//匹配URL的模式,并分组
                MatchCollection mc = Regex.Matches(html, pattern);//满足pattern的匹配集合
                if (mc.Count != 0)
                {
                    foreach (Match match in mc)
                    {
                        GroupCollection gc = match.Groups;
                        list.Add(gc["g"].Value);
                    }
                }
            }
            catch { }
            return list;
        }


        public static string GetHtml(string start, string end, string html)
        {
            string ret = string.Empty;
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?)?{1}", start, end);//匹配URL的模式,并分组
                ret = Regex.Match(html, pattern).Groups["g"].Value;

            }
            catch { }

            return ret;
        }

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Arwen Build 81206



©2007 - 2010 www.leadnt.com 保留一些权利吧