ASP.NET学习社区ASP.NET学习区有问有答 如何用正则表达式取内容!?

1  /  1  页   1 跳转 查看:165

如何用正则表达式取内容!?

如何用正则表达式取内容!?

<!-- 位置导航 广告 爱问搜索 begin -->

    <table cellpadding="0" cellspacing="0" width="950">
    <tr>
    <td width="640">
        <!--位置导航开始-->
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
        <td width="150"><div id="pagelogo"><a href="http://news.sina.com.cn" target="_blank"><img src="http://i3.sinaimg.cn/home/images/logo/sina_xwzx.GIF" width="144" height="34" alt="新闻中心"/></a></div></td>
        <td width="1"><img src="http://i3.sinaimg.cn/bb/article/con_ws_006.gif" width="1" height="34" alt=""/></td>
        <td valign="bottom">
            <p class="lo_links" id="lo_links">
                <span style="float:left;"><a href="http://news.sina.com.cn/">新闻中心</a> > <a href="http://news.sina.com.cn/china/" >国内新闻</a> > <font color="#000099">正文</font>  </span><span class="medialogo" align="right"><a href="http://www.huanqiu.com/" target="_blank"><img src="http://i0.sinaimg.cn/dy/31/idx/2007/1115/U2004P1T31D103F479DT20071115114003.jpg" border=0 /></a></span>
                <span class="clearit"></span>
            </p>
        </td>
        </tr>
        </table>
        <!--位置导航结束-->
    </td>
   
    <!--广告及搜索开始-->
    <td valign="bottom" align="right">
        <table cellspacing="0" cellpadding="0">
        <tr>
        <td class="iAskLLogo"></td>
        <td align="right">
        </td>
        </tr>
        </table>
    </td>
    <!--广告及搜索结束-->
    </tr>
    </table>
   
    <!-- 位置导航 广告 爱问搜索 end -->
   
    <div class="split"></div>

    <!--正文开始-->
    <table cellpadding="0" cellspacing="0" width="950" class="vsplit">
    <tr>
    <td width="640" valign="top" class="lc_blue">
    <div class="lcBlk">
       
        <div id="artibodyTitle">
            <h1>谢长廷办公室发言人要求陈水扁退党(图)</h1>
            <div class="from_info">http://www.sina.com.cn 2008年05月05日08:10 <span class="linkRed02"><a href="http://www.huanqiu.com/" target="_blank" style="text-decoration:none;">环球网</a>  </span></div>
        </div>
       
        <!--正文内容开始-->
        <!-- google_ad_section_start -->
        <div class="artibody" id="artibody">
       
        <div align="center"><img src="http://i2.sinaimg.cn/dy/c/2008-05-05/U1874P1T1D15477752F21DT20080505081019.jpg" border="1" alt="谢长廷办公室发言人要求陈水扁退党(图)"></div><div class="f12" align="center">陈水扁</div><br> <div align="center"><img src="http://i0.sinaimg.cn/dy/c/2008-05-05/U1874P1T1D15477752F23DT20080505081019.jpg" border="1" alt="谢长廷办公室发言人要求陈水扁退党(图)"></div><div class="f12" align="center">邱义仁</div><br> 
        <p>  环球时报·环球网消息:针对台巴“外交“金援遭侵吞案,谢长廷办公室发言人赵天麟昨晚表示,他认为这件事是由“行政院副院长”邱义仁一手主导,但陈水扁知情,若按照媒体看到的各方说法,陈水扁难辞其咎,民进党正进入党改革的关键时期,他呼吁陈水扁主动离开民进党。</p>
        /********* 我要取的 **///
        </div>
        <!-- google_ad_section_end -->
       
        <!--正文内容结束-->
       
        <div align="center">
       
        </div>
        <br>   
       


        <!--关于内容的操作开始-->
               
        <!--发表评论开始-->
        <div style="margin:0 auto;text-align:right;padding:10px 25px;position:relative;">
            【<a href=http://comment4.news.sina.com.cn/comment/comment4.html?channel=gn&newsid=1-1-15477752&style=0>发表评论</a><span name="cmnt_count" cmnt_id="gn:1-1-15477752:0" p_id="1001" p_key="1-1-15477752" p_url="http://news.sina.com.cn/c/2008-05-05/081015477752.shtml" p_expara=""  show_limit="20,0" style="display:none"> _COUNT_条</span>】
        </div>
        <!--发表评论结束-->



上面的是HTML
用正则表达式取出里面的内容!
 

回复:如何用正则表达式取内容!?

为什么没人 帮下偶啊?
 

回复:如何用正则表达式取内容!?

暂时研究中。。。
 

回复:如何用正则表达式取内容!?

原问题:

有一个网页,页面代码:
<td valign="top"><table border="0" cellspacing="0" cellpadding="0">
<tr><td style="height:18px;"><b>宠物素质</b></td></tr>
<tr><td style="height:18px;">力 量:0</td></tr>
<tr><td style="height:18px;">体 质:0</td></tr>
<tr><td style="height:18px;">敏 捷:0</td></tr>
<tr><td style="height:18px;">智 慧:945</td></tr>
<tr><td style="height:18px;">耐 力:0</td></tr>
<tr><td style="height:18px;">悟 性:107</td></tr>
<tr><td style="height:18px;">攻 击:317</td></tr>
<tr><td style="height:18px;">防 御:272</td></tr>
<tr><td style="height:18px;">魔 攻:29522</td></tr>

已经把上面代码 放到 string html;中

如何用正则表达式取出:
力 量:0
体 质:0
敏 捷:0
智 慧:945
等等...
然后分别存放在 string liliang; string tizhi;






我给出的答案:

执行结果为:
力 量:0智 慧:945

页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-

transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="272px" TextMode="MultiLine"

Width="334px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
<br />
<asp:TextBox ID="TextBox2" runat="server" Height="272px" TextMode="MultiLine"

Width="334px"></asp:TextBox><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>

源代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;


public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string s=TextBox1.Text;
string liliang = return_str(s, "力 量:");
string zihui = return_str(s, "智 慧:");

TextBox2.Text = "力 量:" + liliang + "智 慧:" + zihui;
}

private string return_str(string str,string xiang)
{
//Regex r = new Regex("力 量:" + "[^<]+", RegexOptions.IgnoreCase);
Regex r = new Regex(xiang + "[^<]+", RegexOptions.IgnoreCase);
Match m;
m = r.Match(str);

string temp="";
if (m.Success)
{
temp = m.Value.Trim();
if (temp!=null&&temp != "")
{
temp = temp.Replace(xiang, "").Replace(" ","");//去掉"力 量:"等,只要数据
}
}
return temp;
}
}






其它人:

using System;
using System.Text.RegularExpressions;
using System.Collections;

class Test
{
static void Main()
{
string text =
@"<td valign=""top""><table border=""0"" cellspacing=""0"" cellpadding=""0"">
<tr><td style=""height:18px;""><b>宠物素质</b></td></tr>
<tr><td style=""height:18px;"">力 量:0</td></tr>
<tr><td style=""height:18px;"">体 质:0</td></tr>
<tr><td style=""height:18px;"">敏 捷:0</td></tr>
<tr><td style=""height:18px;"">智 慧:945</td></tr>
<tr><td style=""height:18px;"">耐 力:0</td></tr>
<tr><td style=""height:18px;"">悟 性:107</td></tr>
<tr><td style=""height:18px;"">攻 击:317</td></tr>
<tr><td style=""height:18px;"">防 御:272</td></tr>
<tr><td style=""height:18px;"">魔 攻:29522</td></tr>";
Regex r = new Regex(@"<tr><td.*?>(.*?)(\d+?)</td></tr>",
RegexOptions.Multiline | RegexOptions.IgnoreCase);

MatchCollection mc = r.Matches(text);
Hashtable ht = new Hashtable();

foreach (Match match in mc)
ht.Add(match.Groups[1].Value, match.Groups[2].Value);


foreach (DictionaryEntry de in ht)
Console.WriteLine("{0}--->{1}", de.Key, de.Value);
}
}
 

回复:如何用正则表达式取内容!?

大哥  有这么简单吗?
先要匹配DIV还要匹配ID
<div [^>]+><\/div>这是我写的匹配DIV的!
ID不会匹配 自己在想中 你那个不行
我这些数据都是动态的!!!
 
1  /  1  页   1 跳转

版权所有 ASP.NET学习门户 2.0.1214   Sitemap  

返顶部