我有一个HTML输入:
<font size="5"><p>some text</p>
<p> another text</p></font>
我想使用正则表达式移除HTML标签,以使输出变成:some text
another text
有人能建议如何使用正则表达式来实现这个吗?
我有一个HTML输入:
<font size="5"><p>some text</p>
<p> another text</p></font>
我想使用正则表达式移除HTML标签,以使输出变成:some text
another text
有人能建议如何使用正则表达式来实现这个吗?
既然你问了,这里有一个简单而快速的解决方案:
String stripped = input.replaceAll("<[^>]*>", "");
(Ideone.com演示)<tag attribute=">">Hello</tag>
<script>if (a < b) alert('Hello>');</script>
Jsoup.parse(html).text()
。>
作为文字字符使用。 - Gumbo使用HTML解析器。这里是一个Jsoup示例。
String input = "<font size=\"5\"><p>some text</p>\n<p>another text</p></font>";
String stripped = Jsoup.parse(input).text();
System.out.println(stripped);
结果:
一些文本 另一些文本
或者如果你想保留换行符:
String input = "<font size=\"5\"><p>some text</p>\n<p>another text</p></font>";
for (String line : input.split("\n")) {
String stripped = Jsoup.parse(line).text();
System.out.println(stripped);
}
结果:
一些文本 另一些文本
Jsoup还提供了更多优势。您可以使用select()
方法轻松提取HTML文档的特定部分,该方法接受类似于jQuery的CSS选择器。它只需要文档是语义上良好形成的。 1998年以来已弃用的<font>
标记的存在已经不是很好的指示,但如果您预先深入了解HTML结构,仍然可以做到。
从aioobe的代码开始,我尝试了更大胆的东西:
String input = "<font size=\"5\"><p>some text</p>\n<p>another text</p></font>";
String stripped = input.replaceAll("</?(font|p){1}.*?/?>", "");
System.out.println(stripped);
public class HtmlSanitizer {
private static String pattern;
private final static String [] tagsTab = {"!doctype","a","abbr","acronym","address","applet","area","article","aside","audio","b","base","basefont","bdi","bdo","bgsound","big","blink","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","content","data","datalist","dd","decorator","del","details","dfn","dir","div","dl","dt","element","em","embed","fieldset","figcaption","figure","font","footer","form","frame","frameset","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","isindex","kbd","keygen","label","legend","li","link","listing","main","map","mark","marquee","menu","menuitem","meta","meter","nav","nobr","noframes","noscript","object","ol","optgroup","option","output","p","param","plaintext","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","shadow","small","source","spacer","span","strike","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","tt","u","ul","var","video","wbr","xmp"};
static {
StringBuffer tags = new StringBuffer();
for (int i=0;i<tagsTab.length;i++) {
tags.append(tagsTab[i].toLowerCase()).append('|').append(tagsTab[i].toUpperCase());
if (i<tagsTab.length-1) {
tags.append('|');
}
}
pattern = "</?("+tags.toString()+"){1}.*?/?>";
}
public static String sanitize(String input) {
return input.replaceAll(pattern, "");
}
public final static void main(String[] args) {
System.out.println(HtmlSanitizer.pattern);
System.out.println(HtmlSanitizer.sanitize("<font size=\"5\"><p>some text</p><br/> <p>another text</p></font>"));
}
}
优点:
缺点:
如果您发现其他缺点,请务必告诉我。
public String extractAllText(String htmlText){
Source source = new Source(htmlText);
return source.getTextExtractor().toString();
}
元素
也可以做同样的事情:for (Element link : links) {
System.out.println(link.getTextExtractor().toString());
}