導航:首頁 > 編程語言 > 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標簽相關的資料

熱點內容
painter2015視頻教程 瀏覽:204
jsperror 瀏覽:183
網路到底怎麼賺錢 瀏覽:402
蘋果耳機插口接觸不良 瀏覽:934
運動手環app哪個好 瀏覽:854
java設置double精度 瀏覽:587
java代碼分享網站 瀏覽:321
ps怎麼復制到文件裡面 瀏覽:360
win7管理員指紋登錄密碼忘了怎麼辦 瀏覽:38
c是一次性插入多少條數據 瀏覽:928
u盤文件編輯軟體 瀏覽:767
vb如何打開pdf文件 瀏覽:351
soundlinkiii升級 瀏覽:64
如何把文件改成cad 瀏覽:676
如何把多個監控合在一個網路內 瀏覽:637
qq的頭像在哪個文件夾 瀏覽:468
linuxexfat補丁 瀏覽:582
excelvb編程怎麼輸出數 瀏覽:737
567位qq 瀏覽:172
qq網名女生傷感 瀏覽:292

友情鏈接