regex - Java - regular expression finding comments in code -


a little fun java time. want write program reads code standard input (line line, example), like:

// comment class main {     /* blah */     // /* foo     foo();     // foo */     foo2();     /* // foo2 */ } 

finds comments in , removes them. i'm trying use regular expressions, , i've done this:

private static string parsecode(string pcode) {     string mycommentsregex = "(?://.*)|(/\\*(?:.|[\\n\\r])*?\\*/)";     return pcode.replaceall(mycommentsregex, " "); } 

but seems not work cases, e.g.:

system.out.print("we can use /* comments */ inside string of course, shouldn't start comment"); 

any advice or ideas different regex? in advance.

you may have given on intrigued problem.

i believe partial solution...

native regex:

//.*|("(?:\\[^"]|\\"|.)*?")|(?s)/\*.*?\*/ 

in java:

string clean = original.replaceall( "//.*|(\"(?:\\\\[^\"]|\\\\\"|.)*?\")|(?s)/\\*.*?\\*/", "$1 " ); 

this appears handle comments embedded in strings escaped quotes inside strings. threw few things @ check not exhaustively.

there 1 compromise in "" blocks in code end space after them. keeping simple , solving problem difficult given need cleanly handle:

int/* comment */foo = 5; 

a simple matcher.find/appendreplacement loop conditionally check group(1) before replacing space , handful of lines of code. still simpler full parser maybe. (i add matcher loop if interested.)


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -