Reputation: 8636
Hi all I using the following Regex
to extract the control form a form
string MessageBody = String.Empty;
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
filePath = filePath + "WebForm8.aspx";
using (StreamReader sr = new StreamReader(filePath))
{
MessageBody = sr.ReadToEnd();
MatchCollection mLabelCollection1 = Regex.Matches(MessageBody, "(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag><.*?/asp:Label>)", RegexOptions.Singleline);
}
This is working fine but I need to extract the labels which are on the form and exclude the labels which are in some other external controls like Gridview
and all
This is my aspx
part
<asp:Label ID="lbl" runat="server"></asp:Label>
<asp:GridView runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblGrid" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I need to exclude the Gridview label in my regex
Update as per Alex
answer
<asp:Label ID="lbl" runat="server"></asp:Label>
<asp:GridView runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblGrid" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
from the above design I need to extract only the labels which are placed directly and need to exclude the labels from Gridview or any external controls the sample output after regex should be <asp:Label ID="lbl" runat="server"></asp:Label>
Upvotes: 0
Views: 170
Reputation: 43013
(?<unwanted><asp:GridView .*?>.*?(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag></asp:Label>).*?</asp:GridView>)|(?<wanted>(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag></asp:Label>))
Use the named capture group wanted
to find the labels you are looking for.
An xml parser would more suited in the current case. If you ever find other tags like GridView that bring unwanted labels, you can customize the pattern like this.
string[] unwantedTags = {"GridView", "MyOtherTag", "AnotherUnwantedTag"};<br>
string unwantedTagsPattern = "(?:" + String.Join("|",unwantedTags) + ")";
string pattern = "(?<unwanted><asp:" + unwantedTagsPattern + ".*?>.*?(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag></asp:Label>).*?</asp:" + unwantedTagsPattern + ">)|(?<wanted>(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag></asp:Label>))"
Upvotes: 1