導航:首頁 > 編程語言 > java正則匹配html

java正則匹配html

發布時間:2023-01-02 20:58:16

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

//我隨便寫了一個工具類,getRegexData就是那個方法,你可以根據你的需求稍加改動即可因為我使用的
//URL而不是HttpClient,所以數據是全部獲取過來了,你自己改改吧!不懂再問我
packagecom.wdy.util;

importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
/**
*工具類
*@authorWDY
*
*/
publicclassTool{
publicstaticvoidmain(String[]args){
System.out.println(getRegexData("<img[]*src.*?jpg"","<imgsrc="img1.jpg"><imgsrc="img2.jpg""));


try{
URLurl=newURL("http://www..com");
StringstringData=getStringFromInputStream(url.openStream());
System.out.println(stringData+"----------------------------------------");
System.out.println();

System.out.println(getRegexData("http://.{6,70}?(png|jpg)",stringData));
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*給一個正則表達式,和數據,將正則匹配到的數據全數取出來
*
*@paramregex
*@paramdata
*@returnList<String>
*/
publicstaticList<String>getRegexData(Stringregex,Stringdata){
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(data);
List<String>resultList=newArrayList<String>();
intindex=0;//搜索的位置
Stringtemp="";
/*從指定位置查找,如果找到了,就繼續執行下面的代碼*/
while(matcher.find(index)){
temp=matcher.group();//將匹配到的數據取出來放到集合中去
resultList.add(temp);
index+=temp.length();//將查找位置放到此時找到的數據後面
System.out.println(index);
}
returnresultList;
}
/**
*將輸入流裝成字元串
*@paramis
*@return
*/
(InputStreamis)throwsIOException{

StringBuildersbl=newStringBuilder();
byte[]buff=newbyte[1024*8];
intlen;
inti=0;
while((len=is.read(buff))!=-1){
sbl.append(newString(buff,0,len,"utf-8"));
System.out.println(i++);
}
System.out.println(sbl.length());
returnsbl.toString();
}
}

B. java正則表達式替換html中除標簽外的關鍵字內容

\b{keywords}\b(?=[^<>]*<)
把上面{keywords}替換成你的關鍵字,注意把{}也替換掉,我是為了讓你看明白換哪才加的{}。
然後你找到匹配上面的正則的地方後替換成你想替換的字元串就好了,全部替換記得用全局修飾符

C. java 正則表達式是什麼

不同情況下的正則表達式:

閱讀全文

與java正則匹配html相關的資料

熱點內容
java函數return 瀏覽:25
鄭州app積分商城哪裡好 瀏覽:610
7za命令解壓zip 瀏覽:906
硬碟加密無法進入系統 瀏覽:560
四葉花演算法 瀏覽:741
寶寶濾鏡app哪裡下載 瀏覽:1002
java保護代碼 瀏覽:806
游戲海外伺服器什麼意思 瀏覽:568
快手網紅程序員排名 瀏覽:99
首先會通過什麼尋找伺服器的ip地址 瀏覽:199
網頁代碼加密解碼 瀏覽:285
wyks1ms文件夾 瀏覽:93
什麼app可以看柯南高清 瀏覽:425
加密的盤文件恢復 瀏覽:22
絕對路徑能查找隱藏文件夾嗎 瀏覽:568
做抖音相冊用什麼圖片app 瀏覽:726
pc怎麼打開sftp伺服器 瀏覽:998
中長跑用什麼app 瀏覽:943
hlddz是什麼文件夾怎麼換盤 瀏覽:458
oppo主題編譯器下載 瀏覽:807