现在的位置: 首页 > 综合 > 正文

使用正则表达书实现对页面获取邮箱信息

2018年04月30日 ⁄ 综合 ⁄ 共 846字 ⁄ 字号 评论关闭

程序:public class TestEmail {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("g:/test/01.htm"));
String str = null;
StringBuilder sb = new StringBuilder();
try {
while((str=br.readLine())!=null){
sb.append(str);
}
List<String> es = getEmail(sb.toString());
for(String e:es){
System.out.println(e);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if(br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
public static List<String> getEmail(String str){
List<String> es = new ArrayList<String>();
Pattern p = Pattern.compile("[\\w\\.-]*\\w+@[\\w\\.-]*\\w+\\.\\w{2,5}");
Matcher m = p.matcher(str);
while(m.find()) {
es.add(m.group());
}
return es;
}

}

实验结果:

shelly726@Gmail.com
zach8925@gmail.com
814170056@pp.com
zach8925@gmail.com
814170056@pp.com
1064100209@qq.com
haosy2007@163.com(部分结果)

抱歉!评论已关闭.