用到生成静态页,里面的标签匹配问题。假如有一个一个HTML模板里面有标签:[comp:link id=8] ,也有可能是[comp:name id=6]之类。现在如何用正则找出这个标签?找出之后,我要取其中的类型:也就是想知道是: link还是name,再就是取出 id=6 这个6.跪求高手。

解决方案 »

  1.   

    string s = @":[comp:link id=8] ,也有可能是[comp:name id=6]";
    MatchCollection matches = Regex.Matches(s, @"(?is)\[comp:(?<type>[^: ]+)\s+id=(?<value>\d+)\]");
    foreach (Match match in matches)
    {
    Response.Write(match.Groups["type"].Value + "<br/>");
    Response.Write(match.Groups["value"].Value + "<br/><br/>");
    }
    link
    8name
    6
      

  2.   

    如果确定文档中只存在一个结果的,就用这个:
    string s = @":[comp:link id=8] ,也有可能是[comp:name id=6]";
    Match match = Regex.Match(s, @"(?is)\[comp:(?<type>[^: ]+)\s+id=(?<value>\d+)\]");
    Response.Write(match.Groups["type"].Value + "<br/>");
    Response.Write(match.Groups["value"].Value + "<br/><br/>");
      

  3.   

    多谢!还有一个能帮忙再看看吗?{comp:newslist type=all}
                  <li><a href="[newslist:link]><span>[newslist:name]</span></a></li><li class="menu_line2"></li>
                  {/comp:newslist}你看一下标签也应该能明白意思,就是先要取出{comp:newslist 的type值,根据type值来决定 {comp:newslist  与 {/comp:newslist} 之间的内容,再取里面的[newslist 标签 后面的值,,,
    我看正则就像看天书一样还望能帮忙,多多感谢!!!
      

  4.   

    string s = @"{comp:newslist type=all}
      <li><a href=""[newslist:link]""><span>[newslist:name]</span></a></li><li class=""menu_line2""></li>
      {/comp:newslist}";
    Match match = Regex.Match(s, @"(?is)(?<={comp:newslist\s+type=)[^=} ]+(?=})");
    Response.Write(match.Value + "<br/>");
    输出:all
      

  5.   

    谢谢。
    {comp:newslist type=all}
      <li><a href="[newslist:link]><span>[newslist:name]</span></a></li><li class="menu_line2"></li>
      {/comp:newslist}这一段。也是在一段HTML模板中,也有可能有多个。你告诉我怎么取“{comp:newslist type=all} 与 {/comp:newslist}之间的内容吧。其中type=“这里不固定” 。也就是要得到 <li><a href="[newslist:link]><span>[newslist:name]</span></a></li><li class="menu_line2"></li> 这一段。我再结合你一楼的再解决。
    多谢多谢啦!因白天事情多。只有抽空来看下。晚上回去再试。
      

  6.   

    string s = @"{comp:newslist type=all}
      <li><a href=""[newslist:link]""><span>[newslist:name]</span></a></li><li class=""menu_line2""></li>
      {/comp:newslist}";
    MatchCollection matches = Regex.Matches(s, @"(?is)(?<={comp:newslist\s+type=.+?}).+?(?={/comp:newslist})");
    foreach (Match match in matches)
    Response.Write(Server.HtmlEncode(match.Value) + "<br/>");
      

  7.   

    推荐一篇我的博客
    http://blog.csdn.net/keymo_
    其中的“学习笔记11(正则表达式详解)”
    只有真正懂得了个符号的表达意义,才能写出不同需求的表达式,总不能每次都来问吧,程序员要有求知欲
      

  8.   

    如果要包含标签本身呢?也就是包含{comp:newslist type=all}和{/comp:newslist}也就是在一个HTML模板中取多个
    {comp:newslist type=all}
      <li><a href=""[newslist:link]""><span>[newslist:name]</span></a></li><li class=""menu_line2""></li>
      {/comp:newslist}
    ----
    多谢啊,我是不是太笨了啊。。以后一定要补一下正则。
      

  9.   

    string s = @"{comp:newslist type=all}
      <li><a href=""[newslist:link]""><span>[newslist:name]</span></a></li><li class=""menu_line2""></li>
      {/comp:newslist}";
    MatchCollection matches = Regex.Matches(s, @"(?is){comp:newslist\s+type=.+?}(.+?){/comp:newslist}");
    foreach (Match match in matches)
    {
    Response.Write(Server.HtmlEncode(match.Value) + "<br/>");
    Response.Write(Server.HtmlEncode(match.Groups[1].Value+"<br/><br/>"));
    }