匹配内容<%@ 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
</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();求大神解决方案?
所以修正如下string pattern =@"<asp:Content(?<Attribute>[^<]*?)>(?<content>[\s\S]*?)</asp:Content>";
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();