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

熱點內容
南匯污水處理廠在哪裡 瀏覽:808
知識蒸餾英文 瀏覽:55
辛集皮革污水招工網 瀏覽:9
醫學陽離子交換劑 瀏覽:736
新車內空氣凈化器怎麼選擇 瀏覽:750
家禽廢水 瀏覽:567
供排水管道除垢 瀏覽:532
凈水出口和純水出口怎麼區分 瀏覽:541
洛陽地埋式污水處理設備價格 瀏覽:503
回用景觀水 瀏覽:447
粉劑除垢劑 瀏覽:296
樹脂瓦機械多少錢 瀏覽:381
環氧樹脂地坪的防火等級要求 瀏覽:218
岳陽污水處理廠有哪些 瀏覽:34
什麼飲水機安全 瀏覽:356
超濾凈的水含有礦物質嗎 瀏覽:594
拾回雕文怎麼用 瀏覽:889
污水處理池施工碰到的問題 瀏覽:129
宋dm空氣凈化器怎麼樣 瀏覽:605
靜放水除水垢 瀏覽:359