123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import 'package:video/model/video.dart';
- class RegularSearch {
- static List<Video> getRecommend(String responseBody) {
- RegExp reg = new RegExp(r'class="xing_vb">[\s\S]*?</div');
- Iterable<Match> matches = reg.allMatches(responseBody);
- List<Video> list = [];
- for (Match m in matches) {
- //groupCount返回正则表达式的分组数
- //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
- String match = m.group(0);
- //在此匹配
- list = getItems(match);
- }
- return list;
- }
- static List<Video> getItems(String match) {
- RegExp reg = new RegExp(
- 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>');
- Iterable<Match> matches = reg.allMatches(match);
- List<Video> list = [];
- for (Match m in matches) {
- //groupCount返回正则表达式的分组数
- //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
- String url = "";
- String title = "";
- String time = "";
- for (int i = 0; i < m.groupCount + 1; i++) {
- String match = m.group(i);
- switch (i) {
- case 1:
- url = match;
- break;
- case 2:
- title = match;
- break;
- case 3:
- break;
- case 4:
- time = match;
- break;
- }
- }
- Video video = new Video(url: url, title: title, time: time);
- list.add(video);
- }
- return list;
- }
- }
|