读书人

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

发布时间: 2013-08-01 15:23:18 作者: rapoo

正则表达式筛选数据(C#)
<!-- <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="正则表达式罗选数据(C#)" 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="正则表达式罗选数据(C#)" /></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="正则表达式罗选数据(C#)" /></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="正则表达式罗选数据(C#)" /></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等,请问正则表达式怎么写呀??? 行业数据 正则表达式 C#
[解决办法]

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>

*/

[解决办法]
可能你的.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)<a\s*href="([^"]*)"\s*data-hovercard="\/ajax\/hovercard\/user\.php\?id=\d+">([^<]*)<

正则表达式罗选数据(C#)
[解决办法]
因为你用了@,就不需要转义符
[解决办法]
试试这个
new Regex(@"(?i)<a\s*href=""([^""]*)""\s*data-hovercard=""\/ajax\/hovercard\/user\.php\?id=\d+"">([^<]*)<", RegexOptions.ExplicitCapture)
[解决办法]
正则表达式罗选数据(C#)


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;

}


[解决办法]

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;

}

读书人网 >C#

热点推荐