導航:首頁 > 編程語言 > java正則過濾html

java正則過濾html

發布時間:2024-02-01 20:35:16

A. 【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);

}

}

B. java正則表達式過濾html p標簽

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

C. 如何寫一個java正則表達式,用來判斷給定字元串是否匹配到<html標簽

如果只是匹配<html的話,直接s.contains("<html");就可以。

D. 如何使用java的正則表達式提取html標簽

你的意思是不是用Java訪問一個鏈接,在返回的數據中提取出放在標簽中的數據,例如取出<img src=""/>這些標簽中的數據

E. java中怎麼用正則截取html中的全部<td ... >....</td>

<td( .*?)?>.*?</td>
//看看這個吧,可能沒那麼完善,但應該足以應付大多數情況。

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TdMatcher {
public static void main(String[] args) {
String html = "";
html += "<table>";
html += "<tr>";
html += "<td>message</td>";
html += "<td colspan='2'>message2</td>";
html += "<td >message3</td>";
html += "</tr>";
html += "</table>";
String[] matcher = matcher(html);
for (int i = 0; i < matcher.length; i++) {
System.out.println(matcher[i]);
}
}

private static String[] matcher(String html) {
Pattern pattern = Pattern.compile("<td( .*?)?>.*?</td>");
Matcher matcher = pattern.matcher(html);
List<String> list = new ArrayList<String>();
while (matcher.find()) {
list.add(matcher.group());
}
return list.toArray(new String[0]);
}
}

F. JAVA正則表達式解析HTML字元串

public class TestString4 {
public static void main(String[] args) {
String s = "<R_Data> 0005,實驗室0,0,0|0101,實驗室A-測試點1,200,200|0102,實驗室C-測試點2,80,400|0109,實驗室C-測試點1,80,300|1020,實驗室C-測試點3,80,500|1141,實驗室A-測試點2,400,400|1146,實驗室A-測試點3,300,300|1239,實驗室B-測試點1,50,150|1240,實驗室B-測試點2,80,200|1264,實驗室B-測試點3,220,110| </R_Data>";
s = s.replace("<R_Data>", "").replace("</R_Data>", "").trim();
String ss[] = s.split("\|");
String[][] sss = new String[ss.length][];
for(int i=0;i<ss.length;i++){
sss[i] = ss[i].split(",");
}
}
}

sss中存放的就是你需要的數據

G. 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(); //返迴文本字元串
}
}

H. 求一個去除html源代碼中的 無效代碼( 如注釋,空白字元,空白行等)的 java正則表達式~謝謝

注釋的正則:<!--[\s\S]*?-->
頁面樣式的正則:<style[^>]*>[^<]*?</style>
HTML標簽的正則:<[^>]*?>

/// <summary>
/// 正則替換
/// </summary>
/// <param name="sOld">原內容</param>
/// <param name="sRegexString">正則表達式</param>
/// <param name="sReplaceString">新字元串</param>
/// <returns></returns>
public static string ReplaceRegxString(string sOld, string sRegexString, string sReplaceString)
{
Regex reg = new Regex(@sRegexString, RegexOptions.Singleline | RegexOptions.IgnoreCase);
return reg.Replace(sOld, sReplaceString);
}

閱讀全文

與java正則過濾html相關的資料

熱點內容
qq名片八張拼圖組圖 瀏覽:254
win下顯示文件內容 瀏覽:417
wifisd的密碼 瀏覽:694
把excel表格插入word 瀏覽:117
經發e家app忘記密碼怎麼辦 瀏覽:921
goagent教程 瀏覽:344
桌面文件全選按鍵 瀏覽:105
PM2019支持哪些格式文件 瀏覽:272
全拼app現在叫什麼名字 瀏覽:476
win10現在成熟了嗎 瀏覽:727
win10mtp段落無效 瀏覽:862
cbf收銀機用什麼app 瀏覽:336
javascript變數頁面 瀏覽:766
網路相冊淘寶 瀏覽:60
編程拉絲機怎麼樣 瀏覽:897
msdn不是最新版本 瀏覽:749
參加編程比賽學什麼編程好 瀏覽:349
把c盤的桌面文件移走了怎麼辦 瀏覽:949
2025第三代軍用北斗升級版 瀏覽:925
魅藍手勢密碼怎麼弄 瀏覽:509

友情鏈接