字体:  

求助:请帮帮忙看看这题怎么解啊~!

diyuxinlang 发表于: 2008-11-03 15:06 来源: Ruby Community

相同域名的网址中,只保留一个二级域名,其他同域名的网址删除.
例:
# 需要处理的文本文件内容为:
http://www.163.com/123/
http://news.163.com/255/
http://blog.163.com/888/
http://game.163.com/258/
http://www.google.com/d5f6/
http://google.com/d5f6/
http://map.google.com/d5f6/
http://doc.google.cn/t2y4/
http://doc.fans.com.cn/index.php
http://doc.fox.com.cn/index.php
# 处理的结果为:
http://news.163.com/255/
http://map.google.com/d5f6/
http://doc.google.cn/t2y4/
http://doc.fans.com.cn/index.php
http://doc.fox.com.cn/index.php

小弟我不善于解释,不知道这样说大家明不明白.
如果有不明白的请提出,我在做补充.
请各位高手一定要帮忙.
先谢过各位了!

最新回复

bbschat at 2008-11-03 18:03:54

CODE:

  $MY_LOCATION = Dir.getwd
  
  Dir.chdir("#{$MY_LOCATION}/in")
  $IN_FILES = Dir["*"]

  $IN_FILES.each {|in_file|
   
    $convent = IO.readlines("#{$MY_LOCATION}/in/" + in_file)
    file_out = File.new("#{$MY_LOCATION}/out/" + in_file, "w")
   
    exist_domin = []
   
    $convent.each{|ln|
      
      if ln =~ /http\:\/\/(.*?)\//
        
        full_domin = $1.split(".")
        
        next if full_domin.size <= 2
        next if full_domin.shift == "www"
        
        next if exist_domin.include?(full_domin)
        
        exist_domin.push full_domin
        
        file_out.puts ln
      end
      
    }
    file_out.close
  }
diyuxinlang at 2008-11-03 20:46:16
看了你的代码后有一种,"这样也可以啊~~!! "的感觉,呵~!
看来还得在继续学习啊~!
谢谢你,真的是帮了我的大忙了,呵~!