在做的项目中,经常会牵扯到,对文章的图片处理(把base64的图片保存到服务器上,并转换为服务器的图片链接),和利用h1,h2标签,去生成目录大纲,下面我写了一个工具类,使用正则表达式提取h1,h2标签,为标签赋予id属性,并提取标签内容和id的静态方法。
public class StringUtil { private String content; private String outline; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getOutline() { return outline; } public void setOutline(String outline) { this.outline = outline; } /* * 生成文章大纲 * wmy 15:35 2019/7/5 * @Param [source,element] * @return com.raise3d.model.Article **/ public static StringUtil generateOutline(String source,String element) { StringUtil article =new StringUtil(); if (source != null && source.length() > 0) { List<Map<String,String>> list = new ArrayList<>(); String reg = "<"+element+"([\\S\\s]*?)>" + "(.+?)</" + element + ">"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { Map<String,String> map = new HashMap<>(); String title = m.group(2); //这里我使用的id是使用MD5截取并把数字转换为英文字母 String id = MD5.GetMD5Code(title).substring(14,20); //要求id用全英文字母,html的标签id只需开头是英文字母就ok for (int i = 0; i < id.length(); i++) { if (((int) id.charAt(i)) < 65) { id = id.replace(id.charAt(i),(char) ((int) id.charAt(i) + 65)); } } String table = m.group().substring(1,3); source = source.replaceAll(m.group(),"<"+table+" id=\""+id +"\">"+title+"</"+table+">"); map.put("id",id); map.put("title",title); list.add(map); } article.setContent(source); article.setOutline(JSON.toJSONString(list)); } else { article.setContent(null); article.setOutline(null); } return article; } }
测试用例:
public class Test {
public static void main(String[] args) throws InterruptedException {
String content = "<p>asdggasdhfh</p><h1 class=\"qqsbfw\" id=\"yqcrrx\">AAAAAAAAAAAAAAsAAAAA</h1><p>GGGGGGGGGGGGGGGGGGGGGGGGGGGGG</p><h2 class=\"tbtwra\" id=\"tbtwra\">FFFFFFFFFFFFFFFFFFFFFFFFFFFFF</h2><p>DDDDDDDDDDDDDDDDDDDDDDD</p><h1 class=\"qbyftd\" id=\"qbyftd\">SSSSSSSSSSSSSSSSSSSSSSSS</h1><p>HHHHHHHHHHHHHHHHHHHHHHHHHH</p><h2 id=\"zfeerf\" id=\"zfeerf\">JJJJJJJJJJJJJJJJJJJ</h2><p>KKKKKKKKKKKKKKKKKKKK</p><p>LLLLLLLLLLLLLLLLLLLLLL</p>";
StringUtil article = StringUtil.generateOutline(content,"h[1-2]");
System.out.println(article.getContent());
System.out.println(article.getOutline());
}
}
测试结果:
<p>asdggasdhfh</p><h1 id="yqcrrx">AAAAAAAAAAAAAAsAAAAA</h1><p>GGGGGGGGGGGGGGGGGGGGGGGGGGGGG</p><h2 id="tbtwra">FFFFFFFFFFFFFFFFFFFFFFFFFFFFF</h2><p>DDDDDDDDDDDDDDDDDDDDDDD</p><h1 id="qbyftd">SSSSSSSSSSSSSSSSSSSSSSSS</h1><p>HHHHHHHHHHHHHHHHHHHHHHHHHH</p><h2 id="zfeerf">JJJJJJJJJJJJJJJJJJJ</h2><p>KKKKKKKKKKKKKKKKKKKK</p><p>LLLLLLLLLLLLLLLLLLLLLL</p>[{"id":"yqcrrx","title":"AAAAAAAAAAAAAAsAAAAA"},{"id":"tbtwra","title":"FFFFFFFFFFFFFFFFFFFFFFFFFFFFF"},{"id":"qbyftd","title":"SSSSSSSSSSSSSSSSSSSSSSSS"},{"id":"zfeerf","title":"JJJJJJJJJJJJJJJJJJJ"}]