br转换行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class Test1 {

    private static final Log log = LogFactory.getLog(Test1.class);

    public static void main(String[] args) throws Exception {
        String str = "<br> <br  > <br  /> <br/>";

        str = str.replaceAll("(?i)<br[^>]*>","\n");
        System.out.println("str = " + str);

    }
    

正则表达式解析

html转txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public static String html2text(String input) {
return html2text(input, true);
}

public static String html2text(String input, boolean replaceBR) {
// String s = nullToBlank(input).replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*",
// " ");
if (!isValid(input))
return "";
if (!replaceBR) {
input = input.replaceAll("(?i)<br[^>]*>", "br2nl").replaceAll("\n",
"br2nl");
}
String text = Jsoup.parse(input).text();
if (!replaceBR) {
text = text.replaceAll("br2nl", "\n").trim();
}
return text;
}

参考链接

正则表达式解析