zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java使用正则抓捕网上邮箱详解编程语言

JAVA编程语言 使用 详解 正则 邮箱 网上 抓捕
2023-06-13 09:20:43 时间

java使用正则抓捕网上邮箱详解编程语言

 

import java.io.BufferedReader; 

import java.io.IOException; 

import java.io.InputStreamReader; //和网络相关的操作 

import java.net.URL; 

import java.net.URLConnection; 

import java.util.regex.Matcher; 

import java.util.regex.Pattern; 

/** * 正则抓取邮箱 * @author happy * */ 

public class Test { 

public static void main(String[] args) throws IOException { 

 //1.1 创建一个url对象 

URL url = new URL("https://www.douban.com/group/topic/8845032/"); 

 //1.2 打开连接 

URLConnection conn = url.openConnection(); 

//1.3 设置连接网络超时时间 单位为毫秒 

conn.setConnectTimeout(1000 * 10); 

//1.4 通过流 操作读取指定网络地址中的文件 

BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

String line = null; 

//1.5 匹配email的正则 

String regex = "[a-zA-Z0-9_-][email protected]//w+//.[a-z]+(//.[a-z]+)?"; 

//1.6 使用模式的compile()方法生成模式对象 

Pattern p = Pattern.compile(regex); //1. 

while((line = bufr.readLine()) != null) 

{ Matcher m = p.matcher(line); while(m.find()) 

 System.out.println(m.group());// 获得匹配的email 

 }

这里主要是利用正则来匹配邮箱:

String regex = [a-zA-Z0-9_-][email protected]//w+//.[a-z]+(//.[a-z]+)?

[a-zA-Z0-9_-]只能包含字母、数字、下划线、减号,”+ ”的话就是匹配[a-zA-Z0-9_-]一次或多次,@后可以出现任何非单字符等价于[^a-zA-Z0-9_],他可以重复出现一次或多次,结束后必须要有/.是给.做转义的,然后点后可以出现a到z之间的任意字符。

捕获的结果如下:

 

java使用正则抓捕网上邮箱详解编程语言

java使用正则抓捕网上邮箱详解编程语言

转载请注明来源网站:blog.ytso.com谢谢!

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14941.html

cjava