正则表达式筛选数据(C#)

I永超 2013-07-19 02:29:05
<!-- <table class="uiGrid subscriptionsTable" cellspacing="0" cellpadding="0"><tbody><tr><td class="vTop labelItem"><span class="fsxl fwb">人名</span></td><td class="vTop contentsItem"><div class="fbProfileBrowser"><div class="standardLayout"><div class="listView clearfix" id="u_0_2j"><img class="throbber img" src="https://fbstatic-a.akamaihd.net/rsrc.php/v2/y9/r/jKEcVPZFk-2.gif" alt="" width="32" height="32" /><div class="lists"><div class="fbProfileBrowserResult hideSummary" id="u_0_26"><div class="pvm phs fbProfileBrowserSummaryBox uiBoxWhite bottomborder"></div><div class="fbProfileBrowserList fbProfileBrowserListContainer fbProfileBrowserNoMoreItems"><ul class="uiList clearfix _4kg _4ks"><li class="followListItem fbProfileBrowserListItem"><div class="clearfix"><a class="_8o _8t lfloat" href="https://www.facebook.com/karma.dawa.7?fref=pb" tabindex="-1" aria-hidden="true"><img class="_s0 _rw img" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/203182_100003717698520_1238461149_q.jpg" alt="" /></a><div class="clearfix _42ef"><div class="_6a rfloat"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><span class="_52m- _3lt"><a class="hidden_elem uiButton" href="#" role="button" ajaxify="/ajax/follow/follow_profile.php?profile_id=100003717698520&location=4" rel="async-post" id="u_0_2f"><i class="mrs img sp_2r8qut sx_52cbe4"></i><span class="uiButtonText">关注</span></a><label class="profileFollowButton _52nf _3spg uiButton" id="u_0_2g" for="u_0_2i"><i class="mrs img sp_2r8qut sx_be022e"></i><input value="关注" aria-haspopup="1" data-profileid="100003717698520" type="submit" id="u_0_2i" /></label></span></div></div><div class="uiProfileBlockContent"><div class="_6a"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><div><span class="fsl fwb fcb"><a href="https://www.facebook.com/karma.dawa.7?fref=st" data-hovercard="/ajax/hovercard/user.php?id=100003717698520">杜汶澤 <span class="alternate_name">(Karma Dawa)</span></a></span> <div class="inlineBlock"><span class="fsm fwn fcg">· 304,809 位关注者</span></div></div><ul class="uiList _4kg"><li><span class="_50hf fsm fwn">在 <a class="uiLinkSubtle" href="https://www.facebook.com/pages/EEG-%E8%8B%B1%E7%9A%87%E5%A8%9B%E6%A8%82/190788624279794">EEG (英皇娛樂)</a> 任 <a class="uiLinkSubtle" href="https://www.facebook.com/pages/ProducerArtist/143848955644597">Producer/Artist</a></span></li></ul></div></div></div></div></div></li><li class="followListItem fbProfileBrowserListItem"><div class="clearfix"><a class="_8o _8t lfloat" href="https://www.facebook.com/holaam?fref=pb" tabindex="-1" aria-hidden="true"><img class="_s0 _rw img" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/623959_1231987616_2122202226_q.jpg" alt="" /></a><div class="clearfix _42ef"><div class="_6a rfloat"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><span class="_52m- _3lt"><a class="hidden_elem uiButton" href="#" role="button" ajaxify="/ajax/follow/follow_profile.php?profile_id=1231987616&location=4" rel="async-post" id="u_0_2b"><i class="mrs img sp_2r8qut sx_52cbe4"></i><span class="uiButtonText">关注</span></a><label class="profileFollowButton _52nf _3spg uiButton" id="u_0_2c" for="u_0_2e"><i class="mrs img sp_2r8qut sx_be022e"></i><input value="关注" aria-haspopup="1" data-profileid="1231987616" type="submit" id="u_0_2e" /></label></span></div></div><div class="uiProfileBlockContent"><div class="_6a"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><div><span class="fsl fwb fcb"><a href="https://www.facebook.com/holaam?fref=st" data-hovercard="/ajax/hovercard/user.php?id=1231987616">Jackie Fung</a></span> <div class="inlineBlock"><span class="fsm fwn fcg"> · 朋友请求已送出</span><span class="fsm fwn fcg">· 33 位关注者</span></div></div><ul class="uiList _4kg"><li><span class="_50hf fsm fwn"><a class="uiLinkSubtle" href="https://www.facebook.com/pages/Hong-Kong/113317605345751">Hong Kong</a></span></li></ul></div></div></div></div></div></li><li class="followListItem fbProfileBrowserListItem"><div class="clearfix"><a class="_8o _8t lfloat" href="https://www.facebook.com/li.sally.96?fref=pb" tabindex="-1" aria-hidden="true"><img class="_s0 _rw img" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/370802_100003238374636_486819783_q.jpg" alt="" /></a><div class="clearfix _42ef"><div class="_6a rfloat"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><span class="_52m- _3lt"><a class="hidden_elem uiButton" href="#" role="button" ajaxify="/ajax/follow/follow_profile.php?profile_id=100003238374636&location=4" rel="async-post" id="u_0_27"><i class="mrs img sp_2r8qut sx_52cbe4"></i><span class="uiButtonText">关注</span></a><label class="profileFollowButton _52nf _3spg uiButton" id="u_0_28" for="u_0_2a"><i class="mrs img sp_2r8qut sx_be022e"></i><input value="关注" aria-haspopup="1" data-profileid="100003238374636" type="submit" id="u_0_2a" /></label></span></div></div><div class="uiProfileBlockContent"><div class="_6a"><div class="_6a _6b" style="height:50px"></div><div class="_6a _6b"><div><span class="fsl fwb fcb"><a href="https://www.facebook.com/li.sally.96?fref=st" data-hovercard="/ajax/hovercard/user.php?id=100003238374636">肖丽</a></span> <div class="inlineBlock"><span class="fsm fwn fcg"> · 朋友请求已送出</span><span class="fsm fwn fcg">· 34 位关注者</span></div></div><ul class="uiList _4kg"><li><span class="_50hf fsm fwn"><a class="uiLinkSubtle" href="https://www.facebook.com/pages/Hunan-University-China/108398415858284">Hunan University, China</a></span></li></ul></div></div></div></div></div></li></ul></div></div></div></div></div></div></td></tr></tbody></table> -->
大家看一下我这段HTML源码,我要从中筛选出:杜汶澤还有https://www.facebook.com/karma.dawa.7?fref=st,等这一模块所有值还包括:肖丽和https://www.facebook.com/li.sally.96?fref=st等,请问正则表达式怎么写呀???
...全文
264 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
I永超 2013-07-19
  • 打赏
  • 举报
回复
我还要筛选到:杜汶澤这个模块所对应的ID值如: 100003717698520 求正则表达式呀???
I永超 2013-07-19
  • 打赏
  • 举报
回复
为什么筛选不到Jackie Fung呢其它的都筛选到了???
I永超 2013-07-19
  • 打赏
  • 举报
回复
private void btn_get_Click(object sender, EventArgs e) { Regex threg = new Regex(@"(?i)<a\s*href=""(?<url>[^""]*)""\s*data-hovercard=""\/ajax\/hovercard\/user\.php\?id=\d+"">(?<name>[^<]*)<", RegexOptions.ExplicitCapture); MatchCollection theMatches = threg.Matches(textBox1.Text); string result = ""; foreach (Match thematch in theMatches) { if (thematch.Length!=0) { result += thematch.Groups["url"].Value; result += thematch.Groups["name"].Value; } } textBox2.Text = result; }
shawn_yang 2013-07-19
  • 打赏
  • 举报
回复

 private void btn_get_Click(object sender, EventArgs e)
        {
            Regex threg = new Regex(@"(?i)<a\s*href=""(?<url>[^""]*)""\s*data-hovercard=""\/ajax\/hovercard\/user\.php\?id=\d+"">(?<name>[^<]*)<", RegexOptions.ExplicitCapture);
            MatchCollection theMatches = threg.Matches(textBox1.Text);
            string result = "";
            foreach (Match thematch in theMatches)
            {
                if (thematch.Length!=0)
                {
                    
                    result += thematch.Groups["url"].Value;
                    result += thematch.Groups["name"].Value;
                   
                }

            }
            textBox2.Text = result;

        }
shawn_yang 2013-07-19
  • 打赏
  • 举报
回复



private void btn_get_Click(object sender, EventArgs e)
{
Regex threg = new Regex(@"(?i)<a\s*href=""([^""]*)""\s*data-hovercard=""\/ajax\/hovercard\/user\.php\?id=\d+"">([^<]*)<", RegexOptions.ExplicitCapture);
MatchCollection theMatches = threg.Matches(textBox1.Text);
string result = "";
foreach (Match thematch in theMatches)
{
if (thematch.Length!=0)
{
result += thematch.Value.ToString();
//.Groups["title"].ToString();
}

}
textBox2.Text = result;

}
I永超 2013-07-19
  • 打赏
  • 举报
回复
用这个怎么筛选不到呢???
I永超 2013-07-19
  • 打赏
  • 举报
回复

不用也不对呀
shawn_yang 2013-07-19
  • 打赏
  • 举报
回复
试试这个 new Regex(@"(?i)<a\s*href=""([^""]*)""\s*data-hovercard=""\/ajax\/hovercard\/user\.php\?id=\d+"">([^<]*)<", RegexOptions.ExplicitCapture)
shawn_yang 2013-07-19
  • 打赏
  • 举报
回复
因为你用了@,就不需要转义符
I永超 2013-07-19
  • 打赏
  • 举报
回复

这个正则怎么有错误呀???
I永超 2013-07-19
  • 打赏
  • 举报
回复
string tempStr =File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt string patterns = @"<span[^>]*?class=(['""]?)fsl fwb fcb\1[^>]*?>\s*?<a[^>]*?href=(['""]?)([^'""]*?)\2[^>]*?>([^<>]*?)(?=<)"; var list = Regex.Matches(tempStr, patterns).OfType<Match>().Select(a => new { href = a.Groups[3].Value, text = a.Groups[4].Value, }); 这个方法是怎么输出结果呀???我要看到筛选到的结果
shawn_yang 2013-07-19
  • 打赏
  • 举报
回复
(?i)<a\s*href="([^"]*)"\s*data-hovercard="\/ajax\/hovercard\/user\.php\?id=\d+">([^<]*)<

I永超 2013-07-19
  • 打赏
  • 举报
回复

这是什么原因呀???

为什么我用这种也筛选不到呢?
  • 打赏
  • 举报
回复
可能你的.netFrame版本太低了吧
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
            string pattern = @"<span[^>]*?class=(['""]?)fsl fwb fcb\1[^>]*?>\s*?<a[^>]*?href=(['""]?)([^'""]*?)\2[^>]*?>([^<>]*?)(?=<)";
            foreach (Match m in Regex.Matches(tempStr, pattern))
            {
                string href = m.Groups[3].Value;
                string text = m.Groups[4].Value;
            }
I永超 2013-07-19
  • 打赏
  • 举报
回复

怎么有错呀???
  • 打赏
  • 举报
回复
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
            string pattern = @"<span[^>]*?class=(['""]?)fsl fwb fcb\1[^>]*?>\s*?<a[^>]*?href=(['""]?)([^'""]*?)\2[^>]*?>([^<>]*?)(?=<)";

            var list = Regex.Matches(tempStr, pattern).OfType<Match>().Select(a => new 
            { 
                href=a.Groups[3].Value,
                text=a.Groups[4].Value
            });
            /*
             * +		[0]	{ href = "https://www.facebook.com/karma.dawa.7?fref=st", text = "杜汶澤 " }	<Anonymous Type>
                +		[1]	{ href = "https://www.facebook.com/holaam?fref=st", text = "Jackie Fung" }	<Anonymous Type>
                +		[2]	{ href = "https://www.facebook.com/li.sally.96?fref=st", text = "肖丽" }	<Anonymous Type>

             */

110,552

社区成员

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

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

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