<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"><a href="www.123.com">123</a><a href="www.456.com">456</a> <a href="www.789.com">789</a> </asp:Label>
<asp:ListBox ID="ListBox1" runat="server" Height="166px" Width="271px"></asp:ListBox>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"><a href="www.123.com">123</a><a href="www.456.com">456</a> <a href="www.789.com">789</a> </asp:Label>
<asp:ListBox ID="ListBox1" runat="server" Height="166px" Width="271px"></asp:ListBox>
</div>
</form>
public void Geturl()
{
List<String> li = new List<String>();
MatchCollection matches = Regex.Matches(Label1.Text, @"<\s*a\s+[^>]*href\s*=\s*[""'](?<HREF>[^""']*)[""'][^>]*>(?<IHTML>[\s\S]+?)<\s*/\s*a\s*>", RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
string s = match.Groups["IHTML"].Value + ":" + match.Groups["HREF"].Value;
li.Add(s);
ListBox1.Items.Add(s);
}
}
本文介绍了一个具体的示例,展示了如何使用C#和正则表达式从ASP.NET页面上的Label控件中提取HTML链接及其显示文本,并将这些信息添加到ListBox控件中进行展示。
2153

被折叠的 条评论
为什么被折叠?



