正则表达式知识详解之实战 获取网页中的邮箱地址 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识 

源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094


示例功能:

1、根据一个url,获取页面里的所有邮箱地址

步骤一、根据url读取页面内容

	/**
	 * 根据url读取网页内容
	 * @date 2016-04-27 10:34:13
	 * @author sgl
	 * @param urlStr
	 * @return
	 */
	public static String readHtml(String urlStr){
		StringBuffer sb=new StringBuffer("");
		BufferedReader br=null;
		try {
			URL url=new URL(urlStr);
			HttpURLConnection conn=(HttpURLConnection)url.openConnection();
			InputStream in=conn.getInputStream();
			br=new BufferedReader(new InputStreamReader(in));
			String line=null;
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return sb.toString();
	}



步骤二、从页面内容中找出邮箱地址

	/**
	 * 从字符串中找出邮件地址
	 * @date 2016-04-27 10:35:27
	 * @author sgl
	 * @param str
	 * @return
	 */
	public static List<String>findEmail(String str){
		Pattern p=Pattern.compile("[\\w-]+@[\\w\\.-]*\\w+\\.\\w{2,6}");
		Matcher m=p.matcher(str);
		List<String>list=new ArrayList<String>();
		while(m.find()){
			list.add(m.group());
		}
		return list;
	}

步骤三、获取邮箱地址

	public static void main(String[] args) {
		String htmlTxt=Demo02.readHtml("http://blog.sina.com.cn/s/blog_515617e60101e151.html");
		List<String>list=Demo02.findEmail(htmlTxt);
		for(String email:list){
			System.out.println(email);
		}
		System.out.println(list.size());
	}

运行结果:(中间部分省略了)

530180782@qq.com
243678025@qq.com
398018489@qq.com
....
398018489@qq.com
595064131@qq.com
362483245@qq.com
285340035@qq.com
448280012@qq.com
438










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值