導航:首頁 > 凈水問答 > 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相關的資料

熱點內容
蒸餾酒許可證怎麼辦6 瀏覽:484
美的飲水機浮球組件怎麼放 瀏覽:664
濟南水處理廠有限公司 瀏覽:868
洗衣櫃的清污水如何排管 瀏覽:735
城市污水回用 瀏覽:240
生物膜對廢水的凈化過程及原理 瀏覽:411
閻良水處理環保公司 瀏覽:918
桑普空氣凈化器怎麼辦 瀏覽:97
北京賽諾超濾膜原件 瀏覽:841
美國陶氏RO膜是哪裡產的 瀏覽:285
污水管道清理費入什麼賬 瀏覽:53
反相色譜為什麼不能用純水 瀏覽:445
廣州光明村污水管工程 瀏覽:647
全自動咖啡機可以用檸檬酸除垢嗎 瀏覽:68
養綠蘿用什麼凈化器好 瀏覽:50
英尼克純水機怎麼安裝 瀏覽:379
污水處理中存在的問題調查報告 瀏覽:58
濾芯裡面有螞蟻怎麼辦 瀏覽:939
純水電導多少合適 瀏覽:276
汽油濾芯40bar什麼意思 瀏覽:507