Regex for HTML with java.util.regex
I need a regex for following html :
<div xmlns="http://www.w3.org/1999/xhtml"> <p/>
<p/><p/> <p/>
</div>
This comes form a r开发者_开发知识库ichtext field and obviously this is no meaningful content or means: empty. I can not say in java: if (richTextConent == null || richTextContent.length == 0) because the richtext field contains something. Semantically the above content is empty so i thought of using a regex. I need to match this snippet with java.util.regex
If there is something meaningful in the snippet like:
<div xmlns="http://www.w3.org/1999/xhtml"> text<p/>
<p/><p/>text <p/>
</div>
than the regex should not match.
Use a HTML parser like Jsoup.
String html1 = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> <p/> <p/><p/> <p/></div>";
String html2 = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> text<p/> <p/><p/>text <p/> </div>";
System.out.println(Jsoup.parse(html1).text().isEmpty()); // true
System.out.println(Jsoup.parse(html2).text().isEmpty()); // false
See also:
- Should I parse (X)HTML with regex?
- HTML parsers in Java
- Another Jsoup example
精彩评论