JAVA 利用正则表达式截取img标签(封装成方法*便于调用)
//:导入包
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//...等等
public List<String> getImgSrc(String htmlStr) {
String img = "";
Pattern p_image;
Matcher m_image;
List<String> pics = new ArrayList<String>();
// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(htmlStr);
while (m_image.find()) {
img = img + "," + m_image.group();
// Matcher m =
// Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while (m.find()) {
pics.add(m.group(1));
}
}
return pics;
}
public static void main(String[] args) {
String html = "<p>wseqweqwdewqeqweqweqw<img src='ueditor/jsp/upload/image/20161117/1479366071667028023.png' title='1479366071667028023.png' alt='2016-11-16_010256.png'/></p>";
List<String> str = getImgSrc(html);
for(String h:str){
System.out.println(h);
}
}