1 package regex;
2
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 public class TestReg03_email {
13 public static void main(String[] args) {
14 BufferedReader br = null;
15 StringBuilder sb = new StringBuilder();
16
17 try {
18 br = new BufferedReader(new FileReader("G:\\IoTest\\01.htm"));
19 String str = null;
20 while ((str = br.readLine()) != null) {
21 sb.append(str);
22 }
23 List<String> emails = getEmails(sb.toString());
24 for (String e : emails) {
25 System.out.println(e);
26 }
27 } catch (FileNotFoundException e) {
28 e.printStackTrace();
29 } catch (IOException e) {
30 e.printStackTrace();
31 } finally {
32 if (br != null)
33 try {
34 br.close();
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 }
39 }
40
41 public static List<String> getEmails(String str) {
42 List<String> es = new ArrayList<String>();
43 Pattern p = Pattern.compile("[\\w\\.-]*\\w+@[\\w\\.-]*\\w+\\.\\w{2,5}");
44 Matcher m = p.matcher(str);
45 while (m.find()) {
46 es.add(m.group());
47 }
48 return es;
49 }
50 }