本人正在做采集程序,采集html内容如下:
<li>
     <p class="img"><a href="/ticket_19872.html"><img src="http://pimg.damai.cn/perform/project/198/19872_n_109_144.jpg" alt="安徒生经典童话剧《卖火柴的小女孩》" title="安徒生经典童话剧《卖火柴的小女孩》" /></a></p>
<h2><a href="/ticket_19872.html">安徒生经典童话剧《卖火柴的小女孩》</a></h2>
<p>让我们一起用爱心点燃那短小的火柴,祝福小女孩儿能够找到天国的钥匙与奶奶相聚!</p>
<p class="mt10">
  时间:2012.04.07 <span class="ml20"> 场馆:<a class="c3" href="http://venue.damai.cn/venue_241.html" target="_blank">小伙伴剧场</a>  </span><span class="ml15">状态: 售票中</span> </p>
<p class="mb20 strings">票价:60 </p> 
<p class="seat">
     </p>
<p class="btn">&nbsp;<a href="/ticket_19872.html"   class="btn_buy"  >立即购买</a><a href="javascript:void(0)" pid="19872" class="collect btn_fav ml10" >添加收藏</a></p>
<p class="price">60</p> </li>我想获取标名称、时间、场馆、状态、票价五个内容,希望会写正则的高手帮忙解决一下,谢谢!

解决方案 »

  1.   

    取出标题
     string pattern = @"(?is)<h2><a href=""/ticket_19872.html"">(?<title>(.*?))</a></h2>";
     string title=Regex.Match(input,pattern).Group["title"].Value;
      

  2.   

    string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
                    string pattern = @"(?i)<h2><a[^>]+>([^<]+?)</a></h2>[\s\S]*?<p[^>]*?class=(['""]?)mt10\2[^>]*>[\s\S]*?[::]([^<]+)<span[^>]*>[\s\S]*?<a[^>]+>([^<]+)</a>[\s\S]*?</span><span[^>]*>[^::]+[::]([^<]+)</span>[\s\S]*?</p>[\s\S]*?<p[^>]*?class=\2mb20 strings\2[^>]*>[^::]+[::]([^<]+)</p>";
                    foreach (Match m in Regex.Matches(tempStr, pattern))
                    {
                        //循环输出
                        string title = m.Groups[1].Value;//安徒生经典童话剧《卖火柴的小女孩》
                        string time = m.Groups[3].Value;//2012.04.07
                        string place = m.Groups[4].Value;//小伙伴剧场
                        string state = m.Groups[5].Value;//售票中
                        string price = m.Groups[6].Value;//60
                    }
      

  3.   

      static void Main(string[] args)
                {
                    string htmlStr = @"<li>
    <p class=""img""><a href=""/ticket_19872.html""><img src=""http://pimg.damai.cn/perform/project/198/19872_n_109_144.jpg"" alt=""安徒生经典童话剧《卖火柴的小女孩》"" title=""安徒生经典童话剧《卖火柴的小女孩》"" /></a></p>
    <h2><a href=""/ticket_19872.html"">安徒生经典童话剧《卖火柴的小女孩》</a></h2>
    <p>让我们一起用爱心点燃那短小的火柴,祝福小女孩儿能够找到天国的钥匙与奶奶相聚!</p>
    <p class=""mt10"">
    时间:2012.04.07 <span class=""ml20""> 场馆:<a class=""c3"" href=""http://venue.damai.cn/venue_241.html"" target=""_blank"">小伙伴剧场</a> </span><span class=""ml15"">状态: 售票中</span> </p>
    <p class=""mb20 strings"">票价:60 </p>  
    <p class=""seat"">
    </p>
    <p class=""btn"">&nbsp;<a href=""/ticket_19872.html"" class=""btn_buy"" >立即购买</a><a href=""javascript:void(0)"" pid=""19872"" class=""collect btn_fav ml10"" >添加收藏</a></p>
    <p class=""price"">60</p> </li>
    ";                Regex re = new Regex(@"(?is)<li>\s*<p[^>]+><a[^>]+><img[^>]+></a></p>\s*<h2><a[^>]+>(?<title>.*?)</a></h2>\s*<p.*?</p>\s*<p[^>]+>(?<name>.*?)<span[^>]+>[^<]+<a[^>]+>(?<adress>.*?)</a>\s*</span><span[^>]+>(?<status>.*?)</span>.*?<p[^>]+>(?<price>.*?)</p>.*?</li>", RegexOptions.None);
                    MatchCollection mc = re.Matches(htmlStr);
                    foreach (Match ma in mc)
                    {
                        Console.WriteLine(ma.Groups["title"].Value);
                        Console.WriteLine(ma.Groups["name"].Value);
                        Console.WriteLine(ma.Groups["adress"].Value);
                        Console.WriteLine(ma.Groups["status"].Value);
                        Console.WriteLine(ma.Groups["price"].Value);                 
                    }                
                    Console.ReadLine();
                    
                    
                }
      

  4.   

     static void Main(string[] args)
                {
                    string htmlStr = @"<li>
    <p class=""img""><a href=""/ticket_19872.html""><img src=""http://pimg.damai.cn/perform/project/198/19872_n_109_144.jpg"" alt=""安徒生经典童话剧《卖火柴的小女孩》"" title=""安徒生经典童话剧《卖火柴的小女孩》"" /></a></p>
    <h2><a href=""/ticket_19872.html"">安徒生经典童话剧《卖火柴的小女孩》</a></h2>
    <p>让我们一起用爱心点燃那短小的火柴,祝福小女孩儿能够找到天国的钥匙与奶奶相聚!</p>
    <p class=""mt10"">
    时间:2012.04.07 <span class=""ml20""> 场馆:<a class=""c3"" href=""http://venue.damai.cn/venue_241.html"" target=""_blank"">小伙伴剧场</a> </span><span class=""ml15"">状态: 售票中</span> </p>
    <p class=""mb20 strings"">票价:60 </p>  
    <p class=""seat"">
    </p>
    <p class=""btn"">&nbsp;<a href=""/ticket_19872.html"" class=""btn_buy"" >立即购买</a><a href=""javascript:void(0)"" pid=""19872"" class=""collect btn_fav ml10"" >添加收藏</a></p>
    <p class=""price"">60</p> </li>
    ";                Regex re = new Regex(@"(?is)<li>\s*<p[^>]+><a[^>]+><img[^>]+></a></p>\s*<h2><a[^>]+>(?<title>.*?)</a></h2>\s*<p.*?</p>\s*<p[^>]+>\s*时间:(?<time>.*?)<span[^>]+>[^<]+<a[^>]+>(?<adress>.*?)</a>\s*</span><span[^>]+>状态:(?<status>.*?)</span>.*?<p[^>]+>票价:(?<price>.*?)</p>.*?</li>", RegexOptions.None);
                    MatchCollection mc = re.Matches(htmlStr);
                    foreach (Match ma in mc)
                    {
                        Console.WriteLine(ma.Groups["title"].Value);  //标题
                        Console.WriteLine(ma.Groups["time"].Value);  //时间
                        Console.WriteLine(ma.Groups["adress"].Value);  //地点
                        Console.WriteLine(ma.Groups["status"].Value);  //状态
                        Console.WriteLine(ma.Groups["price"].Value);    //价格              
                    }                
                    Console.ReadLine();
                    
                    
                }