如何用正则表达式提取超链接中的链接和文本?

zhongkeruanjian 2006-06-01 06:19:23
比如有如下字符串:
<a id=ctl00_header_home href="http://localhost:82/Index.aspx"><font face=Tahoma color=#000080 size=2>Home</font></a><a id=index2 href="http://localhost:82/Type.aspx"><font face=Tahoma color=#000080 size=2>Type</font></a>

如何提取为如下:
文本 链接
Home http://localhost:82/Index.aspx
Type http://localhost:82/Type.aspx
...全文
1133 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken_flash 2006-06-02
  • 打赏
  • 举报
回复
帮顶
不会
add8849 2006-06-02
  • 打赏
  • 举报
回复
关注
zhongkeruanjian 2006-06-02
  • 打赏
  • 举报
回复
lai ren
zhongkeruanjian 2006-06-02
  • 打赏
  • 举报
回复
please
心情解码 2006-06-02
  • 打赏
  • 举报
回复
:)






-
zhongkeruanjian 2006-06-02
  • 打赏
  • 举报
回复
help me
zhongkeruanjian 2006-06-02
  • 打赏
  • 举报
回复
chjlcn(http://www.chenjiliang.com)

非常感谢你!

强人,测试通过,不过有点小小的问题,我开始没说清楚。

在<a></a>之间的文本外可能不只有Font,可能有还有其他标签,比如<b>,或者可能还会有<img>
用你的办法取就会把<b>这些标签一起带出来。。。

真是难为你了,有什么办法没有?
computerclass 2006-06-02
  • 打赏
  • 举报
回复
up
阿良chjlcn 2006-06-02
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
string str = @"<a id=ctl00_header_home href=""http://localhost:82/Index.aspx""><font face=Tahoma color=#000080 size=2>Home</font></a><a id=index2 href=""http://localhost:82/Type.aspx""><font face=Tahoma color=#000080 size=2>Type</font></a>";
string pattern = @"\<a.*?href=""(?<linkA>.+?)""\>\<font.*?\>(?<textA>.+?)\</font\>\</a\>\<a.*?href=""(?<linkB>.+?)""\>\<font.*?\>(?<textB>.+?)\</font\>\</a\>";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(str);
StringBuilder sb = new StringBuilder();
foreach (Match match in matches)
{
string linkA = match.Groups["linkA"].Value;
string textA = match.Groups["textA"].Value;
string linkB = match.Groups["linkB"].Value;
string textB = match.Groups["textB"].Value;

sb.AppendFormat(@"
textA = {0}, linkA = {1}
textB = {2}, linkB = {3}
",
textA, linkA, textB, linkB);
}
textBox1.Text = sb.ToString();
}



private void button2_Click(object sender, EventArgs e)
{
string str = @"<a id=ctl00_header_home href=""http://localhost:82/Index.aspx""><font face=Tahoma color=#000080 size=2>Home</font></a><a id=index2 href=""http://localhost:82/Type.aspx""><font face=Tahoma color=#000080 size=2>Type</font></a><a id=index2 href=""http://localhost:82/ccc.aspx"">ccc没有font标签</a>";
string pattern = @"\<a.*?href=""(?<link>.+?)""\>(\<font.*?\>)??(?<text>.+?)(\</font\>)??\</a\>";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(str);
StringBuilder sb = new StringBuilder();
foreach (Match match in matches)
{
string link = match.Groups["link"].Value;
string text = match.Groups["text"].Value;

sb.AppendFormat(@"
text = {0}, link = {1}
",
text, link);
}
textBox1.Text = sb.ToString();
}
lnhndx 2006-06-01
  • 打赏
  • 举报
回复
文本:"(?=\>)[^\<\>]+(?=\<)"
zhongkeruanjian 2006-06-01
  • 打赏
  • 举报
回复
而且<font>这样的字样不见得会有。。。
zhongkeruanjian 2006-06-01
  • 打赏
  • 举报
回复
chjlcn(http://www.chenjiliang.com)


谢谢,明天测一下,不过看你的表达式好像只能取两个。。如果字符串里有多个超链接,怎么办?
Yuna_2z 2006-06-01
  • 打赏
  • 举报
回复
mark
kason_j 2006-06-01
  • 打赏
  • 举报
回复
高难度.帮顶
阿良chjlcn 2006-06-01
  • 打赏
  • 举报
回复
string pattern = @"\<a.*?href="(?<linkA>.+?)"\>\<font.*?\>(?<textA>.+?)\</font\>\</a\>\<a.*?href="(?<linkB>.+?)"\>\<font.*?\>(?<textB>.+?)\</font\>\</a\>";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Match(yourText);
foreach (Match match in matches)
{
string linkA = match.Groups["linkA"].Value;
string textA = match.Groups["textA"].Value;
string linkB = match.Groups["linkB"].Value;
string textB = match.Groups["textB"].Value;
}

没有测试过,仅供参考。
anthit 2006-06-01
  • 打赏
  • 举报
回复
不懂 帮顶
dgrwang 2006-06-01
  • 打赏
  • 举报
回复
上边那个是标准的
http://(.*?)"
dgrwang 2006-06-01
  • 打赏
  • 举报
回复
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧