In the projects we are doing, it is often involved in the image processing of the article (the base64 image is saved on the server and converted to the image link of the server), and the use of h1, h2 tags to generate Table of Contents Outline, below I wrote a tool class that uses regular expressions to extract h1, h2 tags, assign id attributes to tags, and extract tag content and id static methods.
public class< span style="color: #000000;"> 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;
}
/*
* Generate article 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
Test case:
public class Test {
public static void main(String[] args) throws InterruptedException {
String content = "asdggasdhfh
AAAAAAAAAAAAAAsAAAAA
GGGGGGGGGGGGGGGGGGGGGGGGGGGGG
FFFFFFFFFFFFFFFFFFFFFFFFFFF
DDDDDDDDDDDDDDDDDDDDDDD
SSSSSSSSSSSSSSSSSSSSSS
HHHHHHHHHHHHHHHHHHHHHHHHHHH
JJJJJJJJJJJJJJJJJJJJJ
KKKKKKKKKKLLLLLLLL
" = StringUtil.generateOutline(content, "h[1-2]");
System.out.println(article.getContent());
System.out.println(article.getOutline());< br> }
}
Test results:
asdggasdhfh
AAAAAAAAAAAAAAsAAAAA
GGGGGGGGGGGGGGGGGGGGGGGGGGGGG
< h2 id="tbtwra">FFFFFFFFFFFFFFFFFFFFFFFFFFFFF
DDDDDDDDDDDDDDDDDDDDDDD
SSSSSSSSSSSSSSSSSSSSSS
HHHHHHHHHHHHHHHHHHHHHHHHHHH
JJJJJJJJJJJJJJJJJJJJJLLLLLLLLLLKK”KKLLLLKK
LLLLKK
LLKKLLKK :”yqcrrx”,”title”:”AAAAAAAAAAAAAAsAAAAA”},{“id”:”tbtwra”,”title”:”FFFFFFFFFFFFFFFFFFFFFFFFFFF”},{“id”:”qbyftd”,”title”:”SSSSSSSSSSSSSSSSSSSSSS”}, {“id”:”zfeerf”,”title”:”JJJJJJJJJJJJJJJJJJJJ”}]
public < span style="color: #0000ff;">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;
}
/*
* Generate article 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