Java – regular expression finding comments in code

You may have already given up on this by now but I was intrigued by the problem. I believe this is a partial solution… Native regex: //.*|(“(?:\\[^”]|\\”|.)*?”)|(?s)/\*.*?\*/ In Java: String clean = original.replaceAll( “//.*|(\”(?:\\\\[^\”]|\\\\\”|.)*?\”)|(?s)/\\*.*?\\*/”, “$1 ” ); This appears to properly handle comments embedded in strings as well as properly escaped quotes inside strings. I … Read more

What is self-documenting code and can it replace well documented code? [closed]

Well, since this is about comments and code, let’s look at some actual code. Compare this typical code: float a, b, c; a=9.81; b=5; c= .5*a*(b^2); To this self-documenting code, which shows what is being done: const float gravitationalForce = 9.81; float timeInSeconds = 5; float displacement = (1 / 2) * gravitationalForce * (timeInSeconds … Read more

Regex for comments in strings, strings in comments, etc

I’ve broken the regex into 4 lines corresponding with the 4 paths in the graph, don’t keep those line-breaks in there if you ever use this. ([‘”])(?:(?!\1|\\).|\\.)*\1| \/(?![*/])(?:[^\\/]|\\.)+\/[igm]*| \/\/[^\n]*(?:\n|$)| \/\*(?:[^*]|\*(?!\/))*\*\/ Debuggex Demo This code grabs 4 types of “blocks” that can contain the other 3. You can iterate through this and do with each one … Read more

How do I comment out a block of tags in XML?

You can use that style of comment across multiple lines (which exists also in HTML) <detail> <band height=”20″> <!– Hello, I am a multi-line XML comment <staticText> <reportElement x=”180″ y=”0″ width=”200″ height=”20″/> <text><![CDATA[Hello World!]]></text> </staticText> –> </band> </detail>

Does the C preprocessor strip comments or expand macros first? [duplicate]

Unfortunately, the original ANSI C Specification specifically excludes any Preprocessor features in section 4 (“This specification describes only the C language. It makes no provision for either the library or the preprocessor.”). The C99 specification handles this explicity, though. The comments are replaced with a single space in the “translation phase”, which happens prior to … Read more

Best way to automatically remove comments from PHP code

I’d use tokenizer. Here’s my solution. It should work on both PHP 4 and 5: $fileStr = file_get_contents(‘path/to/file’); $newStr=””; $commentTokens = array(T_COMMENT); if (defined(‘T_DOC_COMMENT’)) { $commentTokens[] = T_DOC_COMMENT; // PHP 5 } if (defined(‘T_ML_COMMENT’)) { $commentTokens[] = T_ML_COMMENT; // PHP 4 } $tokens = token_get_all($fileStr); foreach ($tokens as $token) { if (is_array($token)) { if (in_array($token[0], … Read more