HtmlTools.java
6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.xkl.tools;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by win7 on 2017/1/15.
*/
public class HtmlTools {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
/**
* @param htmlStr
* @return 删除html, style, javascript标签 ,剩余纯文本
*/
public static String getTextFromHtml(String htmlStr) {
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
htmlStr = htmlStr.replaceAll(" ", ""); //过滤
return htmlStr.trim(); // 返回文本字符串
}
/**
* @param content
* @return 删除纯html标签,保留换行与空格
*/
public static String stripHtml(String content) {
// <p>段落替换为换行
content = content.replaceAll("<p .*?>", "\r\n");
// <br><br/>替换为换行
content = content.replaceAll("<br\\s*/?>", "\r\n");
// 去掉其它的<>之间的东西
content = content.replaceAll("\\<.*?>", "");
// 替换空格转义符
content = content.replaceAll(" ", " ");
return content;
}
public static void main(String[] args) {
String test1= "<p> <span style=\"font-size:16px;\"><strong><b style=\"line-height:20px;text-indent:-29.266666412353516px;white-space:normal;background-color:#FFFFFF;\"><b><span style=\"font-size:18px;font-family:宋体;\">血红蛋白:</span></b></b><span style=\"line-height:20px;text-indent:-29.266666412353516px;white-space:normal;background-color:#FFFFFF;font-size:18px;font-family:宋体;\"></span></strong><span style=\"font-size:18px;font-family:宋体;\"></span><span style=\"line-height:20px;text-indent:-29.266666412353516px;white-space:normal;background-color:#FFFFFF;font-size:18px;font-family:宋体;\"></span><span style=\"line-height:20px;text-indent:-29.266666412353516px;white-space:normal;background-color:#FFFFFF;font-size:18px;font-family:宋体;color:#000000;\">是一种含铁的蛋白质,其功能是运输氧和二氧化碳,维持血液酸碱平衡。</span></span> </p> <p> <span style=\"font-size:16px;\"></span><span style=\"font-size:16px;\"></span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <b style=\"text-indent:45pt;\"><span style=\"font-size:18px;font-family:宋体;\">成年男性:</span></b><span style=\"font-size:18px;font-family:宋体;\"></span><span style=\"font-size:18px;\">120g</span><span style=\"font-size:18px;font-family:宋体;\">~</span><span style=\"font-size:18px;\">160 g/L</span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <b style=\"text-indent:45pt;\"><span style=\"font-size:18px;font-family:宋体;\">成年女性:</span></b><span style=\"font-size:18px;font-family:宋体;\"></span><span style=\"font-size:18px;\">110g</span><span style=\"font-size:18px;font-family:宋体;\">~</span><span style=\"font-size:18px;\">150 g/L</span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <b style=\"text-indent:45pt;\"><span style=\"font-size:18px;font-family:宋体;\">儿 童:</span></b><span style=\"font-size:18px;font-family:宋体;\"></span><span style=\"font-size:18px;\">110g</span><span style=\"font-size:18px;font-family:宋体;\">~</span><span style=\"font-size:18px;\">160 g/L</span><span style=\"font-size:18px;\"> </span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <b style=\"text-indent:45pt;\"><span style=\"font-size:18px;font-family:宋体;\">新 生 儿:</span></b><span style=\"font-size:18px;font-family:宋体;\"></span><span style=\"font-size:18px;\">170g</span><span style=\"font-size:18px;font-family:宋体;\">~</span><span style=\"font-size:18px;\">200 g/L</span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <span style=\"font-size:18px;color:#E53333;\"><span style=\"white-space:nowrap;color:#E53333;\"><strong><br /> </strong></span></span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <span style=\"font-size:18px;color:#E53333;\"><span style=\"white-space:nowrap;color:#E53333;\"><strong>升高:缺氧、吸烟、剧烈运动后,慢性肺源性心脏病或先天性心脏病及高原病。</strong></span></span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <span style=\"font-size:18px;\"><span style=\"white-space:nowrap;color:#009900;\"><strong><br /> </strong></span></span> </p> <p style=\"margin-left:21.95pt;line-height:20px;white-space:normal;text-indent:-21.95pt;\"> <span style=\"font-size:18px;\"><span style=\"white-space:nowrap;color:#009900;\"><strong>建议:戒烟,到负氧离子浓度高的环境中调养,如公园、森林等。</strong></span><span style=\"color:#009900;\"><strong></strong></span><br /> </span> </p> <p> <span style=\"font-size:16px;\"><strong><span style=\"color:#e53333;\"> </span></strong></span> </p> <p style=\"text-indent:18.05pt;\"> <strong><b><span style=\"font-size:16px;font-family:宋体;color:red;\"><br /> </span></b> </strong> </p> <p style=\"text-indent:18.05pt;\"> <strong><br /> </strong> </p> <span style=\"font-size:16px;\"><strong></strong></span> <p> <br /> </p> <p> </p>";
String test2= "<span style=\"font-size:16px;\"><strong>总胆固醇:</strong>指血液中所有脂蛋白所含胆固醇之总和,人群总胆固醇水平主要取决于遗传因素和生活方式。</span><br />";
System.out.println(stripHtml(test1));
System.out.println(stripHtml(test2));
}
}