正则表达式求救

xr396464010 2009-12-29 06:12:38
获取 下面内容
家用电器
手机数码
电脑产品
日用百货
<ul id="Nav">
<li ><span class="Nav_first"><a href="http://www.360buy.com/" onclick="setWebBILinkCount('index_channel_index')" onfocus="this.blur()">首 页</a></span></li>
<li ><span><a href="http://www.360buy.com/electronic.html" onclick="setWebBILinkCount('index_channel_ele')" onfocus="this.blur()">家用电器</a></span></li>
<li ><span><a href="http://www.360buy.com/digital.html" onclick="setWebBILinkCount('index_channel_mobile')" onfocus="this.blur()">手机数码</a></span></li>
<li class="curr"><span><a href="http://www.360buy.com/computer.html" onclick="setWebBILinkCount('index_channel_pc')" onfocus="this.blur()">电脑产品</a></span></li>
<li ><span><a href="http://www.360buy.com/jdlife.html" onclick="setWebBILinkCount('index_channel_jdlife')" onfocus="this.blur()">日用百货</a></span></li>
</ul>
...全文
103 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainsnowclowd 2009-12-29
  • 打赏
  • 举报
回复
這是我寫的源碼了


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

namespace FormExport
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string s = "<li > <span> <a href=\"http://www.360buy.com/electronic.html\" onclick=\"setWebBILinkCount('index_channel_ele')\" onfocus=\"this.blur()\">家用电器 </a> </span> </li>";
Regex rg = new Regex("<li > <span> <a .+>(\\w+) </a> </span> </li>");
bool b1 = Regex.IsMatch("asdf", ".*");
bool b = rg.IsMatch(s);
Match m = rg.Match(s);

string name = m.Groups[1].ToString();


}
}
}



回去睡覺了
xr396464010 2009-12-29
  • 打赏
  • 举报
回复
上面的贴我发错了,很对不起大家,没看清楚, 但是我很急希望能够,帮下我的忙呀!!
要获取 求求大家了!!!!! 我没分发贴了。。。
(1) 电脑整机首页
(2)电脑整机
(3)笔记本
(4)联想
<div id="Position" class="margin_b6"><a href="http://www.360buy.com/">首页</a> > <a href = "http://www.360buy.com/computer.aspx">电脑整机首页</a> > <a href="http://www.360buy.com/products/670-671-000-0-0-0-0-0-0-0-1-1-1.html">电脑整机</a> > <a href="http://www.360buy.com/products/670-671-672-0-0-0-0-0-0-0-1-1-1.html">笔记本</a> > <a href="http://www.360buy.com/brands/672,%e8%81%94%e6%83%b3.html">联想</a></div>
rainsnowclowd 2009-12-29
  • 打赏
  • 举报
回复
string name = m.Groups[1].ToString();
這句改下。。

我的運行結果你看看呢

m.Groups[1] {家用电器}
b true bool
rg {<li > <span> <a .+>(\w+) </a> </span> </li>}
s "<li > <span> <a href=\"http://www.360buy.com/electronic.html\" onclick=\"setWebBILinkCount('index_channel_ele')\" onfocus=\"this.blur()\">家用电器 </a> </span> </li>"

圖貼不上哦 不貼了
xr396464010 2009-12-29
  • 打赏
  • 举报
回复
楼上的不太对呀获取不到,,麻烦再帮忙看下了。。
码农的彼岸 2009-12-29
  • 打赏
  • 举报
回复
System.Text.RegularExpressions.Regex.Replace("代码", @"[\u4e00-\u9fa5\s]", string.Empty);
rainsnowclowd 2009-12-29
  • 打赏
  • 举报
回复

//Regex 類在這個命名空間中哦
using System.Text.RegularExpressions;

rainsnowclowd 2009-12-29
  • 打赏
  • 举报
回复
一定要注意空格,本來我想寫:( )* 拿這個匹配空格的,可是每個地方都只有一個空格我就直接用空格代替了。。如果少了空格就會匹配不到了
rainsnowclowd 2009-12-29
  • 打赏
  • 举报
回复
匹配应该拿这一整行进行匹配
<li > <span> <a href="http://www.360buy.com/electronic.html" onclick="setWebBILinkCount('index_channel_ele')" onfocus="this.blur()">家用电器 </a> </span> </li>


string s = "<li > <span> <a href=\"http://www.360buy.com/electronic.html\" onclick=\"setWebBILinkCount('index_channel_ele')\" onfocus=\"this.blur()\">家用电器 </a> </span> </li>"
Regex rg = new Regex("<li > <span> <a .+>(\\w+) </a> </span> </li>");


bool b = rg.IsMatch(s);//我測試時,這裡返回true

Match m = rg.Match(s);

string name = m.Groups[1];//這裡取出的值就是你想要的值了,這裡在正則表達式中就是我用()括起來的那個地方的值


silentwins 2009-12-29
  • 打赏
  • 举报
回复
@"[\u4e00-\u9fa5\s]+"
xr396464010 2009-12-29
  • 打赏
  • 举报
回复
高手看到解决下
小第这里谢过了
十八道胡同 2009-12-29
  • 打赏
  • 举报
回复

@”[u4e00-u9fa5]+“
xr396464010 2009-12-29
  • 打赏
  • 举报
回复
呵呵学习。。。
SQL77 2009-12-29
  • 打赏
  • 举报
回复
匹配中文的正则,
忘了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧