java去掉html标签
来源:互联网 发布:青少儿编程 编辑:程序博客网 时间:2024/04/30 14:54
public static String getText(String htmlStr) {
if(htmlStr==null || "".equals(htmlStr)) return "";
String textStr ="";
java.util.regex.Pattern pattern;
java.util.regex.Matcher matcher;
try {
String regEx_remark = "<!--.+?-->";
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标签的正则表达式
String regEx_html1 = "<[^>]+";
htmlStr = htmlStr.replaceAll("\n","");
htmlStr = htmlStr.replaceAll("\t","");
pattern=Pattern.compile(regEx_remark);//过滤注释标签
matcher=pattern.matcher(htmlStr);
htmlStr=matcher.replaceAll("") ;
pattern = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤script标签
pattern = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤style标签
pattern = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
pattern = Pattern.compile(regEx_html1,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
textStr = htmlStr.trim();
}catch(Exception e) {
System.out.println("获取HTML中的text出错:");
e.printStackTrace();
}
return textStr;//返回文本字符串
}
if(htmlStr==null || "".equals(htmlStr)) return "";
String textStr ="";
java.util.regex.Pattern pattern;
java.util.regex.Matcher matcher;
try {
String regEx_remark = "<!--.+?-->";
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标签的正则表达式
String regEx_html1 = "<[^>]+";
htmlStr = htmlStr.replaceAll("\n","");
htmlStr = htmlStr.replaceAll("\t","");
pattern=Pattern.compile(regEx_remark);//过滤注释标签
matcher=pattern.matcher(htmlStr);
htmlStr=matcher.replaceAll("") ;
pattern = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤script标签
pattern = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤style标签
pattern = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
pattern = Pattern.compile(regEx_html1,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
textStr = htmlStr.trim();
}catch(Exception e) {
System.out.println("获取HTML中的text出错:");
e.printStackTrace();
}
return textStr;//返回文本字符串
}
- java去掉html标签
- java去掉html标签
- java 去掉html标签
- java 去掉html标签
- java去掉html标签 必须首先去掉 "
- java中去掉html标签
- java 去掉html标签 使用正则表达式删除HTML标签。
- JAVA去掉字符串里的html标签。
- java正则表达式去掉所有HTML标签
- java正则表达式去掉所有HTML标签
- java正则表达式去掉所有HTML标签
- java正则表达式去掉所有HTML标签
- java正则去掉所有html标签
- java正则表达式去掉html标签
- 去掉HTML的标签
- js去掉HTML标签
- 去掉HTML标签
- 去掉HTML标签
- Jetty Buffer
- 数据库索引
- hdu 3518后缀数组
- 云存储系统的特征-软件与存储设备结合
- Android 实用工具Hierarchy Viewer实战
- java去掉html标签
- jquerymobile-22 方法
- ctags和cscope阅读代码
- PL/SQL实战 避免误用
- Vim的文件加密与解密
- 佛跳墙
- 模板参数
- Algorithm Practice for 1577
- 解释bash脚本中set -e与set -o pipefail的作用