in reply to Modify C comment removal code to kill newlines
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Modify C comment removal code to kill newlines
by anjiro (Beadle) on Aug 21, 2006 at 14:39 UTC |