业务要求:截取传过来的字符串,截取出现的<small hidden="">2</small>中间的值,进行数据库更新操作。
//需要处理的字符串(截取字段如:<small hidden="">2</small>获取中间)String divAllHtml=" <div class="item"><img src="test.jpg"<span>测试11</span> <small hidden="">8</small></div><div class="item"><img src="test.jpg"<span>测试 22</span><small hidden="">8</small></div>";//开始的下标的KEYString startKey = "startIndex";//结束的下标的KEYString endKey = "endIndex";//正则匹配需要获取的开始的字符标记,截取字段如:<small hidden="">2</small>Pattern startPattern = pile("<small hidden=\"\">");//正则匹配需要获取的结束的字符标记Pattern endPattern = pile("</small>");//开始标记对比Matcher startMatcher = startPattern.matcher(divAllHtml);//结束标记对比Matcher endMatcher = endPattern.matcher(divAllHtml);List<Map<String, Integer>> mapList = new ArrayList();//开始标记查找while (startMatcher.find() && endMatcher.find()) {Map<String, Integer> map = new HashMap<>();//<small hidden="">的结束下标map.put(startKey, startMatcher.end());//</small>的开始下标map.put(endKey, endMatcher.start());mapList.add(map);}for (int i = 0; i < mapList.size(); i++) {Map<String, Integer> tempMap = mapList.get(i);//根据前面获取的下标直接截取字段即可String smallValue= divAllHtml.substring(tempMap.get(startKey), tempMap.get(endKey));#######进行业务操作即可###########}