Here's a little snippet that produces the output you desire. However, it doesn't really take into account nested comments or multiline comments, but you can extend that on your own.
By the way, your last line of sample code /*The end!/*} is not a valid comment terminator. I think you meant /*The end!*/}.
#!/usr/bin/perl use strict; use warnings; undef $/; my $code = <DATA>; # $code =~ s|^\s*/\*.*?\*/||gm; # Changed as per anjiro's comments $code =~ s#^\s*/\*.*?\*/##gm; $code =~ s#(?<=\S)\s/\*.*?\*/\s*?$##gm; $code =~ s#\n##ms; print $code; __DATA__ /*This is a bogus function*/ void function foo(void) /*My function is the best*/ { int i; /*i is an integer*/ int j; /*j is also an integer*/ /*Now I'm going to set i to 1*/ i = 1; /*Also j*/ j = 1; /*Here's some incrementing!*/ i++; /*And more!*/ j++; /*The end!*/}
Hope this helped,
-v.
Update: Changed the regexes to reflect anjiro's comments.
In reply to Re: Modify C comment removal code to kill newlines
by Velaki
in thread Modify C comment removal code to kill newlines
by anjiro
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |