【java工具类】去除HTML的标签的java代码如下:

2024-06-09 17:48

本文主要是介绍【java工具类】去除HTML的标签的java代码如下:,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

java工具类:

 

 

去除HTML的标签的java代码如下:

public class HtmlFilterTagUtils {private static final int subLength = 200; //截取字符串长度private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";// 定义script的正则表达式private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";// 定义style的正则表达式private static final String regEx_html = "<[^>]+>";// 定义HTML标签的正则表达式private static final String d_class = "<d class=[^>]*?>[\\s\\S]*?";// 定义SpanHTMLClass标签的正则表达式private static final String a_class = "<a[^>]*?>[\\s\\S]*?";// 定义AHTMLClass标签的正则表达式//    private static final String span_class = "<span class\\s*[^>]*>(.*?)<\\/span>";// 定义HTMLClass标签的正则表达式private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符private static final String regEx_enter = "(^|(\\r\\n|\\n))\\s*(\\r\\n|\\n)";//   private static final String regEx_enter = "(\n|\r\n)\\s+";//空白行private static final String regEx_weixinJs = "<span data-shimo-docs=\"(.*?)\">";public static String filter(String htmlStr) {Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);Matcher m_space = p_space.matcher(htmlStr);htmlStr = m_space.replaceAll(""); // 过滤空格回车标签Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);Matcher m_script = p_script.matcher(htmlStr);htmlStr = m_script.replaceAll(""); // 过滤script标签Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);Matcher m_style = p_style.matcher(htmlStr);htmlStr = m_style.replaceAll(""); // 过滤style标签String reg = ">\\s+([^\\s<]*)\\s+<";htmlStr = htmlStr.replaceAll(reg, ">$1<");htmlStr = htmlStr.replace("<p>&rdquo;&rdquo;</p>", "");htmlStr = htmlStr.replace("<br>", "<br>\n");htmlStr = htmlStr.replace("</table>", "</table>\n").replace("</h1>", "</h1>\n").replace("</li>", "</li>\r\n").replace("</p>", "</p>\n").replace("</div>", "\n").replace("</br>", "</br>\n").replace("</i>", "</i>\n").replace("</tr>", "</tr>\r\n");Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);Matcher m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll(""); // 过滤html标签htmlStr = htmlStr.replace("&rdquo;", " ").replace("&ldquo;", " ").replace("&nbsp;", " ").replace("&ensp", " ").replace("&emsp;", " "); // 过滤空格htmlStr = htmlStr.replace(" ", "").replace(" ", "").replace("\\t", "").replace("\n\n", "\n").replace("\n\n.replace(\"&ensp;\",\" \")", "\n").replace("\n\n", "\n");Pattern p_enter = Pattern.compile(regEx_enter, Pattern.CASE_INSENSITIVE);Matcher m_enter = p_enter.matcher(htmlStr);htmlStr = m_enter.replaceAll(""); // 过滤空格回车标签htmlStr = htmlStr.replace("\n", "\n\n").replace("\r\n\n", "\n");return htmlStr; // 返回文本字符串3}public static String filterSpan(String htmlStr) {Pattern p_html = Pattern.compile(d_class, Pattern.CASE_INSENSITIVE);Matcher m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll(""); // 过滤html标签return htmlStr; // 返回文本字符串}public static String filterA(String htmlStr) {htmlStr = htmlStr.replaceAll(regEx_weixinJs, "");return htmlStr;}/****  返回文本字符串 <div></d>进行特殊处理* @param htmlStr* @return*/public static String filterSpanContent(String htmlStr) {Pattern p_html = Pattern.compile(d_class, Pattern.CASE_INSENSITIVE);Matcher m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll("");htmlStr = htmlStr.replace("</d>", "");// &ensp;	 	半个空白位  &emsp;	 	一个空白位  &nbsp;	 	不断行的空白htmlStr = htmlStr.replace("&rdquo;", " ").replace("&ldquo;", "\"").replace("&nbsp;", " ").replace("&ensp", " ").replace("&emsp;", " "); // 过滤空格return htmlStr;}public static String filterHtml(String htmlStr) {Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);Matcher m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll("");return htmlStr;}/**** 替换掉微信特有的js代码* @param htmlStr* @return*/public static String filterWeixinJs(String htmlStr) {htmlStr = htmlStr.replaceAll(regEx_weixinJs, "");return htmlStr;}/***** 获取图片的链接处理  <src* @param html* @return*/public static List<String> getImgSRC(String html) {String regex = "src=\"(.*?)\"";List<String> list = new ArrayList<>();Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(html);while (ma.find()) {list.add(ma.group());}return list;}/***** 获取# 特殊字符识别的字符串  <src* @param expression* @return*/public static String getStrFromMark(String expression) {String regex = "#(.*?)#";List<String> list = new ArrayList<>();Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(expression);String result = "";while (ma.find()) {result = ma.group();break;}result = result.replace("#", "");return result;}/***** 获取# 特殊字符识别的字符串  <src* @param expression* @return*/public static List<String> getStrFromMarkList(String expression) {String regex = "#(.*?)#";List<String> list = new ArrayList<>();Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(expression);String result = "";while (ma.find()) {result = ma.group();list.add(result);}return list;}public static List<String> getImg(String html) {String regex = "<(img|IMG).*?(\\>|\\/>|\\<\\/img>)";List<String> list = new ArrayList<>();Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(html);while (ma.find()) {list.add(ma.group());}return list;}public static List<String> getImgANDSRC(String html) {String regex = "<img[\\s+]src=.*?(\\/>|\\<\\/img>)";List<String> list = new ArrayList<>();Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(html);while (ma.find()) {list.add(ma.group());}return list;}public static Boolean isPattern(String content, String regex) {if(StringUtils.isBlank(content)){return false;}Pattern pa = Pattern.compile(regex, Pattern.DOTALL);Matcher ma = pa.matcher(content);if (ma.find()) {return true;}return false;}/***** 排除掉单引号的影响* @param title* @return*/public static String getTitleNoMark(String title) {title = title.replace("'", "\\'");title = title.replace(",", "\\\\,");return title;}
}

 

这篇关于【java工具类】去除HTML的标签的java代码如下:的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1045868

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3