java正则表达式,如何剔除HTML注释

小羊快跑 2013-02-18 05:28:18
下面这段html代码,我想剔除所有的标签、脚本、注释,只留下实际的文字内容,使用的java方法也贴出来了,可结果却不尽人意,剔除结果是剩下了几个字,经研究,发现是regEx_o = "<\\!--.*-->"导致的,由于<body></body>的前后都有<!---->注释,导致整个body都被截掉了,尝试这样写regEx_o = "<\\!--[^(*-->*)]-->";还是会存在问题,所有正能请教高人了!
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;
}
...全文
467 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小羊快跑 2013-02-28
  • 打赏
  • 举报
回复
谢谢各位的解答,因为忙着其他事,这个问题被搁置了,先结贴,回头再试各位给的支的招!
失落夏天 2013-02-20
  • 打赏
  • 举报
回复
引用 8 楼 suciver 的回复:
引用 6 楼 AA5279AA 的回复:desc = desc.replaceAll("<!--(.*?)-->", ""); 这样就行了。 我工程里面就这么解决的 这样的是不够的,因为注释的部分有可能多行,这种的只能去掉单行注释 html.replaceAll("<!--(.|[\r\n])*?-->","");这样的就可以把不论多行的还是单行的都去掉 ……
只要读出来的是一个字符串,多行注释也是没有关系的。 desc = desc.replaceAll("<!--(.*?)-->", ""); 这样是没有问题的。 当然换行的时候会转译成$ 这个建议处理一下, desc = desc.replaceAll("$", ""); 当然,这样肯定英雄,可以判断一下$后面连接什么再删除
suciver 2013-02-20
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
desc = desc.replaceAll("<!--(.*?)-->", ""); 这样就行了。 我工程里面就这么解决的
这样的是不够的,因为注释的部分有可能多行,这种的只能去掉单行注释 html.replaceAll("<!--(.|[\r\n])*?-->","");这样的就可以把不论多行的还是单行的都去掉
a597926661 2013-02-20
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
desc = desc.replaceAll("<!--(.*?)-->", ""); 这样就行了。 我工程里面就这么解决的
++
失落夏天 2013-02-20
  • 打赏
  • 举报
回复
desc = desc.replaceAll("<!--(.*?)-->", ""); 这样就行了。 我工程里面就这么解决的
爱T 2013-02-20
  • 打赏
  • 举报
回复
本想解决的 不过看着看着就看不下去了。。。。 就当路过了
Java_Super_Man 2013-02-20
  • 打赏
  • 举报
回复
可以用现成的库,这个比较方便 我记得名字是htmlparser 上网搜一下
coder-zzzz 2013-02-18
  • 打赏
  • 举报
回复
还有就是你的代码逻辑有点乱~~~
coder-zzzz 2013-02-18
  • 打赏
  • 举报
回复
//去除注释 regexPattern = "<!--.[^-]*(?=-->)-->"; pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(result); result = matcher.replaceAll(""); //去除标签 regexPattern = "<[^>]+>"; pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(result); result = matcher.replaceAll(""); //去除空格 regexPattern = "\\s"; pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(result); result = matcher.replaceAll(""); 处理结果:气发〔2001〕×号质量检验工作改革方案的通知为适应精细化天气预报服务工作需求,开展精细化的天气预报质量检验工作,完善预报业务质量考核体系,有效促进天气预报准确率的提高,我司决定对现行中短期天气预报质量检验工作进行改革。主要改革内容为:12小时降水量预报用语严格按照12小时降水量分级标准,24小时时效的检验使用2段12小时的预报质量检验结果。现行的由2段12小时预报合成为24小时预报进行质量检验的方法暂且保留。预报司2012年11月29日

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧