Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java用正则表达式删除html标签几个例子

java用正则表达式删除html标签几个例子

发布时间:2016-10-22   编辑:www.jquerycn.cn
jquery中文网为您提供java用正则表达式删除html标签几个例子等资源,欢迎您收藏本站,我们将为您提供最新的java用正则表达式删除html标签几个例子资源
正则表达式可以执行替换操作从而删除我们指定的字符串了,在正则表达式中html就是普通的字符串了,下面一起来看小编整理的几个正则表达式删除html标签的例子

例子1

新闻内容或者博客文章,如果显示摘要,需要去除内容的html格式标签,找到一个正则表达式,实现了:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4624')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4624>

/**
     * 删除input字符串中的html格式
     * 
     * @param input
     * @param length
     * @return
     */ 
    public static String splitAndFilterString(String input) { 
        if (input == null || input.trim().equals("")) { 
            return ""; 
        } 
        // 去掉所有html元素, 
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll( 
                "<[^>]*>", "").replaceAll("[(/>)<]", ""); 
        return str; 
    }

过滤掉所有script脚本的正则:
content.replaceAll("<script[^>]*?>[\\s\\S]*?<\\/script>", "")
过滤掉所有style的正则:
content.replaceAll("<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>", "");
过滤掉所有html标签,保留p和br标签。
content.replaceAll("</?(?!br|/?p)[^>]*>", "");
过滤掉所有html标签,保留p标签。
content.replaceAll("</?(?!/?p)[^>]*>", "");

</td></tr></table>

例子2

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2488')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2488>

import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class HtmlUtil {
    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(""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }
      
    public static String getTextFromHtml(String htmlStr){
        htmlStr = delHTMLTag(htmlStr);
        htmlStr = htmlStr.replaceAll("&nbsp;", "");
        htmlStr = htmlStr.substring(0, htmlStr.indexOf("。") 1);
        return htmlStr;
    }
      
    public static void main(String[] args) {
        String str = "<div style='text-align:center;'> 整治“四风”   清弊除垢<br/><span style='font-size:14px;'> </span><span style='font-size:18px;'>公司召开党的群众路线教育实践活动动员大会</span><br/></div>";
        System.out.println(getTextFromHtml(str));
    }
}

</td></tr></table>

例子3

/

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8921')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8921>

**

 * 删除Html标签

 *

 * @param inputString

 * @return

 */

 public static String htmlRemoveTag(String inputString) {

 if (inputString == null)

 return null;

 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;

 try {

 //定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script>

 String regEx_script = "<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>";

 //定义style的正则表达式{或<STYLE[^>]*?>[\s\S]*?<\/style>

 String regEx_style = "<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>";

 String regEx_html = "<[^>] >"; // 定义HTML标签的正则表达式

 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标签

 textStr = htmlStr;

 } catch (Exception e) {

 e.printStackTrace();

 }

 return textStr;// 返回文本字符串

 }

</td></tr></table>

您可能感兴趣的文章:
java用正则表达式删除html标签几个例子
PHP删除HTMl标签的代码
php删除html标签及字符串中html标签的代码
php去除HTML标签的二种方法
PHP删除第一个p标签中内容
常用正则表达式的例子
正则表达式 – 简介
Java 实例
php正则 查找html中包含id属性的html标签
学习javascipt的正则表达式

[关闭]