导航:首页 > 编程语言 > java正则表达式去掉html标签

java正则表达式去掉html标签

发布时间:2024-07-27 00:58:34

『壹』 java正则表达式过滤html p标签

用JavaScript方法如下,JAVA语言类似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')

『贰』 用java去除掉这段代码的HTML标签

public static String HtmlText(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标签

/* 空格 —— */
// p_html = Pattern.compile("\\ ", Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = htmlStr.replaceAll(""," ");

textStr = htmlStr;

}catch(Exception e) {
}
return textStr;
}

传你的字符串进去看看,可以的话加分,谢谢

『叁』 java如何去掉字符串中的 html标签

1.去除单个HTML标记
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML标记
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLSpirit{ ITjob 远标教育
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标签问题

|第一个问题: (第二行代码可写可不写,具体要看你去除html后的正文内容)
txtcontent = htmlcontent.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
txtcontent = txtcontent.replaceAll("\\s*|\t|\r|\n", "");//去除字符串中的空格,回车,换行符,制表符

『伍』 【Java作业向】正则表达式过滤HTML标签

过滤HTML标签的Java正则表达式 (?s)<.*?/?.*?>

按照你的要求编写的用正则表达式过滤HTML标签的Java程序如下

public class AA {

public String tagFilter(String s){

String regex = "(?s)<.*?/?.*?>";

String ss=s.replaceAll(regex,"");

return ss;

}

public static void main(String[] args) {

String s="<div class="guid time online">测试 abc</div><span data-url="games/details/" class="guid done">你好13548</span><a href="games/details/" class="guid">15个字母Abc</a><i class="icon-guid"/>";

String result=new AA().tagFilter(s);

System.out.println(result);

}

}

『陆』 java去掉字段中的html标签

用正则表达式吧,应该比较简单。
或者使用笨点的方法,循环查找版'>'符号的位置,判断下一权个字符是不是'<',如果是,则继续循环,如果不是则是需要留下的文本了,把文本用list保存起来继续循环直到全部字段结束。
最后list里面就是你要留下的文本了

『柒』 鎬庢牱浣跨敤姝e垯琛ㄨ揪寮忓垹闄ゆ墍鎸囧畾鐨凥TML鏍囩

涓哄ぇ瀹舵紨绀轰竴涓杈冧负绠鍗曠殑鍑芥暟鍚э紝杩欎竴涓鍑芥暟鎵瑕佸仛鐨勪簨鎯呭氨鏄瑕佸皢淇濈暀鐨凾AG閫氶氫覆璧锋潵,鐒跺悗鐢熸垚涓涓姝e垯琛ㄨ揪寮,鎺ョ潃灏辫佸皢涓浜涘苟涓嶉渶瑕佺殑TAG閫氶氬垹闄ゃ傚叿浣撶殑鍑芥暟锛屽傚浘鎵绀猴細

阅读全文

与java正则表达式去掉html标签相关的资料

热点内容
数据线转换器多少钱 浏览:274
美国队长qq皮肤多少钱 浏览:630
win8word文档在哪 浏览:180
甘肃省人事局文件在哪里 浏览:689
spss安装输入代码 浏览:546
网络语言知乎 浏览:596
iphoneicloud无法显示 浏览:112
ict程序包 浏览:729
java有哪些条件语句 浏览:345
冒险岛120级去哪里升级 浏览:511
手机输入法声音文件 浏览:876
下划线哪个app 浏览:48
win10h1z1切换桌面 浏览:911
js定义集合数组 浏览:153
win10企业关闭自动更新 浏览:920
js扩展对象 浏览:370
受控文件的章印内容怎么写 浏览:463
微信云文件丢失 浏览:299
手机bbc文件存在哪个路径 浏览:651
系统安装镜像文件名 浏览:620

友情链接