- packagecom.tan.code;
- importjava.util.regex.Pattern;
- publicclassDeleteHtml{
- //去掉文本中的html标签
- staticclassHtmlText{
- staticStringHtml2Text(StringinputString){
- StringhtmlStr=inputString;
- StringtextStr="";
- java.util.regex.Patternp_script;
- java.util.regex.Matcherm_script;
- java.util.regex.Patternp_style;
- java.util.regex.Matcherm_style;
- java.util.regex.Patternp_html;
- java.util.regex.Matcherm_html;
- java.util.regex.Patternp_html1;
- java.util.regex.Matcherm_html1;
- try{
- StringregEx_script="<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";//定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
- //}
- StringregEx_style="<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";//定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
- StringregEx_html="<[^>]+>";//定义HTML标签的正则表达式
- StringregEx_html1="<[^>]+";
- p_script=Pattern.compile(regEx_script,
- Pattern.CASE_INSENSITIVE);
- m_script=p_script.matcher(htmlStr);
- htmlStr=m_script.replaceAll("");//过滤script标签
- p_style=Pattern
- .compile(regEx_style,Pattern.CASE_INSENSITIVE);
- m_style=p_style.matcher(htmlStr);
- htmlStr=m_style.replaceAll("");//过滤style标签
- p_html=Pattern.compile(regEx_html,250)"> m_html=p_html.matcher(htmlStr);
- htmlStr=m_html.replaceAll("");//过滤html标签
- p_html1=Pattern
- .compile(regEx_html1,250)"> m_html1=p_html1.matcher(htmlStr);
- htmlStr=m_html1.replaceAll(""); textStr=htmlStr;
- }catch(Exceptione){
- System.err.println("Html2Text:"+e.getMessage());
- }
- returntextStr;//返回文本字符串
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。