使用正则表达式抓取网页中的email地址

来源:互联网 发布:vs2015能开发php吗 编辑:程序博客网 时间:2024/05/16 13:38
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;/* * 根据抓取的网页,获取其中包含的Email地址 * 主要是正则表达式应用 * */public class EmailSpider {public static void main(String[] args) {String filePath = "E:\\email.html";getEmail(filePath);}private static void getEmail(String filePath) {BufferedReader br = null;Pattern p = null;Matcher m = null;//构建邮件的正则表达式p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");try {br = new BufferedReader(new FileReader(filePath));String line = "";while((line = br.readLine()) != null) {m = p.matcher(line);if(m.find()) {System.out.println(m.group());}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if(br != null) {br.close();br = null;}} catch (IOException e) {e.printStackTrace();}}}}

1 0
原创粉丝点击