java正则表达式,如何剔除HTML注释
HTML页面的内容:
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 12">
<meta name=Originator content="Microsoft Word 12">
<link rel=File-List href="54-01-01_3_2.files/filelist.xml">
<link rel=Edit-Time-Data href="54-01-01_3_2.files/editdata.mso">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<title>气发〔2001〕×号</title>
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>admin</o:Author>
</o:DocumentProperties>
</xml><![endif]-->
<link rel=themeData href="54-01-01_3_2.files/themedata.thmx">
<link rel=colorSchemeMapping href="54-01-01_3_2.files/colorschememapping.xml">
<!--[if gte mso 9]><xml>
<w:WordDocument>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
</style>
<![endif]--><!--[if gte mso 9]><xml>
</o:shapelayout></xml><![endif]-->
</head>
<body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>
<div class=Section1 style='layout-grid:30.8pt -.2pt;mso-layout-grid-char-alt:
-849'>
<p class=MsoNormal style='line-height:28.3pt;mso-line-height-rule:exactly'><!--[if gte vml 1]><v:line
id="_x0000_s1029" style='position:absolute;left:0;text-align:left;z-index:-3;
visibility:visible;mso-position-vertical-relative:page' from="-20.95pt,783.3pt"
to="460.95pt,783.3pt" strokecolor="red" strokeweight="4.5pt">
<v:stroke linestyle="thinThick"/>
</v:line><![endif]-->
</span><![endif]><!--[if gte vml 1]><v:line id="_x0000_s1027" style='position:absolute;
left:0;text-align:left;z-index:-5;visibility:visible;
mso-position-vertical-relative:page' from="-20.75pt,140.6pt" to="461.15pt,140.6pt"
strokecolor="red" strokeweight="4.5pt">
<v:stroke linestyle="thickThin"/>
</v:line><![endif]--><![if !vml]><span style='mso-ignore:vglayout;position:
absolute;z-index:-5;left:0px;margin-left:-31px;margin-top:184px;width:649px;
height:7px'><img width=649 height=7 src="54-01-01_3_2.files/image002.gif"
v:shapes="_x0000_s1027"></span><![endif]></p>
</span><![endif]></p>
<p class=MsoNormal align=center style='text-align:center;line-height:35.0pt;
mso-line-height-rule:exactly;layout-grid-mode:char'><span style='font-size:
22.0pt;mso-bidi-font-size:10.0pt;font-family:方正小标宋简体;mso-bidi-font-weight:bold'>质量检验工作改革方案的通知<span
lang=EN-US><o:p></o:p></span></span></p>
<p class=MsoNormal style='text-indent:31.6pt;mso-char-indent-count:2.0;
line-height:25.0pt;mso-line-height-rule:exactly'><span style='mso-bidi-font-size:
16.0pt;font-family:仿宋_GB2312;mso-bidi-font-family:宋体;color:black;mso-font-kerning:
0pt'>为适应精细化天气预报服务工作需求,开展精细化的天气预报质量检验工作,完善预报业务质量考核体系,有效促进天气预报准确率的提高,我司决定对现行中短期天气预报质量检验工作进行改革。主要改革内容为:<span
lang=EN-US>12</span>小时降水量预报用语严格按照<span lang=EN-US>12</span>小时降水量分级标准,<span
lang=EN-US>24</span>小时时效的检验使用<span lang=EN-US>2</span>段<span lang=EN-US>12</span>小时的预报质量检验结果。现行的由<span
lang=EN-US>2</span>段<span lang=EN-US>12</span>小时预报合成为<span lang=EN-US>24</span>小时预报进行质量检验的方法暂且保留。<span
lang=EN-US><o:p></o:p></span></span></p>
<p class=MsoNormal style='line-height:21.5pt;mso-line-height-rule:exactly;
layout-grid-mode:char'><!--[if gte vml 1]><v:shape id="文本框_x0020_2" o:spid="_x0000_s1030"
type="#_x0000_t202" style='position:absolute;left:0;text-align:left;
margin-left:182.2pt;margin-top:16.15pt;width:272.75pt;height:34.7pt;z-index:5;
visibility:visible;mso-width-relative:margin;mso-height-relative:margin'
o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOE
" stroked="f" strokecolor="white">
<v:fill opacity="0"/>
<v:textbox style='mso-next-textbox:#文本框_x0020_2'>
<![if !mso]>
<table cellpadding=0 cellspacing=0 width="100%">
<tr>
<td><![endif]>
<div>
<p class=MsoNormal align=center style='text-align:center'><span
style='font-family:仿宋_GB2312;mso-ascii-font-family:"Times New Roman"'>预报司</span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><![endif]--><![if !vml]><span style='mso-ignore:vglayout'>
<table cellpadding=0 cellspacing=0 align=left>
<tr>
<td width=242 height=21></td>
</tr>
<tr>
<td></td>
<td><img width=369 height=51 src="54-01-01_3_2.files/image003.gif"
alt="文本框: 预报司" v:shapes="文本框_x0020_2"></td>
</tr>
</table>
</span><![endif]>
<p class=MsoNormal align=right style='margin-right:63.15pt;mso-para-margin-right:
4.0gd;text-align:right;line-height:30.0pt;mso-line-height-rule:exactly;
layout-grid-mode:char'><span lang=EN-US style='font-family:仿宋_GB2312;
letter-spacing:-.3pt'>2012</span><span style='font-family:仿宋_GB2312;letter-spacing:
-.3pt'>年<span lang=EN-US>11</span>月<span lang=EN-US>29</span>日<span lang=EN-US><o:p></o:p></span></span></p>
</div>
</body>
</html>
我使用的java方法:
public static String stripHtml(String HTMLStr) {
String htmlStr = HTMLStr;
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
java.util.regex.Pattern p_o;
java.util.regex.Matcher m_o;
try {
String regEx_o = " ";//空格
p_o = Pattern.compile(regEx_o, Pattern.CASE_INSENSITIVE);
m_o = p_o.matcher(htmlStr);
htmlStr = m_o.replaceAll(" ");
regEx_o = "<\\/p>";//换行符
p_o = Pattern.compile(regEx_o, Pattern.CASE_INSENSITIVE);
m_o = p_o.matcher(htmlStr);
htmlStr = m_o.replaceAll("</p>;newline;");
regEx_o = "<\\!--.*-->";//html注释
p_o = Pattern.compile(regEx_o, Pattern.CASE_INSENSITIVE);
m_o = p_o.matcher(htmlStr);
htmlStr = m_o.replaceAll("");
String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; //脚本
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll("");
String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; //样式
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll("");
regEx_o = " ";//空格
p_o = Pattern.compile(regEx_o, Pattern.CASE_INSENSITIVE);
m_o = p_o.matcher(htmlStr);
htmlStr = m_o.replaceAll(" ");
regEx_o = "<\\/p>";//换行符
p_o = Pattern.compile(regEx_o, Pattern.CASE_INSENSITIVE);
m_o = p_o.matcher(htmlStr);
htmlStr = m_o.replaceAll("</p>;newline;");
String regEx_html = "<[^>]+>"; //其他html标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll("");
textStr = htmlStr.replaceAll(" ", "");
textStr = textStr.replaceAll(";newline;","\n");
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;
}相关推荐
fengmian123
fengmian123
QYR-18900-2026-2032全球与中国智能旋转导向钻井系统市场现状及未来发展趋势 Sample-shijunzhong.docx
QYR-18900-2026-2032全球与中国智能旋转导向钻井系统市场现状及未来发展趋势 Sample-shijunzhong.docx
XERJ 是一款受到社区信赖的本地 AI 搜索引擎
XERJ 是一款受到社区信赖的本地 AI 搜索引擎,能自动为任意文件夹建立索引,让编程智能体直接检索所需代码,省去逐个读取文件消耗的 token。它的主要用途是参考现有实现来写代码(reference coding):接到任务后,智能体下载与任务相关的开源仓库,建立索引,再查阅这些项目的解决方法。 在一项对照研究中,这种做法让智能体的输出 token 用量降至原来的约 1/2.7,同时保持 16/16 的任务完成率(案例研究)。用户在日常开发中报告的用量约为原来的 1/5(使用反馈)。有了可检索的参考实现,较小、较便宜的模型也可能胜过只凭记忆工作的昂贵模型,减少反复修改的次数。把下面这段提示词交给智能体即可开始
DocRes.Vulkan.WinForms.VS2022-Source-r1.1-20261016-102846-206.zip
DocRes.Vulkan.WinForms.VS2022-Source-r1.1-20261016-102846-206.zip 博客地址:https://blog.csdn.net/lw112190/article/details/168119884
电力系统连锁故障一种用于识别引发连锁故障的多重故障集合的“随机化学”算法研究(Matlab代码实现)
内容概要:本文研究了一种基于“随机化学”算法的多重故障集合识别方法,旨在准确识别电力系统中可能引发连锁故障的关键故障组合。该方法结合ADMM(交替方向乘子法)优化框架,建立了考虑连锁故障传播路径的N-k多阶段双层优化模型,并设计了高效的故障场景筛选机制,以应对大规模电网中连锁故障风险分析的高复杂性挑战。通过Matlab代码实现了算法的全流程仿真验证,结果表明该方法在识别高风险故障集方面具有较高的有效性与计算效率,为电力系统的安全评估、脆弱性分析及预防控制策略制定提供了先进的技术支撑。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力,从事电力系统安全稳定、连锁故障防御、风险评估等相关研究的科研人员与工程技术人员。; 使用场景及目标:①应用于大规模电力系统中高风险N-k故障场景的快速识别与排序;②支撑基于N-k安全准则的电网脆弱环节分析与主动防御策略设计;③为运行调度、规划检修及应急管理提供科学的故障预案筛选工具。; 阅读建议:建议读者结合Matlab代码与论文中的模型推导,深入理解随机化学算法在双层优化框架中的实现逻辑,重点关注ADMM的分解协调机制与故障传播路径的建模方法,并通过复现仿真案例掌握其在实际电网中的应用流程与参数设置技巧。


Java SE
