最近在调用同事写的接口时,返回了一段带标签的字符串,
而我需要取到img标签中的图片地址内容,采用的事正则表达式匹配的方式
public String regexLabel(String xml,String img) {
//正则表达式
String regex = "<"+img+">(.*?)</"+img+">";
String context = "";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(xml);
while(m.find()){
int i = 1;
context = context + m.group(i) + ",";
i++;
}
if(context.length() != 0) {
context = context.substring(0,context.length()-1);
}
return context;
}
参数xml即上面返回的字符串,img即要解析的标签名。