public String setNewDescription(String descriptions) {
String message = "this is message";
Pattern filter = Pattern.compile("<div class=\"item-description\".*?>[\\s\\S]*?</div>", Pattern.CASE_INSENSITIVE);
Matcher filterMatcher = filter.matcher(descriptions);
String content = null;
if(filterMatcher.find()) {
content = filterMatcher.group();
}
StringBuffer sb = new StringBuffer(content);
sb.insert(content.lastIndexOf("</div>"), message);
return descriptions.replaceAll("<div class=\"item-description\".*?>[\\s\\S]*?</div>", sb.toString());
}
String message = "this is message";
Pattern filter = Pattern.compile("<div class=\"item-description\".*?>[\\s\\S]*?</div>", Pattern.CASE_INSENSITIVE);
Matcher filterMatcher = filter.matcher(descriptions);
String content = null;
if(filterMatcher.find()) {
content = filterMatcher.group();
}
StringBuffer sb = new StringBuffer(content);
sb.insert(content.lastIndexOf("</div>"), message);
return descriptions.replaceAll("<div class=\"item-description\".*?>[\\s\\S]*?</div>", sb.toString());
}
本文介绍了一种使用正则表达式和Java标准库修改HTML中特定div标签内容的方法。通过实例演示了如何定位并替换页面上商品描述的部分,适用于网页爬虫或内容修改的应用场景。
1052

被折叠的 条评论
为什么被折叠?



