正则表达式如何取得url的重要信息

cuiyang_0227 2013-07-12 11:42:37
我如何利用正则表达式来取得URL的重要信息,
比如

http://agsfeke.com/haha

以上这个网址,我如何能取到agsfeke

http://www.baidu.com

这个网址 我只取到 baidu,
请正则高手帮忙指点一下,最好能有实例
先谢谢各位了
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rencht 2013-07-12
  • 打赏
  • 举报
回复
大概这样,
		String url = "http://www.wwwagsfekecom.he.com/haha";
		Pattern p = Pattern.compile("(.*?://)?(www\\.|bbs\\.)?(.*?)\\.(com|cn).*");
		Matcher m = p.matcher(url);
		while (m.find()) {
			System.out.println(m.group(3));
		}
其他的像new.或是.org等等,自己添加就是了
花木兰1闪21A 2013-07-12
  • 打赏
  • 举报
回复
 public static void main(String[] args) {
        String inputString = "http://www.baidu.com";
        Pattern pattern = Pattern.compile("(//www.|//)(.*)(.com|.net|.org)");
        Matcher matcher = pattern.matcher(inputString);
        while (matcher.find()) {
            System.out.println(matcher.group(2));
        }
    }
根据你自己的情况看着改
失落夏天 2013-07-12
  • 打赏
  • 举报
回复
http://agsfeke.com/haha http://www.baidu.com 其实也只有这两种情况吧? 带www的或者不带的? regex="http://(.*?).com.*?|http://www.(.*?).com.*?"; while(m.find()){ String str=m.group(1); } .用不用转义一下我忘了。你试试吧。
lcf 2013-07-12
  • 打赏
  • 举报
回复
这个要看你怎么定义重要信息了。。比如: http://hk.finance.yahoo.com,你要的是啥 http://del.icio.us,你要的是啥
cuiyang_0227 2013-07-12
  • 打赏
  • 举报
回复
引用 4 楼 rencht 的回复:
大概这样,
		String url = "http://www.wwwagsfekecom.he.com/haha";
		Pattern p = Pattern.compile("(.*?://)?(www\\.|bbs\\.)?(.*?)\\.(com|cn).*");
		Matcher m = p.matcher(url);
		while (m.find()) {
			System.out.println(m.group(3));
		}
其他的像new.或是.org等等,自己添加就是了
这个好用,非常感谢!!
cuiyang_0227 2013-07-12
  • 打赏
  • 举报
回复
引用 1 楼 lcf 的回复:
这个要看你怎么定义重要信息了。。比如: http://hk.finance.yahoo.com,你要的是啥 http://del.icio.us,你要的是啥
怎么跟你说呢 我要实现的功能是 通过一个网站的主页 一层一层的追迹进去,为了不追迹到与这个网站无关的网站,我限定了 如果domain数据不一样的话就不进行追迹,但是现在的问题是 有一个网站地址 http://fujifilm.jp/index.html 他的link中有一个连接地址是 http://www.fujifilm.co.jp/corporate/index.html domian地址变了,其实还是与其相关 但是追不进去了

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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