作者:
felaray (傲嬌魚)
2013-06-19 13:07:09前天開始研究RexExp,這東西令人惱羞.做了兩天以後才弄好想要的東西
所以在此分享給大家.語法為C#
說明:html是用HttpWebRequest抓出來的網頁資料,
目標是抓出<dt>...</dt>裡面的資料
string pattern = @"<dt[^>]*?>(?<word>.*?)</dt>";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(html);
int index = 0;
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
string x = groups["word"].Value.Trim();
if (x != "") //因為抓到不明空白,所以在此解決
Response.Write( x + "<BR>");
要條列序號的話就把++index加入上行
}
輸出資料
1: absolute bolometric magnitude 絕對熱星等
2: absolute zero 絕對零度,絕對零點
3: acceleration 加速度
4: acceleration of gravity 重力加速度
5: accretion 吸積
6: Achernar 水委一
7: achondrites 無球粒隕石
8: achromatic lens 消色差透鏡
9: albedo 反照率
10: Alcaid 搖光
11: Alcor 輔、開陽伴星
12: Alcyone 昂宿六
Html原始碼(節錄)
<dt><b>absolute zero 絕對零度,絕對零點 </b></dt>