I wrote a method to remove comments from java code in order to compare two programs. There is a regular expression that can do this but I didn't know it at the time. This method does not ignore 'comment' strings inside quoted strings, as it should, but it was good enough for my purpose:
Joined: Oct 14, 2008
I'm a fan of using lexers for parsing text (using JFlex, for example), and I think this problem would be a perfect candidate for one. It'd also make a good introduction on how to create lexers since it's not a very involved problem. The result would be easier to maintain -I'd argue that the above code is not very maintainable by someone who didn't write it- and it would be much easier to extend (in case the underlying parsing becomes more complex, as -over time- is usually the case).
Joined: Oct 13, 2005
Welcome to JavaRanch Glenn Strickland
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: How to ignore or remove comments from a java file with the help of another java file.