做項目時碰到一個需求是提取<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());
}