如何使用Regex提取asp标签 [英] How can I extract the asp label using Regex
本文介绍了如何使用Regex提取asp标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我想从文本中提取完整的aspx标签语法,以便它能给我标签的确切语法。
< asp:Label ID =lblrunat =serverText =Test>< / asp:Label>
我正在使用以下正则表达式
正则表达式reg =新正则表达式(@< asp:[az] + \ * ID =(?< ID> [^] *)\\s *文本=(小于?文本> [^] *)[^ /] * />中);
使用 (StreamReader sr = new StreamReader(路径)) // 其中path是我的网址
{
while (sr.Peek()> ; = 0 )
{
MessageBody = sr.ReadLine();
MessageBody = MessageBody.TrimStart();
匹配m = reg.Match(MessageBody);
}
}
所以有人可以帮助我
按照回答张贴我试过但没用过
string MessageBody = String .Empty;
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
filePath = filePath + WebForm5.aspx;
使用(StreamReader sr = new StreamReader(filePath))
{
while (sr.Peek()> = 0 )
{
MessageBody = sr.ReadLine();
MessageBody = MessageBody.TrimStart();
string regexp = (?< openingtag>< ASP:?标签* GT)(<内容> *)(小于closingtag>)< / closingtag>< /内容>< / openingtag>中跨度>??? ;
MatchCollection match = Regex.Matches(MessageBody,regexp);
}
}
解决方案
尝试这种模式:
(?< openingtag > < asp:Label。*? > ; )(?< content > ; 。*?)(?< closingtag > < 。*?/ asp:Label > )
测试:
< asp:label id = lbl runat = 服务器 text = 测试 xmlns:asp = #unknown > 一些文字< span class =code-keyword>< / asp:label >
使用 Regex Designer [ ^ ]并且它有效!
结果:
OpeningTag:< asp:label id =lblrunat =servertext =Testxmlns:asp =#unknown>
内容:一些文字
ClosingTag:< / asp:label>
虽然 RegEx 很受欢迎模式匹配技术,但对于HTML解析/模式匹配,我发现 Html Agility Pack 更容易和更有效。
http://htmlagilitypack.codeplex.com/
Hi all I want to extract the complete aspx label syntax from the text so that it should give me the exact syntax of a label.
<asp:Label ID="lbl" runat="server" Text="Test"></asp:Label>
I am using the following regex
Regex reg = new Regex(@"<asp:[a-z]+\s*ID=(?<id>[^]*)\\s*Text=(?<text>[^]*)[^/]*/>");
using (StreamReader sr = new StreamReader(path)) // where path is my url
{
while (sr.Peek() >= 0)
{
MessageBody = sr.ReadLine();
MessageBody = MessageBody.TrimStart();
Match m = reg.Match(MessageBody);
}
}
So can some one help me
As per the answer posted I tried but didn't worked
string MessageBody = String.Empty;
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
filePath = filePath + "WebForm5.aspx";
using (StreamReader sr = new StreamReader(filePath))
{
while (sr.Peek() >= 0)
{
MessageBody = sr.ReadLine();
MessageBody = MessageBody.TrimStart();
string regexp = "(?<openingtag><asp:label.*?>)(?<content>.*?)(?<closingtag>)</closingtag></content></openingtag>";
MatchCollection match = Regex.Matches(MessageBody, regexp);
}
}
解决方案
Try this pattern:
(?<openingtag><asp:Label.*?>)(?<content>.*?)(?<closingtag><.*?/asp:Label>)
Tested on:
<asp:label id="lbl" runat="server" text="Test" xmlns:asp="#unknown">Some text</asp:label>
using Regex Designer[^] and it works!
Results:
OpeningTag: <asp:label id="lbl" runat="server" text="Test" xmlns:asp="#unknown"> Content: Some text ClosingTag: </asp:label>
Although RegEx is popular pattern matching technique, but for HTML parsing/pattern matching, I found Html Agility Pack easier and efficient.
http://htmlagilitypack.codeplex.com/
这篇关于如何使用Regex提取asp标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文