匹配内容<%@ Page Language="C#" MasterPageFile="~/Content/Default.master" AutoEventWireup="true" CodeFile="Add.aspx.cs" Inherits="Temp_Add" %><asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
内容1
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PositionPlaceHolder" runat="Server">
    内容2
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainPlaceHolder" runat="Server">
    内容3
</asp:Content>
我想匹配<asp:Content>节点的内容与属性,
要求结果如下:
属性:ID="Content1" ContentPlaceHolderID="head" runat="Server"
内容:内容1我尝试着分组匹配,似乎匹配不到 string html ="字符串"
string pattern = "<asp:Content(?<Attribute>[^<]*?)>(?<content>.*?)</asp:Content>";
 foreach (Match item in Regex.Matches(html, pattern))
{Response.Write(item.Groups["Attribute"].Value+"<br/>");
Response.Write(item.Groups["content"].Value + "<br/>");
Response.Write("=============================<br/>");
}
Response.End();求大神解决方案?

解决方案 »

  1.   

    原因很明显,.是不会匹配换行的
    所以修正如下string pattern =@"<asp:Content(?<Attribute>[^<]*?)>(?<content>[\s\S]*?)</asp:Content>";
      

  2.   

    我重新修改了一下,如果asp:Content节点内容没有html就可以匹配到,如果有html就匹配不到 string html = File.ReadAllText(@"G:\ProjectManager\Project\Project\WebForm\Designer\Entitys\Add.aspx");
                string pattern = "<asp:Content(?<Attribute>[^<]*?)>(?<content>[^<asp:Content]*?)</asp:Content>";//(.*)</asp:Content>
                foreach (Match item in Regex.Matches(html, pattern))
                {
                    Response.Write("属性:"+item.Groups["Attribute"].Value+"<br/>");
                    Response.Write("内容:"+item.Groups["content"].Value + "<br/>");
                    Response.Write("======================================================<br/>");
                }           
                Response.End();