正则表达式,模拟网络爬虫小例子

前端之家收集整理的这篇文章主要介绍了正则表达式,模拟网络爬虫小例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package cn.zhengze;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13.  
  14. public class netbug {
  15.  
  16. /**
  17. * @param args
  18. * @throws IOException
  19. */
  20. public static void main(String[] args) throws IOException {
  21. File file = new File("mail.html");
  22. String regex = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}";
  23. List<String> mailList = getMails(file,regex);
  24. for (String mail : mailList) {
  25. System.out.println(mail);
  26.  
  27. }
  28. }
  29.  
  30. private static List<String> getMails(File file,String regex)
  31. throws IOException {
  32.  
  33. BufferedReader bufr = new BufferedReader(new FileReader(file));
  34.  
  35. Pattern p = Pattern.compile(regex);
  36. List<String> list = new ArrayList<String>();
  37. String line = null;
  38. while ((line = bufr.readLine()) != null) {
  39. Matcher m = p.matcher(line);
  40. while (m.find()) {
  41. list.add(m.group());
  42. }
  43.  
  44. }
  45. return list;
  46. }
  47.  
  48. }

猜你在找的正则表达式相关文章