Ⅰ 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));
}
}
}
Ⅱ 怎樣用正則表達式過濾掉頁面中除了<p></p>和<img>以外所有的標簽
這個還真不容易實現,單獨保留p或者img都可以,但是兩個條件放一起就不行了。於專是我換屬了一種思路,用了個函數實現了,你看下,代碼是python下的:
importre
t='<html>asdfasdf<head>1111111111<body><p>asdfasdfasdf</p><imgherf="fff">'
defreplace_two(m):
"""
#過濾掉頁面中除了<p></p>和<img>以外所有的標簽
"""
all=re.findall(r'</?.*?>',m)
save=re.findall(r'</?(?:img).*?>|</?[pP]*?>',m)
foreinall:
ifenotinsave:
m1=m.replace(e,'')
m=m1
returnm
printreplace_two(t)
Ⅲ java中如何替換<p>標簽
你說的問題不夠表達清楚,是在html替換,還是java中字元替換啊,反正因該都一樣,要麼用正則表達化,要麼用String.replace(char oldChar, char newChar);//這樣解決,腳本裡面好像沒有這個方法,只能用正則表達式了
Ⅳ java正則表達式過濾html p標簽
用JavaScript方法如下,JAVA語言類似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')
Ⅳ JAVA正則表達式,去掉<p><p class=MsoNorma>內容</p></p> ,把標簽都去掉只留內容。
取出>([^<]+)即可,其他都不要
Ⅵ java導出excel會有<p>標簽,怎麼處理掉
在程序中將這些字元替換掉即可,代碼如下:
for(int i=10;i<14;i++)
{
str = str.replaceAll(String.valueOf((char)i), "");
}
Ⅶ java 中把下面的這段字元串中的 <P>標簽 , 和後面的那堆字母數字去掉。
正則替換:
s.replaceAll("<.*?>", "");
Ⅷ java 如何替換字元串中的特殊字元<p></p>標簽和空格,字元串是這樣的:<p><img>abcde</img> </p>
<p><img>abcde</img> </p>你想怎麼替換?
是替換所有字元還是只替換<p></p>標簽和空格。
public class RegTest {
public static void main(String[] args) {
String str1="<p><img>abcde</img> </p>";
System.out.println(str1.replaceAll("(<(\\/)?p>)|\\s", ""));
}
}
就可以了。
Ⅸ java去除文本內容的標簽跟
這個方法是替換內容,最後trim方法是,清除字元串兩邊的空格
String newStr =str.replaceAll("<p>", "").replaceAll("</p>", "").trim();
Ⅹ java如何用正則去除<p>標簽後面的空格
java零寬斷言里的正則必須指定最大長度,我就寫了個100,匹配p標簽內可能含有的其他字元,應該夠用了
Stringp="<pid="test"class="para">今天我在圖書館加班。</p>";
p=p.replaceAll("(?<=<p.{0,100}>)[]+(?=\S)","");
System.out.println(p);