今天有這樣一個需求 從文章內容中截取出圖片的鏈接。
於是有了下面這個方法
public class StringUtil {
public static List<String> getHref(String param) {
List<String> results = new ArrayList<>();
//參數是 正則表達式 從 '<' 截取到 '>'
Pattern p = Pattern.compile("<(.*?)>");
Matcher m = p.matcher(param);
while (m.find()) {
results.add(m.group(1));
}
return results;
}
}