reqularSearch.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'package:video/model/video.dart';
  2. class RegularSearch {
  3. static List<Video> getRecommend(String responseBody) {
  4. RegExp reg = new RegExp(r'class="xing_vb">[\s\S]*?</div');
  5. Iterable<Match> matches = reg.allMatches(responseBody);
  6. List<Video> list = [];
  7. for (Match m in matches) {
  8. //groupCount返回正则表达式的分组数
  9. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  10. String match = m.group(0);
  11. //在此匹配
  12. list = getItems(match);
  13. }
  14. return list;
  15. }
  16. static List<Video> getItems(String match) {
  17. RegExp reg = new RegExp(
  18. r'class="xing_vb4">[\s\S]*?href="(.*?)"[\s\S]*?target="_blank">(.*?)<span[\s\S]*?class="xing_vb5">(.*?)</span[\s\S]*?class="xing_vb6">(.*?)</span[\s\S]*?/li>');
  19. Iterable<Match> matches = reg.allMatches(match);
  20. List<Video> list = [];
  21. for (Match m in matches) {
  22. //groupCount返回正则表达式的分组数
  23. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  24. String url = "";
  25. String title = "";
  26. String time = "";
  27. for (int i = 0; i < m.groupCount + 1; i++) {
  28. String match = m.group(i);
  29. switch (i) {
  30. case 1:
  31. url = match;
  32. break;
  33. case 2:
  34. title = match;
  35. break;
  36. case 3:
  37. break;
  38. case 4:
  39. time = match;
  40. break;
  41. }
  42. }
  43. Video video = new Video(url: url, title: title, time: time);
  44. list.add(video);
  45. }
  46. return list;
  47. }
  48. }