Java正则表达式应用

时间:2021-05-11 05:13:54

查找html中的图片

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class PicDownload { public static void main(String[] args) throws Exception{
String content = "<html><img data-src=\"test1\">cc<img data-src=\"test2\"></html>";
Pattern p = Pattern.compile("<img data-src=[\"\'](.*?)[\"\'].*?>");
Matcher m = p.matcher(content);
boolean b = m.find();
while(b){
String path = m.group(1);
System.out.println(path);
b = m.find();
}
} }

输出结果

test1
test2