日期:2014-05-17  浏览次数:21090 次

做个小偷程序,怎么用正则表达式提取内容?
结构类似如下:
<div   class= "txtAlgCen   fLeft   handle "   onClick= "detail(); "   title= "点击查看详细信息 ">
<div   class= "wetDico   w02 "> </div>
<div   class= "wetSts "> 阴转多云 </div>
<div   class= "wetTmp "> <b> 今天 </b> :22℃~31℃ </div>
</div>
<div   class= "txtAlgCen   fRig   handle "   onClick= "tomorrow(); "   title= "点击查看详细信息 ">
<div   class= "wetDico   w03 "> </div>
<div   class= "wetSts "> 阵雨 </div>
<div   class= "wetTmp "> <b> 明天 </b> :24℃~31℃ </div>
</div>
现在要提取今天和明天的三个 <div> 里面的内容,肯定要用到分组捕获.试了几次不成功,求教高人!

------解决方案--------------------
var str= 'div class= "txtAlgCen fLeft handle " onClick= "detail(); " title= "点击查看详细信息 "> <div class= "wetDico w02 "> </div> <div class= "wetSts "> 阴转多云 </div> <div class= "wetTmp "> <b> 今天 </b> :22℃~31℃ </div> </div> <div class= "txtAlgCen fRig handle " onClick= "tomorrow(); " title= "点击查看详细信息 "> <div class= "wetDico w03 "> </div> <div class= "wetSts "> 阵雨 </div> <div class= "wetTmp "> <b> 明天 </b> :24℃~31℃ </div> </div> '

var b=str.match(/ <div class=\ "wetTmp\ "\> (.*?) <\/div> /)

for(var i=0;i <b.length;i++){

alert(b[i].replace(/ <\/?[^> ]*> /g, " "))
}
------解决方案--------------------
private void button2_Click(object sender, EventArgs e)
{
CN.Open();
string Location= " ";

using (System.Data.DataTable dt = SqlHelper.ExecuteDataset(CN, CommandType.Text, @ "select top 1 * from crawled where tag=0 and HtmlInfo like '%class= " "BL TMuchBigger_2 Bold " "% ' ").Tables[0])
{

if (dt.Rows.Count > 0)
{
Location = dt.Rows[0][ "HtmlInfo "].ToString();
Regex re = new Regex(@ " <a href=(.*) class= " "BL TMuchBigger_2 Bold " " > ");
Regex re2 = new Regex(@ "[\d]{2,8} ");
if (re.IsMatch(Location))
{
MatchCollection mc;
mc = re.Matches(Location) ;


for (int i = 0; i < mc.Count; i++)
{
Location=mc[i].Value;
string L1 = Location;
Location = re2.Match(Location).Value;
MessageBox.Show(L1 + "\n " +Location);

}

}
else
{
MessageBox.Show( "无! ");
}

}

}
if (CN.State == ConnectionState.Open)