1. 응용 프로그램 시나리오 : HTML 파일 또는 문자열 (HTML 컨텐츠)에서 일반 텍스트를 추출하고 웹 페이지 태그를 제거합니다.
2. 코드 1 : replaceall이 완료되었습니다
// html에서 일반 텍스트 추출 public static string stripht (String strhtml) {String txtContent = strhtml.replaceall ( "</? [^>]+>", ""); // <html> ttxtContent = txtContent.replaceall ( "<a> // s*|/t |/r |/n |/n |/n </a>", ""); // 문자열에서 공백을 제거, 캐리지 리턴, 라인 브레이크, 탭 반환 txtContent; }3. 코드 2 : 정규식이 완료되었습니다
// html에서 일반 텍스트 추출 public static string html2text (String inputString) {String htmlstr = inputString; // html 태그 문자열 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.regex.ptertn.plattern.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]*? 스크립트 [^>]*?> [// s // s]*? <[// s]*? // [// s]*? // [// s]*? 스크립트 [// s]*?>"; // 정의 정의 {또는 <script [^>]*?> [// s // s]*? <// script> string regex_style = "<[// s]*? style [^>]*?> [// s // s]*? <[// s]*? // [// s]*?>"; // 정의 정의 {또는 <Style [^>]*?> [// s // s]*? <// style> String regex_html = "<[^>]+>"; // 정의 표현식 p_script = pattern.compile (regex_script, pattern.case_insensitive); m_script = p_script.matcher (htmlstr); htmlstr = m_script.replaceall ( ""); // 필터 스크립트 태그 p_style = pattern.compile (regex_style, pattern.case_insensitive); m_style = p_style.matcher (htmlstr); htmlstr = m_style.replaceall ( ""); // 필터 스크립트 태그 p_style = pattern.compile (regex_style, pattern.case_insensitive); m_style = p_style.matcher (htmlstr); htmlstr = m_style.replaceall ( ""); // 필터 스타일 태그 p_html = pattern.compile (regex_html, pattern.case_insensitive); m_html = p_html.matcher (htmlstr); htmlstr = m_html.replaceall ( ""); // 필터 html 태그 textstr = htmlstr; } catch (예외 e) {System.err.println ( "html2text :" + e.getMessage ()); } // 우주선 제외 textstr = textstr.replaceall ( "[]+", ""); textstr = textstr.replaceall ( "(? m)^// s*$ (// n | // r // n)", "); return textstr; // return text string}4. 코드 3 : htmleditorkit.parsercallback, Java의 자체 클래스가 완료되었습니다
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)는 ioexception {s = new StringBuffer (); Parserdelegator Delegator = New ParserDelegator (); // 세 번째 매개 변수는 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 {// html to the cronged in = new StringReader ( "String"); filereader in = new Filereader ( "java-new.html"); html2text parser = new html2text (); parser.parse (in); 넣다(); System.out.println (parser.getText ()); } catch (예외 e) {e.printstacktrace (); }}}HTML 텍스트에서 일반 텍스트를 추출하는 위의 Java 방법은 내가 공유하는 모든 내용입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.