public static String htmlText(String inputString) {
String htmlStr = inputString; //含html标签的字符串
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_nbsp;
java.util.regex.Matcher m_nbsp;
java.util.regex.Pattern p_r;
java.util.regex.Matcher m_r;
java.util.regex.Pattern p_n;
java.util.regex.Matcher m_n;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
String regEx_nbsp = "&nbsp;"; //定义&nbsp;标签的正则表达式
String regEx_r = "\r"; //定义&nbsp;标签的正则表达式
String regEx_n = "\n"; //定义&nbsp;标签的正则表达式

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,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //过滤html标签

p_nbsp = Pattern.compile(regEx_nbsp,Pattern.CASE_INSENSITIVE);
m_nbsp = p_nbsp.matcher(htmlStr);
htmlStr = m_nbsp.replaceAll(""); //过滤&nbsp;

// p_r = Pattern.compile(regEx_r, Pattern.CASE_INSENSITIVE);
// m_r = p_r.matcher(htmlStr);
// htmlStr = m_r.replaceAll("");//过滤\r
//
// p_n = Pattern.compile(regEx_n, Pattern.CASE_INSENSITIVE);
// m_n = p_n.matcher(htmlStr);
// htmlStr = m_n.replaceAll("");//过滤\n

textStr = htmlStr;

}catch(Exception e) {
}
return textStr;
}

去除html代码中的标签的更多相关文章

  1. C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值

    //C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值,表达式如下: Regex regImg = new Regex(@"(?is)<a[^>]*?href ...

  2. php - 去除php代码中的多余空格

    <?php class Test{ public function test(){ $tmplContent = file_get_contents('./test.php'); $tmplCo ...

  3. java去除html代码中含有的html、js、css标签,获取文字内容

    https://blog.csdn.net/u010882234/article/details/80585175

  4. PHP提取HTML代码中img标签下src属性

    需求:提取整片文章中img的src属性,并保存到一个数组当中 preg_match_all("/(href|src)=([\"|']?)([^\"'>]+.(jpg ...

  5. 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)

    移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...

  6. 去除C/C++程序代码中的注释

    最近搞软件著作权,去除代码空行和注释比较麻烦,想写个程序自动去除,去网上搜了下,发现有类似的程序,不过只有去除注释.鉴于word中可以去除空行(用^p^p替换^p),先用网上的代码,以后有时间写个完整 ...

  7. idea中去除重复代码提示的灰色波浪线

    可以看到上面代码中的灰色波浪线,特别影响观感,可以看到是因为有了重复代码.不确定它是怎么确定重复代码的. 解决办法: Setting--Editor--Inspections--General---D ...

  8. Salesforce 自定义标签在代码中的应用

    自定义标签简介 Salesforce 中自定义标签(Custom Label)的作用是存储一般性的文本,可以用于 Apex.Visualforce 页面.Lightning 组件等地方,用于显示提示信 ...

  9. Android在代码中获取meta标签内容

    最近写SDK需要获取<meta>标签的值,网上资料很多~分享是件好事~我很快就找到了相关资料. 下面贴上代码: ApplicationInfo appInfo = null; String ...

随机推荐

  1. 设计模式-生成者模式之c#代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 2018.10.08 NOIP模拟 斐波那契(贪心+hash/map)

    传送门 签到题. 显然是可以贪心分组的,也就是尽量跟当前的分成一组. 这时我们需要判断a[l]+a[r],a[l+1]+a[r]...a[r−1]+a[r]a[l]+a[r],a[l+1]+a[r]. ...

  3. hdu-2955(01背包+逆向思维+审题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:注意p和m[i]是被抓的概率,不能直接用,要转换为逃跑的概率,然后将得到的钱视为背包体积再 ...

  4. 35. Romantic Love and Ideal Romantic Relationship 爱情及理想爱情关系

    35. Romantic Love and Ideal Romantic Relationship 爱情及理想爱情关系 ① Romantic love has clear evolutionary r ...

  5. jedis 链接池使用(转)

    Jedis作为redis的最佳客户端,它提供了连接池的特性,“连接池”在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apache commons-po ...

  6. java最全的Connection is read-only. Queries leading to data modification are not allowed

    Connection is read-only. Queries leading to data modification are not allowed 描述:框架注入时候,配置了事物管理,权限设置 ...

  7. hdu 4994 前后有序Nim游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=4994 Nim游戏变成从前往后有序的,谁是winner? 如果当前堆数目为1,玩家没有选择,只能取走.遇到到不为1 ...

  8. 动态的把固定格式的json数据以菜单形式插入

    var root=$("#side-menu"); $(menuData).each(function(i,n){ var top1Li=$("<li>< ...

  9. 团队项目第六周——Alpha阶段项目复审(名字很难想队)

    Alpha阶段项目复审 小组 优点 缺点 排名 小谷围驻广东某工业719电竞大队 一个贴近大学生生活的二手交易平台.界面美观功能完善. 部分功能未完善,没有第三方登录 1 中午吃啥队 系统完善,界面简 ...

  10. maven下@override标签失效

    经常遇见此问题,现记录如下,以备下次查阅. 在pom文件添加配置: <plugin> <groupId>org.apache.maven.plugins</groupId ...