<div class="content" title="2013-03-26 12:00:42">我需要的内容</div>正html 是很长的。有很多组div内容我需要全部取出。            Regex regImg = new Regex(@"<div class=""content"" title="".*"">?<imgUrl>(.*)</div>", RegexOptions.IgnoreCase);
            // 搜索匹配的字符串sHtmlText 为html内容
            MatchCollection matches = regImg.Matches(sHtmlText);            int i = 0;
            string[] sUrlList = new string[matches.Count];            // 取得匹配项列表
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;            return sUrlList;我这样写错了。求指导。
html正则提取内容

解决方案 »

  1.   

    "(?is)(?<=<div[^<>]>)[^<>]+(?</div>)"
      

  2.   


            private Match GetMatch(string input, string pattern, string find)
            {
                string _pattn = Regex.Escape(pattern);
                _pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?");
                _pattn = Regex.Replace(_pattn, @"((\\r\\n)|(\\ ))+", @"\s*", RegexOptions.Compiled);
                if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success)
                    _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)");
                else
                    _pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)");
                Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                Match m = r.Match(input);
                return m;
            }
    以上是一个采集内容的规则之一
    首先我们要定义内容的规则
    <div class="content" title="[变量]">[内容]</div>GetMatch(要采集的内容,规则,"[内容]").Groups["TARGET"].Value;
      

  3.   

    string pattern=@"(?<=<div[^>]*?class=""content""[^>]*?>).*?(?=</div>)";
      

  4.   

    调用的时候 GetMatch(要采集的内容,规则,"[内容]").Groups["TARGET"].Value;这方法的参数 不太明白
    input 是html的内容吗?
    规则 就是 整个的正则表达式吗?
    [内容] 是什么内容?我的意向是 去整个html 里面 所有 ‘我需要的内容’ 的值 
      

  5.   


    //经过不懈的努力,终于完成 谢谢各位的帮忙。
     public static string[] GetHtmlImageUrlList(string sHtmlText)
            {
                string pattern = @"<div class=[^<>]+>(?<imgUrl>[^<>]+)</div>";
                Regex regImg = new Regex(pattern, RegexOptions.IgnoreCase);
                // 搜索匹配的字符串            
                MatchCollection matches = regImg.Matches(sHtmlText);            int i = 0;
                string[] sUrlList = new string[matches.Count];            // 取得匹配项列表
                foreach (Match match in matches)
                    sUrlList[i++] = match.Groups["imgUrl"].Value;
                return sUrlList;
            }