代码:HTML code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>无标题页</title>
    <style type="text/css">
    <!--
    .list{padding:10px;font-size:13px;line-height:20px;}
    .list .list_title{height:30px;line-heigh:25px;color:black;font-size:12pt;border-bottom:solid 1px gray; background:#cccccc;}
    -->
    </style>
</head>
<body>
    <form id="form1">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>菜单文件</td>
            </tr>
        </table>  
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td align="left" valign="top" class="list">
                    <div class="list_title">国内大事<a href=''> 更多</a></div>
                    {ylcms:news type="11" number="20"}
                        <div>
                            国内大事<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                        </div>
                      <br />
                    {/ylcms}
                </td>
                <td align="left" valign="top" class="list">
                    <div class="list_title">国外大事<a href=''> 更多</a></div>
                    {ylcms:message type="11" number="20"}
                        <div>
                            国外大事<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                        </div>
                      <br />
                    {/ylcms}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top" class="list">
                    <div class="list_title">军事新闻<a href=''> 更多</a></div>
                    {ylcms:list type="11" number="20"}
                        <div>
                            军事新闻<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                        </div>
                      <br />
                    {/ylcms}
                </td>
                <td align="left" valign="top" class="list">
                    <div class="list_title">财经新闻<a href=''> 更多</a></div>
                    {ylcms:notice type="11" number="20"}
                        <div>
                            财经新闻<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                        </div>
                      <br />
                    {/ylcms}
                </td>
            </tr>
      </table>
    </form>
</body>
</html>C# code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
protected void ButtonAll_Click(object sender, EventArgs e)
    {
        //以下是标签库
        ArrayList a1 = new ArrayList();//起始标签
        a1.Add("{ylcms:news");
        a1.Add("{ylcms:message");
        a1.Add("{ylcms:list");
        a1.Add("{ylcms:notice");//现在模板只调用了以上四个,
        a1.Add("{ylcms:pic");
        a1.Add("{ylcms:link");
        a1.Add("{ylcms:aaaa");
        a1.Add("{ylcms:bbbb");
        ArrayList a2 = new ArrayList();//结束标签
        a2.Add("{/ylcms}");
        a2.Add("{/ylcms}");
        a2.Add("{/ylcms}");
        a2.Add("{/ylcms}");
        a1.Add("{/ylcms}");
        a1.Add("{/ylcms}");
        a1.Add("{/ylcms}");
        a1.Add("{/ylcms}");        string TemplatePath = HttpContext.Current.Server.MapPath("../template/index.htm");//取模板
        Encoding Code = Encoding.GetEncoding("gb2312");//定义编码方式
        StreamReader ReaderFile = null;
        string strFile =string.Empty;
        ReaderFile = new StreamReader(TemplatePath, Code);//读出模板内容
        strFile = ReaderFile.ReadToEnd();
        ReaderFile.Close();
        ReaderFile.Dispose();        string strss = "";//等每组标签内的内容
        string strTemplateLabel = "";
        for (int i = 0; i < a1.Count; i++)//特环标签库
        {
            int intStart = strFile.IndexOf(a1[i].ToString());
            int intEnd = strFile.IndexOf(a2[i].ToString()) +a2[i].ToString().Length;
            strss += strTemplateLabel+"<hr>";
        }
        alnewContent.Clear();
        a1.Clear();
        a2.Clear();
        Response.Write(strss);补充一下:我想要取出每个{ ........}之间的数据.
结果显示如下:
{ylcms:notice type="11" number="20"}
  <div>
  财经新闻<a href="(ylcms:Url/)">  
  (ylcms:Title/)</a>
  <span>(ylcms:Date/)</span>
  </div>   
{/ylcms}
.......
请各位高手帮忙一下,谢谢谢谢 
 

解决方案 »

  1.   

    咦 你还在C#版发了一次
     string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
                Regex reg = new Regex(@"(?is)\{ylcms[^\}]+?\}.*?\{/ylcms\}");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Value);
                    Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
                }
      

  2.   


                MatchCollection matches = Regex.Matches("your html", @"(\{ylcms[^\}]*?\}[^\{]*?\{/ylcms\})", RegexOptions.IgnoreCase);
                foreach (Match match in matches)
                {
                    Console.WriteLine(match.Groups[1].Value);
                }
      

  3.   

    借用下一楼的代码 稍加改进
    string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
                Regex reg = new Regex(@"(?is)\{[^\}]+?\}.*?}");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Value);
                    Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
                }
      

  4.   


    老师您好:
    刚才在那边你用这种方法
    Regex reg = new Regex(@"(?is)\{ylcms[^\}]+?\}((?!\{/?ylcms).)*{/ylcms\}");
    帮我得出了页面上所有 {ylcms:.....}.....{/ylcms}的标签内容.可是把我的 {ylcms:pagetitle/}
    这个标签过滤掉了,我想要一起显示出来,
    比如页面上有:
    <table border="0" cellpadding="0" cellspacing="0">
                <tr><td>{ylcms:title /ylcms}</td>
    <td align="left" valign="top" class="list">
                        <div class="list_title">国内大事<a href=''> 更多</a></div>
                        {ylcms:news type="11" number="20"}
                            <div>
                                国内大事<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                            </div>
                          <br />
                        {/ylcms}
                    </td>
                    <td align="left" valign="top" class="list">
                        <div class="list_title">国外大事<a href=''> 更多</a></div>
                        {ylcms:message type="11" number="20"}
                            <div>
                                国外大事<a href="(ylcms:Url/)">(ylcms:Title/)</a><span>(ylcms:Date/)</span>
                            </div>
                          <br />
                        {/ylcms}
                    </td>
    </tr>
            </table>  
    我想要输出的结果是包含:
    {ylcms:pagetitle/}
    {ylcms:message type="11"...}
    .......
    {/ylcms}
    {ylcms:news type="11"...}
    .......
    {/ylcms}
    请问可以再帮一下忙吗?
      

  5.   

    试试
                string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
                Regex reg = new Regex(@"(?is)\{ylcms[^\}]+?\}(?:(?:(?!\{/?ylcms).)*{/ylcms\})?");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Value);
                    Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
                }