我在找到多行HTML文件中的空HTML元素時遇到了問題。我的正則表達式是這樣的:Java正則表達式模式找到空的HTML/XML元素
Pattern pattern = Pattern.compile("<([a-zA-Z][a-zA-Z0-9]*)[^>]*?>[\\s]*?</\\1>");
Matcher matcher = pattern.matcher(htmlOut);
while (matcher.find())
{
htmlOut = matcher.replaceAll("");
matcher = pattern.matcher(htmlOut);
}
問題是它不匹配任何空標籤。
僅供參考:同樣的正則表達式<([a-zA-Z][a-zA-Z0-9]*)[^>]*?>[\s]*?</\1>
適用於崇高的文本!
任何方法?
Obligatory正則表達式/ Html回覆:http://gackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – glenatron 2012-07-24 11:07:46
@glenatron:刪除空標籤是在正則表達式的可能範圍之內。即使使用HTML。 – 2012-07-24 11:26:48