12

Can anyone tell me how to write a regular expression to match the comments in XML file, for example,

<!-- Global JNDI resources
   Documentation at /docs/jndi-resources-howto.html
-->

I'm using Eclipse (java) and want to remove those comments from XML file.

3 Answers 3

21
xmlFileContent.replaceAll( "(?s)<!--.*?-->", "" );
Sign up to request clarification or add additional context in comments.

Comments

12
xmlFileContent.replaceAll("<!--[\s\S]*?-->", "");

[\s\S] works like '.', but will consume line endings as well.

2 Comments

+1 I think this is the correct answer. Others wouldn't even work for the given example. But shouldn't it be "<!--[\\s\\S]*?-->" so the compiler doesn't escape those characters?
Although MVH's answer equals with nobody's answer in functional point of view but there is quite big difference when performance take into account. nobody's answer win in this case because it is ~3 times faster (executing 100000 cycles: 127ms vs. 395ms) so +1 to nobody's answer.
1
xmlFileContent.replaceAll("<!--.*?-->", "");

3 Comments

Thanks, but this only removes those comments that fits in a line. How to also include those multi-line comments?
nobody's answer (5 hours later then Murali's answer) already includes the DOTALL flag
Late to the game, but this looks like it would replace the first <!-- everything between it, until the last -->. May not be the best approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.