java过滤html标签获取纯文本信息
生活随笔
收集整理的這篇文章主要介紹了
java过滤html标签获取纯文本信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自?java過濾html標簽獲取純文本信息
package com.lyt.base.util;import java.util.regex.Pattern;public class FilterHtmlUtil {public static String Html2Text(String inputString){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{String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script> }String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style> }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;//返回文本字符串 } }總結
以上是生活随笔為你收集整理的java过滤html标签获取纯文本信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HtmlParser提取网页中的纯文本信
- 下一篇: 配置电脑的软件有哪些(配置电脑的软件)