正则达人 帮忙写个正则 匹配网址

wy811007 2012-06-21 03:41:15
<a href="/xxx/76166.shtml" target="_blank" title="xx6">
<a href="/xxx/76165.shtml" target="_blank" title="xx5">
<a href="/xxx/76164.shtml" target="_blank" title="xx4">
<a href="/xxx/76163.shtml" target="_blank" title="xx3">

帮忙写个正则
需要 匹配返回一个string[] str
里面 包括 href中的数字和title
{{76166,xx6},{76165,xx5},{76164,xx4},{76163,xx3}}
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wy811007 2012-07-05
  • 打赏
  • 举报
回复
啊哦 虽然有重复的 但还是谢谢了.. 结贴了
wy811007 2012-06-21
  • 打赏
  • 举报
回复
都是高手 谢谢了 不过这个匹配后有重复的咋弄额 我用的 bdmh 大大的
dalmeeme 2012-06-21
  • 打赏
  • 举报
回复
		string s = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6"">
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">";
MatchCollection matches = Regex.Matches(s, @"(?is)<a href=""(.*?)"".+?title=""(.*?)"">");
foreach (Match match in matches)
Console.WriteLine(match.Groups[1].Value + "\t" + match.Groups[2].Value);
EnForGrass 2012-06-21
  • 打赏
  • 举报
回复
用Dictionary<string, string>不错

string input = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6"">
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">
";
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(input, @"(?is)<a\b[^>]*?href=([""'\s]?)/xxx/(\d+)\.shtml\1[^>]*?title=([""'\s]?)([^""']*?)\3[^>]*?>"))
{
dic.Add(m.Groups[2].Value, m.Groups[4].Value);
}

foreach (var m in dic)
{
Console.WriteLine(m.Key + "\t" + m.Value);
}
bdmh 2012-06-21
  • 打赏
  • 举报
回复

StreamReader reader = new StreamReader("c:\\1.txt");
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)(?<num>\d+).shtml[^>]*title=""(?<title>[^>]*)""");
MatchCollection mc = reg.Matches(source);
string[] ss = new string[mc.Count];
for (int i = 0; i < ss.Length; i++)
{
ss[i] = mc[i].Groups["num"].Value + ":" + mc[i].Groups["title"].Value;
}

110,537

社区成员

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

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

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