Java使用正則表達(dá)式刪除所有HTML標(biāo)簽的方法示例
本文實(shí)例講述了Java使用正則表達(dá)式刪除所有HTML標(biāo)簽的方法。分享給大家供大家參考,具體如下:
package com.xz.cxzy.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HtmlUtil {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定義script的正則表達(dá)式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定義style的正則表達(dá)式
private static final String regEx_html = "<[^>]+>"; // 定義HTML標(biāo)簽的正則表達(dá)式
private static final String regEx_space = "\\s*|\t|\r|\n";//定義空格回車換行符
/**
* @param htmlStr
* @return
* 刪除Html標(biāo)簽
*/
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標(biāo)簽
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 過濾style標(biāo)簽
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 過濾html標(biāo)簽
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 過濾空格回車標(biāo)簽
return htmlStr.trim(); // 返回文本字符串
}
public static String getTextFromHtml(String htmlStr){
htmlStr = delHTMLTag(htmlStr);
htmlStr = htmlStr.replaceAll(" ", "");
htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
return htmlStr;
}
public static void main(String[] args) {
String str = "<div style='text-align:center;'>
<span style='font-size:14px;'> </span><span style='font-size:18px;'></span>
</div>";
System.out.println(getTextFromHtml(str));
}
}
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot實(shí)現(xiàn)MapperScan添加動態(tài)配置(占位符)
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)MapperScan添加動態(tài)配置(占位符),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
SpringBoot JVM參數(shù)調(diào)優(yōu)方式
這篇文章主要介紹了SpringBoot JVM參數(shù)調(diào)優(yōu)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3)
這篇文章主要介紹了springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot設(shè)置動態(tài)定時(shí)任務(wù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot設(shè)置動態(tài)定時(shí)任務(wù)的方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的參考價(jià)值,需要的可以參考一下2022-06-06
Future與FutureTask接口實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Future與FutureTask接口實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄赟pringBoot上傳任意文件功能的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Mybatis-Plus中update()和updateById()將字段更新為null
本文主要介紹了Mybatis-Plus中update()和updateById()將字段更新為null,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

