桂林网站建设
您的位置: 首页> 正则表达式

我要叫喊

学习本类知识需要注意某些知识点,它们之间有些关联

      技术文档
      本站业务
      本类热点
  1. c#中使用正则表...
  2. 匹配Unicode字符...
  3. 几个常用的用正...
  4. 常用正则表达式...
  5. replace or remove sc...
  6. CookBook
  7. 正则表达式入门
  8. 在VS2005中使用...
  9. Dreamweaver中的正...
  10. 多关键字替换
c#中使用正则表达式取自己想要的内容

http://zhidao.baidu.com/question/38025291.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);
}
}