- 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[^>]*?>[sS]*?<[s]*?/[s]*?script[s]*?>";//定义script的正则表达式{或<script[^>]*?>[sS]*?</script>
- //}
- StringregEx_style="<[s]*?style[^>]*?>[sS]*?<[s]*?/[s]*?style[s]*?>";//定义style的正则表达式{或<style[^>]*?>[sS]*?</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;//返回文本字符串
- }
使用正则表达式过滤HTML中标签
发布时间:2020-12-14 00:57:12 所属栏目:百科 来源:网络整理
导读:Java代码 package com.tan.code; import java.util.regex.Pattern; public class DeleteHtml{ //去掉文本中的html标签 static class HtmlText{ static StringHtml2Text(StringinputString){ StringhtmlStr=inputString; StringtextStr= "" ; java.util.regex
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 继上次读文件方法里进行筛选
- postgresql部署在WINDOWS SERVER 2008
- 无法弄清楚这些C#和Java代码的不同之处
- objective-c – SpriteKit自定义SKAction =弹跳?
- ruby-on-rails – 在rails 4中实现通用搜索
- c# – 如何避免Radwindow Open上的回发?
- c – std :: is_signed和std :: numeric_limits :: is_sign
- not all arguments converted during string formatting
- Ruby 打开 Windows 对话框并获取返回值
- flash与cocos2d-x完美结合