正则,正则,正则,以组的形式获取有层次区别的数据

cyhcyhhychyc 2010-11-07 11:05:19

要匹配的页面:http://www.ttkdex.com/pages/wdfb/6.html
//可用下面这个方式获取网页源文件
private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}

我要获得"地区"信息和"分部"信息,要注意到"分部"是属于某个"地区"的.虽然很容易以组的形式取得各"分部"的信息,但是如何取得各"分部"对应的"地区"信息?换句话说,匹配完整个页面后,在程序处理groups时要能有足够的信息判断某个"分部"是属于哪个"地区"的.
有两个思路:
1,可以从上到下把"地区"和"分部"数据依次取到组里,而不用管对应关系,这样在程序里也可以知道相互对应关系;
2,可以按"地区"及其所属"分部"作为一整体来匹配.但是一个"地区"下的"分部"是不只一个的,也不好处理;
...全文
104 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2010-11-07
  • 打赏
  • 举报
回复
try...

string html = GetHtmlCode("http://www.ttkdex.com/pages/wdfb/2.html", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?i)(?<area>[^<>]*)</td>\s*</tr>(?:\s*<tr>(?:\s*<td[^>]*>\s*<a[^>]*>\s*<strong>(?<number>\d+)</strong></a>\s*</td>\s*<td[^>]*>\s*<a[^>]*>\s*<strong>(?<name>[^<>]*)</strong></a></div>\s*</td>)+\s*(?:<td[^>]*>\s* \s*</td>\s*<td[^>]*>\s* \s*</td>\s*)?</tr>)+");
MatchCollection mc = reg.Matches(html);
foreach (Match m in mc)
{
richTextBox2.Text += "所ù在ú地?区?:阰" + m.Groups["area"].Value + "\n--------------------\n";
foreach (Capture c in m.Groups["name"].Captures)
{
richTextBox2.Text += "公?司?分?部?名?称?:阰" + c.Value + "\n";
}
richTextBox2.Text += "======================\n\n";
}
hch126163 2010-11-07
  • 打赏
  • 举报
回复
楼主,抱歉,才看到你的采集地址!建议你下次把连接加上,或者,把颜色设置不一样!那样才明显!免得我这样的粗心人找不到!
hch126163 2010-11-07
  • 打赏
  • 举报
回复
我很明白你的意思!

你要把你采集的html 原代码贴出来!!!
做采集都是针对不同的网站配置不同的规则!


是你没仔细看我的回复!!!!

正则,肯定要有参照撒! 你这样没人帮得了你!!!!!!!

你以为有那个妞人可以写一个正则,能满足所有人的要求啊?

要是那样,我们都不用学正则了!
cyhcyhhychyc 2010-11-07
  • 打赏
  • 举报
回复
我想你没有领会我的意思,我关注的是正则表达式的书写,不是后面的程序处理.
你说要按 一个地区" 及其 所有分部 来匹配,但问题是一个地区下的分部数目是不确定的,如何编写正则来匹配?
[Quote=引用 1 楼 hch126163 的回复:]

你要把你采集的html 原代码贴出来!!!
做采集都是针对不同的网站配置不同的规则!

个人认为,你正则匹配 一个地区" 及其 所有分部 这样教好!


Regex r = new Regex(“正则规则”);
MatchCollection mc = r.Matches(strHTML);// 页面的html
if(mc!= null……
[/Quote]
hch126163 2010-11-07
  • 打赏
  • 举报
回复
你要把你采集的html 原代码贴出来!!!
做采集都是针对不同的网站配置不同的规则!

个人认为,你正则匹配 一个地区" 及其 所有分部 这样教好!


Regex r = new Regex(“正则规则”);
MatchCollection mc = r.Matches(strHTML);// 页面的html
if(mc!= null && mc.Count > 0 ){
List<地区> list = new List<地区>(); // 地区集合
foreach(Match m in mc) // 遍历没一个匹配,即 一个地区" 及其 所有分部
{
GroupCollection gc = m.Groups;
if(gc!= null && gc.Count > 1 )
{
地区 p = new 地区();
p.地区名 = gc[1].Value; // 第一组是匹配的字符串,第2组是正则中的第一个()匹配结果
for (int i = 2; i < gc.Count;i++ ) // 遍历每个匹配组,获取分布
{
p.list分部.Add(gc[i].Value);
}

}
}
return list;
}
return null;
}
-过客- 2010-11-07
  • 打赏
  • 举报
回复
晕,什么东东,超过最大字符数给自动截断了?

            string html = GetHtmlCode("http://www.ttkdex.com/pages/wdfb/2.html", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?i)(?<area>[^<>]*)</td>\s*</tr>(?:\s*<tr>(?:\s*<td[^>]*>\s*<a[^>]*>\s*<strong>(?<number>\d+)</strong></a>\s*</td>\s*<td[^>]*>\s*<a[^>]*>\s*<strong>(?<name>[^<>]*)</strong></a></div>\s*</td>)+\s*(?:<td[^>]*>\s* \s*</td>\s*<td[^>]*>\s* \s*</td>\s*)?</tr>)+");
MatchCollection mc = reg.Matches(html);
foreach (Match m in mc)
{
richTextBox2.Text += "所在地区:" + m.Groups["area"].Value + "\n--------------------\n";
foreach (Capture c in m.Groups["name"].Captures)
{
richTextBox2.Text += "公司分部名称:" + c.Value + "\n";
}
richTextBox2.Text += "======================\n\n";
}
cyhcyhhychyc 2010-11-07
  • 打赏
  • 举报
回复

不能匹配...
[Quote=引用 5 楼 lxcnn 的回复:]

try...

C# code
string html = GetHtmlCode("http://www.ttkdex.com/pages/wdfb/2.html", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?i)(?<area>[^<>]*)</td>\s*</tr>(?:\s*<tr>(?:\s*<td[^>……
[/Quote]

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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