coding++:java—提取Html文本字符串中的内容
2024-08-24 23:18:04
package com.tree.ztree_demo; import java.util.regex.Matcher;
import java.util.regex.Pattern; public class StringTools { 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 regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 /**
* @param htmlStr
* @return 删除Html标签
*/
public static String delHTMLTag(String htmlStr) {
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标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签 Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
htmlStr = htmlStr.replaceAll(" ", "");
htmlStr = htmlStr.replaceAll("↵", "");
return htmlStr.trim(); // 返回文本字符串
} }
最新文章
- 得静下心来看python了。
- struts2学习笔记--使用Validator校验数据
- UINavigationController的创建和相关设置---学习笔记四
- LINUX的磁盘管理du命令详解
- sql2012还原sql2008备份文件语句
- POJ 1696	 Space Ant --枚举,模拟,贪心,几何
- HDU 1312 Red and Black --- 入门搜索 BFS解法
- rsync 同步文件
- jQuery getJSON() 能给外部变量赋值
- nginx负载均衡和反向代理有什么区别
- Oracle笔记-表的管理
- vmplayer中的fedora20无法进入图形界面
- CentOS 7安装Docker
- 有趣而又被忽略的Unity技巧
- jQuery菜单示例(全选,反选,取消)
- APP自动化框架LazyAndroid使用手册(3)--核心API介绍
- 利用反射获取数据列+emit生成属性+单例模式
- Tensorflow Chapter-6
- validating &; update ctabfolder css
- POJ 3177 Redundant Paths 双联通分量 割边