C# 正则表达式 不会写

rightnet1 2012-05-23 10:54:16
<div class="c" id="C_3448822217297814">
<a href="/u/256822820?vt=4&">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class="c" id="C_3448822217297814">
<a href="/u/256822820?vt=4&">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class="c" id="C_3448822217297814">
<a href="/u/256822860?vt=4&">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>




想获取 Baby_MR_GaG 这个值。这样可以写正则吗,C#???

...全文
120 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggewt2td 2012-05-24
  • 打赏
  • 举报
回复

用 WebBrowser,
然后通过 js 的 Document对象获取
EnForGrass 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

借用ls的html代码
C# code

string html = @"<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822820?vt=4&amp"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div clas……
[/Quote]
Group
========================>
Groups
EnForGrass 2012-05-23
  • 打赏
  • 举报
回复
借用ls的html代码

string html = @"<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822820?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822820?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822860?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>";
string pattern = @"(?is)<a\s*href=""[^""]+""[^>]*>(.*?)</a>";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Group[1].Value.Trim());//去掉收尾空格
}
//或者
string pattern = @"(?is)(?<=<a\s*href=""[^""]+""[^>]*>).*?(?=</a>)";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Value.Trim());//去掉收尾空格
}
/*
Baby_MR_GaG
Baby_MR_GaG
Baby_MR_GaG
*/
newxdlysk 2012-05-23
  • 打赏
  • 举报
回复
(?is)(?<=\<a[^>]+?).+?(?=\</a\>)
EnForGrass 2012-05-23
  • 打赏
  • 举报
回复
(?is)<a\s*href="[^"]+"[^>]*>(.*?)</a>

取第一个分组即可 m.Group[1].Value.Trim()

或者这样
(?is)(?<=<a\s*href="[^"]+"[^>]*>).*?(?=</a>)
直接取m.Value.Trim();
sxldfang 2012-05-23
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace sxLdfang
{
class Program
{
static void Main(string[] args)
{
string html = @"<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822820?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822820?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>
<div class=""c"" id=""C_3448822217297814"">
<a href=""/u/256822860?vt=4&"">
Baby_MR_GaG
</a>
。。。。。。。。。。。。。。。。。好多1
</div>
<div></div>";
string pattern = @"(?m)(?<=<a href=""/u/\d+\?vt=4&"">)[^<]*(?=</a>)";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
}
}


运行结果:

Baby_MR_GaG


Baby_MR_GaG

111,126

社区成员

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

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

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