简体   繁体   中英

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 richtext 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM