JAVA 使用正则表达式删除HTML标签
2015-07-30 20:38
483 查看
import java.util.regex.Matcher; import java.util.regex.Pattern; public class HTMLSpirit{ public static String delHTMLTag(String htmlStr){ String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 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标签 return htmlStr.trim(); //返回文本字符串 } }
相关文章推荐
- spring 代理(静态代理&动态代理&cglib代理)
- spring task 注解+xml的demo
- 从零开始搭建SpringMVC框架以及最简单的 Hello World 实例
- Spring IoC Container and Spring Bean Example Tutorial
- Spring中通配符
- java基础第四讲——数组、字符串、类与对象(构造器)
- day01_Java开发环境_20150730
- Java错误提示-------java.sql.SQLException:ResultSet is closed
- 字符串反转(java实现)
- 插入排序、冒泡排序、选择排序、希尔排序、快速排序、归并排序、堆排序和LST基数排序——JAVA实现
- SpringBoot 资料收集
- Java基础-----集合
- JAVA中sleep,wait,yield,join函数解析_ftc
- Java读取文件夹大小的6种方法及代码
- Java中的hashcode方法
- Java 线程池的原理与实现
- java正则表达式注意事项
- 《Java编程思想》第五章练习5答案
- Java 8之Lambda
- Java finalize方法使用