Regex.Matches的一个正则表达试怎么写 那位 帮下忙!! 在线

wzw200 2008-09-18 07:49:37
代码是这样的
MatchCollection matches = Regex.Matches(this.HTML, "http://(\\S+[^<^>])\"");

我想用他生成一个见面文件里的所有网站连接 this.HTML是一个网页源码的文。

如 this.HTML=“http://www.baidu.com/cn”

matches=



[0]: "http://passport.baidu.com/?login&tpl=mn&u='+escape(location.href)+'"
[1]: "http://www.hao123.com"
[2]: "http://www.baidu.com')"
好像生成的 网站 有一点错误
那位能帮忙写一个好的 正则表达试??

谢谢
...全文
191 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzw200 2008-09-19
  • 打赏
  • 举报
回复
是的 我用了 你的表达式
不过不通用 有时可能出错
wzw200 2008-09-18
  • 打赏
  • 举报
回复
好的 谢谢你了 我明天再好好看看 我要下机 回家了 !
明天 有问题找你!!
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 wzw200 的回复:]
urls
{维数:[13]}
[0]: "http://news.baidu.com>新 闻 </a> <b>网 页 </"
[1]: "http://tieba.baidu.com>贴 吧 </"
[2]: "http://zhidao.baidu.com>知 道 </"
[3]: "http://mp3.baidu.com>MP3 </"
[4]: "http://image.baidu.com>图 片 </"
[5]: "http://video.baidu.com>视 频 </a> </div> </td> </tr> </tabl"
[6]: "http://hi.baidu.com>空间 </" …
[/Quote]

不会啊,我匹配出来是好的啊.

string s = File.ReadAllText("e:\\2.txt"); //我把你baidu的源码写在一个txt中,读取出来.
MatchCollection matches = Regex.Matches(s, "(?<==)http://(\\S+)(?=[>\"])"); //正则匹配
string ss = "";
foreach (Match m in matches)
{
ss += m.Value + "\r\n"; //把匹配结果写入文件
Console.WriteLine(m.Value); //输出也看一下.
}
File.WriteAllText("e:\\w.txt", ss);

写完后文件里就是18楼的结果啊.
wzw200 2008-09-18
  • 打赏
  • 举报
回复
好哎 !!

你是用的上面的那个表达试吗

我的生成用问题?
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
返回匹配为:
http://news.baidu.com
http://tieba.baidu.com
http://zhidao.baidu.com
http://mp3.baidu.com
http://image.baidu.com
http://video.baidu.com
http://hi.baidu.com
http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com
http://jingjia.baidu.com
http://top.baidu.com
http://ir.baidu.com
http://www.baidu.com/duty/
http://gimg.baidu.com/img/gs.gif
wzw200 2008-09-18
  • 打赏
  • 举报
回复
urls
{维数:[13]}
[0]: "http://news.baidu.com>新 闻</a><b>网 页</"
[1]: "http://tieba.baidu.com>贴 吧</"
[2]: "http://zhidao.baidu.com>知 道</"
[3]: "http://mp3.baidu.com>MP3</"
[4]: "http://image.baidu.com>图 片</"
[5]: "http://video.baidu.com>视 频</a></div></td></tr></tabl"
[6]: "http://hi.baidu.com>空间</"
[7]: "http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为首页</a></"
[8]: "http://jingjia.baidu.com>企业推广</"
[9]: "http://top.baidu.com>搜索风云榜</"
[10]: "http://ir.baidu.co"
[11]: "http://www.baidu.com/duty/>使用百度前必读</"
[12]: "http://gimg.baidu.com/img/gs.gif></"

错了一点点
可能 还要改一下表达式
wzw200 2008-09-18
  • 打赏
  • 举报
回复
好的 这个可以运行
我先调试几个 看看效果!
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
MatchCollection matches = Regex.Matches(this.HTML, "(?<==)http://(\\S+)(?=[>\"])");

sorry,我刚刚进C#调试了一下.你试试
wzw200 2008-09-18
  • 打赏
  • 举报
回复
你写的这个 我贴到我的代码里不行啊 不能执行

说表达试不正确
wzw200 2008-09-18
  • 打赏
  • 举报
回复
我先用你的这个试下 运行 再说!
wzw200 2008-09-18
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace MySpider
{
public class RemoteHtmlReader
{
private Encoding encoding = System.Text.Encoding.Default;
/// <summary>
/// 远程HTML的编码
/// </summary>
public Encoding Encoding
{
get { return encoding; }
set { encoding = value; }
}

private string url;
/// <summary>
/// 设置或获取远程URL
/// </summary>
public string URL
{
get { return url; }
set { url = value; }
}
private WebProxy proxy;
/// <summary>
/// 代理地址
/// </summary>
public WebProxy Proxy
{
get { return proxy; }
set { proxy = value; }
}
string HTML;
/// <summary>
/// 获取远程HTML代码
/// </summary>
/// <returns>HTML代码</returns>
public string GetHTML()
{
WebRequest request = WebRequest.Create(this.url);
request.Proxy = this.proxy;
WebResponse response = request.GetResponse();

Stream strm = response.GetResponseStream();
Encoding encode = this.encoding;// GetEncoding("utf-8");

StreamReader readStream = new StreamReader(strm, encode);

Char[] read = new Char[256];
// Reads 256 characters at a time.
int count1 = readStream.Read(read, 0, 256);

StringBuilder builder = new StringBuilder();
while (count1 > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.

String str = new String(read, 0, count1);
builder.Append(str);

count1 = readStream.Read(read, 0, 256);
//Thread.Sleep(100);
}
response.Close();

this.HTML = builder.ToString();
return builder.ToString();
}
/// <summary>
/// 获取URL的列表
/// </summary>
/// <returns></returns>
public string[] GetUrls()
{
if (this.HTML == "" || this.HTML == null)
{
this.GetHTML();
}
string Html = this.HTML;

List<string> list = new List<string>();

MatchCollection matches = Regex.Matches(this.HTML, "http://(\\S+[^<^>])\"");
foreach (Match item in matches)
{
string url = item.Value;
list.Add(url.Remove(url.Length - 1));
}
return list.ToArray();
}
/// <summary>
/// 生成静态页面
/// </summary>
/// <param name="path">本地路径</param>
public void CreateStaticPage(string path)
{
if (this.HTML == "" || this.HTML == null)
{
this.GetHTML();
}
StreamWriter sw = new StreamWriter(path, false);
sw.Write(this.HTML);
sw.Close();
}
}
}


这是一个类
用来生成 他的文件流 和 所有链接的

就是这个函数 的 public string[] GetUrls()的
MatchCollection matches = Regex.Matches(this.HTML, "http://(\\S+[^ <^>])\""); 有点错
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
是不是要加个\
MatchCollection matches = Regex.Matches(this.HTML, @"(?<==)http://(\S+)(?=[\>\"])");
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
你试试能不能符合要求?

MatchCollection matches = Regex.Matches(this.HTML, @"(?<==)http://(\S+)(?=[>"])");
wzw200 2008-09-18
  • 打赏
  • 举报
回复
好的 我先谢谢你!
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
明白了,我试试看
wzw200 2008-09-18
  • 打赏
  • 举报
回复
如果用过 迅雷的话 右键 全部链接 不知道他是怎么做到的 ?
wzw200 2008-09-18
  • 打赏
  • 举报
回复
想得到 文字的 所有超连接 网站的一个字符串 可以吗 如 http://hi.baidu.com

我的那个正则表达则 能得到 但有时会得到错的 如
[0]: "http://passport.baidu.com/?login&tpl=mn&u='+escape(location.href)+'"
[1]: "http://www.hao123.com"
[2]: "http://www.baidu.com')"


"http://www.baidu.com')"
多了一个)
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
不知道你需要匹配的原则是什么?比如下面这段文字,你需要什么结果:

<a href=http://hi.baidu.com>空间</a>  <a href="http://www.hao123.com" onmousedown="(new Image()).src='http://s.baidu.com/w.gif?fm=index&title=hao123&t='+(new Date().getTime())">hao123</a> | <a href=/more/ style="font-family:宋体">更多>></a></p>
<p style=height:60px><table cellpadding=0 cellspacing=0 id=lk><tr><td></td></tr></table></p>
<p style=height:30px><a onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href=http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为首页</a></p><p style=height:14px><a href=http://jingjia.baidu.com>企业推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=/home.html>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>©2008 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> <img src=http://gimg.baidu.com/img/gs.gif></p><map name=mp><area shape=rect coords="43,22,227,91" href=http://hi.baidu.com/baidu/ target=_blank title="点此进入 百度空间"></map></center></body></html><!--e9619a3977e1e797-->
wzw200 2008-09-18
  • 打赏
  • 举报
回复

this.HTML=
"<html><head><meta http-equiv=Content-Type content=\"text/html;charset=gb2312\"><title>百度一下,你就知道 </title><style>body{margin:4px 0}p{margin:0;padding:0}img{border:0}td,p,#u{font-size:12px}#b,#u,#l td,a{font-family:arial}#kw{font:16px Verdana;height:1.78em;padding-top:2px}#b{height:30px;padding-top:4px}#b,#b a{color:#77c}#u{padding-right:10px;line-height:19px;text-align:right;margin:0 0 3px !important;margin:0 0 10px}#sb{height:2em;width:5.6em}#km{height:50px}#l{margin:0 0 5px 15px}#l td{padding-left:107px}p,table{width:650px;border:0}#l td,#sb,#km{font-size:14px}#l a,#l b{margin-right:1.14em}a{color:#00c}a:active{color:#f60}#hp{position:absolute;margin-left:6px}#lg{margin:-26px 0 -44px}#lk{width:auto;line-height:18px;vertical-align:top}</style></head>\n<body><div id=u></div><center><img src=http://www.baidu.com/img/baidu_logo.gif width=270 height=129 usemap=\"#mp\" id=lg><br><br><br><br><table cellpadding=0 cellspacing=0 id=l><tr><td><div id=m><a onclick=s(this) href=http://news.baidu.com>新 闻</a><b>网&
nbsp;页</b><a onclick=s(this) href=http://tieba.baidu.com>贴 吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知 道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图 片</a><a onclick=s(this) href=http://video.baidu.com>视 频</a></div></td></tr></table>\n<table cellpadding=0 cellspacing=0 style=\"margin-left:15px\"><tr valign=top><td style=\"height:62px;padding-left:92px\" nowrap><form name=f action=/s><input type=text name=wd id=kw size=42 maxlength=100><script>var w=document.f.wd;w.focus();document.getElementById(\"u\").innerHTML='<a href=\"http://passport.baidu.com/?login&tpl=mn&u='+escape(location.href)+'\">登录</a>';function s(o){if(w.value.length>0){var h=o.href;var q=encodeURIComponent(w.value);if(h.indexOf(\"q=\")!=-1){o.href=h.replace(new RegExp(\"q=[^&$]*\"),\"q=\"+q)}else{o.href+=\"?q=\"+q}}};(function(){if(new RegExp(\"q=([^&]+)\").test(location.search)){w.value=decodeURIComponent(RegExp.$1)}})()</script> <input type=submit value=百
度一下 id=sb> <span id=hp><a href=/search/jiqiao.html>帮助</a><br><a href=/gaoji/advanced.html>高级</a></span></form></td></tr></table>\n<p id=km><a href=http://hi.baidu.com>空间</a>  <a href=\"http://www.hao123.com\" onmousedown=\"(new Image()).src='http://s.baidu.com/w.gif?fm=index&title=hao123&t='+(new Date().getTime())\">hao123</a> | <a href=/more/ style=\"font-family:宋体\">更多>></a></p>\n<p style=height:60px><table cellpadding=0 cellspacing=0 id=lk><tr><td></td></tr></table></p>\n<p style=height:30px><a onClick=\"this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')\" href=http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为首页</a></p><p style=height:14px><a href=http://jingjia.baidu.com>企业推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=/home.html>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>©2008 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京IC
P证030173号</a> <img src=http://gimg.baidu.com/img/gs.gif></p><map name=mp><area shape=rect coords=\"43,22,227,91\" href=http://hi.baidu.com/baidu/ target=_blank title=\"点此进入 百度空间\"></map></center></body></html><!--e9619a3977e1e797-->"

wzw200 2008-09-18
  • 打赏
  • 举报
回复
是的 楼上的 我写错了 不过 this.HTML我用其它方法 能得到 贴出来 太多了
加载更多回复(2)

110,537

社区成员

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

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

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