java 去掉html标签

 1 import java.util.regex.Matcher; 
 2 import java.util.regex.Pattern; 
 3 
 4 public class HTMLSpirit{ 
 5     public static String delHTMLTag(String htmlStr){ 
 6         String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
 7         String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
 8         String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
 9          
10         Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
11         Matcher m_script=p_script.matcher(htmlStr); 
12         htmlStr=m_script.replaceAll(""); //过滤script标签 
13          
14         Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
15         Matcher m_style=p_style.matcher(htmlStr); 
16         htmlStr=m_style.replaceAll(""); //过滤style标签 
17          
18         Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
19         Matcher m_html=p_html.matcher(htmlStr); 
20         htmlStr=m_html.replaceAll(""); //过滤html标签 
21 
22         return htmlStr.trim(); //返回文本字符串 
23     } 
24 } 

 

posted @ 2015-06-24 17:35  老瞿  阅读(2569)  评论(0编辑  收藏  举报