fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. // identifies empty tag i.e <tag1></tag> or <tag/>
  11. // it also supports the possibilities of white spaces around or within the tag. however tags with whitespace as value will not match.
  12. private static final String EMPTY_VALUED_TAG_REGEX = "\\s*<\\s*(\\w+)\\s*></\\s*\\1\\s*>|\\s*<\\s*\\w+\\s*/\\s*>";
  13.  
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. String inputXml = "<a>\n<b>value </ b>\n< c ></ c >\n</a>\n< d />\n<e>\n</e>\n<f/>\n<g>value2</g>";
  17. System.out.println("output: " + inputXml.replaceAll(EMPTY_VALUED_TAG_REGEX, ""));
  18. }
  19. }
Success #stdin #stdout 0.1s 36136KB
stdin
Standard input is empty
stdout
output: <a>
<b>value </ b>
</a>
<e>
</e>
<g>value2</g>