导航:首页 > 净水问答 > java过滤html保留img

java过滤html保留img

发布时间:2025-01-02 15:00:27

⑴ 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里面<img>标签里面绝对路径的部分字符串正规则表达式

html.replaceall("<IMG SRC=\\".*?\\"/>","your string")

⑶ 怎么清除所有的html标签,(包括图片、</p>、<span>等等所有标签)只保留文字部分

这是我以前整理的,你可以先试试
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{
publicstatic 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 p标签

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

⑸ java 如何利用正则表达式只保留html里面的<p></p>标签里面的内容

正则表达式:<p.*?>(.*?)</p>
group(1)为正文内容。
输出时加上\n就行了

import java.util.regex.*;
public class Test{
public static void main(String[] args){
String str="<p style=\"font-size:1.3em;font-weight:bold\">No page with that title exists.</p> ";
String regex="<p.*?>(.*?)</p> ";
Pattern p =Pattern.compile(regex);
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
}
}

阅读全文

与java过滤html保留img相关的资料

热点内容
100目过滤豆浆有渣没 浏览:899
水处理用电量 浏览:52
下水道漏了污水怎么处理 浏览:783
反渗透浓水怎么实现内循环 浏览:20
污水处理建设乡镇规划相关文稿 浏览:861
污水处理站隐患排查 浏览:359
磁化水水垢刷不掉 浏览:243
反渗透膜受到污染什么原因 浏览:514
微量蒸馏法测蛋白质 浏览:634
不通电净水器有废水口怎么安装 浏览:146
生活污水回收处理 浏览:460
陕西树脂古建瓦厂 浏览:293
养海水鱼纯水的TDS是多少 浏览:710
城镇污水处理厂污泥处置核查 浏览:135
空调废水多 浏览:177
饮水机直流水泵抖动怎么回事 浏览:527
一体机净水机水龙头怎么拆 浏览:534
树脂台面磨 浏览:187
污水处理配水井与调节池怎么联通 浏览:896
臭气灌入污水井 浏览:822