正则表达式

DiscussQuestions 2011-06-08 04:13:09
现在我要抓取页面
我想要用正则表达式抓取标签中的属性

比如:<a href="http://www.baidu.com" title="百度一下">百度</a>

我如何通过正则表达式获取 href值,title值,以及<a>标签的内容

急急急,在线等待!!!
...全文
113 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
talent1314 2011-06-08
  • 打赏
  • 举报
回复
http://www.jb51.net/article/21853.htm你可以参考一下这个 我觉得蛮有用的
q107770540 2011-06-08
  • 打赏
  • 举报
回复
这个可能是存在的
不过可以这样来规避:

(?i)<a[^>]*href=(['"]?)(?<href>[^'"]+)\1[^>]*title=(['"]?)(?<title>[^'"]+)\2[^>]*>(?<value>[^<]+)</a>
DiscussQuestions 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 huangwenquan123 的回复:]
引用 7 楼 q107770540 的回复:
C# code

void Main()
{
string html = @"<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*href=(['""]?)(?<href>[……

猫兄,这里的title=\1(?<title>[^'""]+)\1,\1用在这里会不会不合适?
因为有可能<a href='xxx' title=""....
[/Quote]

同问
huangwenquan123 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 q107770540 的回复:]
C# code

void Main()
{
string html = @"<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*href=(['""]?)(?<href>[^'""]+)\1[^>……
[/Quote]猫兄,这里的title=\1(?<title>[^'""]+)\1,\1用在这里会不会不合适?
因为有可能<a href='xxx' title=""....
lvyichang 2011-06-08
  • 打赏
  • 举报
回复
凑个热闹!
(?is)(?<=a.*href=).*(?=\s)|(?<=a.*title=).*?(?=[>]|[\s])|(?<=a.*>).*?(?=</)
q107770540 2011-06-08
  • 打赏
  • 举报
回复

void Main()
{
string html = @"<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*href=(['""]?)(?<href>[^'""]+)\1[^>]*title=\1(?<title>[^'""]+)\1[^>]*>(?<value>[^<]+)</a>"))
{
Console.WriteLine("href: {0} title: {1} value: {2}",m.Groups["href"].Value,m.Groups["title"].Value,m.Groups["value"].Value);
}
//href: http://www.baidu.com title: 百度一下 value: 百度

}
porschev 2011-06-08
  • 打赏
  • 举报
回复


string str = "<a href=\"http://www.baidu.com\" title=\"百度一下\">百度</a>";
Regex re = new Regex(@"<a\s*href=\""(?<href>.*?)\""\s*title=\""(?<title>.*?)\"">(?<content>.*?)</a>", RegexOptions.None);
MatchCollection mc = re.Matches(str);
foreach (Match ma in mc)
{
//ma.Groups["href"].Value 这是href,结果:http://www.baidu.com
//ma.Groups["title"].Value 这是title,结果:百度一下
//ma.Groups["content"].Value) 这是A标签里的值,结果:百度
}
laowang134 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 laowang134 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

namespace Con……
[/Quote]
结果
http://www.baidu.com
***********
百度一下
***********
百度
laowang134 2011-06-08
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string html = @":<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach (Match m in Regex.Matches(html, @"(?i)<(a)\s*[^=]+?=""(?<href>[^""]+)""\s*[^=]+?=""(?<title>[^""]+)"">(?<a>[^>]*?)</\1>"))
{
foreach (Capture c in m.Groups["href"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
foreach (Capture c in m.Groups["title"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
foreach (Capture c in m.Groups["a"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
}

}
}
}
DiscussQuestions 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
(?i)<a[^>]*href=(['"]?)(?<href>[^'"]+)\1[^>]*title=\1(?<title>[^'"]+)\1[^>]*>(?<value>[^<]+)</a>
[/Quote]

可否给个完整示例
q107770540 2011-06-08
  • 打赏
  • 举报
回复
(?i)<a[^>]*href=(['"]?)(?<href>[^'"]+)\1[^>]*title=\1(?<title>[^'"]+)\1[^>]*>(?<value>[^<]+)</a>
jiege 2011-06-08
  • 打赏
  • 举报
回复
不是吧 帮你up 学习

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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