做项目时碰到一个需求是提取<img src="" />中的图片来源,
做法:
private static final String REGEX = "(?<=src=\")(.+?)(?=\")";
String str="";//要提取的字符串源 Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(str);
int count=0; while(m.find()) { count++; System.out.println("Match number "+count); System.out.println(m.toString()); System.out.println("start(): "+m.start()); System.out.println("end(): "+m.end()); }