Generate a directory outline of an article with a regular expression

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
> list = new ArrayList<>();
String reg
= "<"+element+"([\\S\\s]*?)>" + "(.+?)";
Matcher m
= Pattern.compile(reg).matcher(source);
while (m.find()) {
Map
map = new HashMap<>() ;
String title
= m.group(2);
//The id I used here is to use MD5 to intercept and put the number Convert to English letters
String id = MD5.GetMD5Code(title).substring(14, 20);
//Requires that id use all English letters, html tag id only It’s ok if it starts with an English letter
for (int i = 0; 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+"" );
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;
}
}

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
> list = new ArrayList<>();
String reg
= "<"+element+"([\\S\\s]*?)>" + "(.+?)";
Matcher m
= Pattern.compile(reg).matcher(source);
while (m.find()) {
Map
map = new HashMap<>() ;
String title
= m.group(2);
//The id I used here is to use MD5 to intercept and put the number Convert to English letters
String id = MD5.GetMD5Code(title).substring(14, 20);
//Requires that id use all English letters, html tag id only It’s ok if it starts with an English letter
for (int i = 0; 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+"" );
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;
}
}

Leave a Comment

Your email address will not be published.