正規表現を使用してHTMLタグを削除します.
1929 ワード
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; // script
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; // style
String regEx_html="<[^>]+>"; // HTML
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
return htmlStr.trim(); //
}
}
Java HTML
Java HTML :
/**
* html 。<br>
* , , 。
*
* @param content
*
* @return
*/
public static String stripHtml(String content) {
// <p>
content = content.replaceAll("<p .*?>", "\r
");
// <br><br/>
content = content.replaceAll("<br\\s*/?>", "\r
");
// <>
content = content.replaceAll("\\<.*?>", "");
// HTML
// content = HTMLDecoder.decode(content);
return content;
}