1. Application scenario: Extract plain text from an html file or from a String (which is html content) and remove web page tags;
2. Code 1: replaceAll is done
//Extract plain text from html public static String StripHT(String strHtml) { String txtcontent = strHtml.replaceAll("</?[^>]+>", ""); //Extract the <html> tag txtcontent = txtcontent.replaceAll("<a>//s*|/t|/r|/n</a>", "");//Remove spaces in the string, carriage return, line breaks, tab return txtcontent; }3. Code 2: Regular expressions are completed
//Extract plain text from html public static String Html2Text(String inputString) {String htmlStr = inputString; // String String with html tag 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_style;java.util.regex.Pattern p_html;java.util.regex.Matcher m_html;try {String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?//[//s]*?//[//s]*?script[//s]*?>"; // Define the regular expression { or <script[^>]*?>[//s//S]*?<///script> String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?//[//s]*?>"; // Define the regular expression { or <style[^>]*?>[//s//S]*?<///style> String regEx_html = "<[^>]+>"; // Define the regular expression p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // Filter script tag p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // Filter script tag p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // Filter style tag p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // Filter html tag textStr = htmlStr; } catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); }// Exclude space lines textStr=textStr.replaceAll("[ ]+", " ");textStr=textStr.replaceAll("(?m)^//s*$(//n|//r//n)", "");return textStr;// Return text string}4. Code 3: HTMLEditorKit.ParserCallback is done, Java's own class
package com.util;import java.io.*;import javax.swing.text.html.*;import javax.swing.text.html.parser.*;public class Html2Text extends HTMLEditorKit.ParserCallback { StringBuffer s; public Html2Text() {} public void parse(Reader in) throws IOException { s = new StringBuffer(); ParserDelegator delegator = new ParserDelegator(); // the third parameter is TRUE to ignore charset direct delegator.parse(in, this, Boolean.TRUE); } public void handleText(char[] text, int pos) { s.append(text); } public String getText() { return s.toString(); } public static void main (String[] args) { try { // the HTML to convert //Reader in=new StringReader("string"); FileReader in = new FileReader("java-new.html"); Html2Text parser = new Html2Text(); parser.parse(in); in.close(); System.out.println(parser.getText()); } catch (Exception e) { e.printStackTrace(); } }}The above method of Java to extract plain text from Html text is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.