50,684
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String text = "<div class=\"music\"><a href=\"/album/241918903?pst=shoufa\">行走的力量</a></div>\n" +
"<div class=\"music\"><a href=\"/album/241920825?pst=shoufa\">不可说</a></div>\n" +
"<div class=\"music\"><a href=\"//y.baidu.com/song/213031?pst=shoufa\">不想听爱情故事</a></div>\n" +
"<div class=\"music\"><a href=\"//y.baidu.com/song/214300?pst=shoufa\">遇萤</a></div>\n" +
"<div class=\"music\"><a href=\"/album/241965534?pst=shoufa\">翻篇</a></div>";
Pattern pattern = Pattern.compile("<a.+?>(.+?)</a>");
Matcher matcher = pattern.matcher(text);
List<String> tokens = new ArrayList<String>();
while (matcher.find()) {
tokens.add(matcher.group(1));
}
System.out.println(tokens);
}
}
输出:
[行走的力量, 不可说, 不想听爱情故事, 遇萤, 翻篇]