Why not just use the c preprocessor to do this? Why are you confining yourself to a home-grown regex?
[Edit] This approach also handles Barts printf(".../*...")
scenario cleanly
Example:
[File: t.c]
/* This is a comment */
int main () {
/*
* This
* is
* a
* multiline
* comment
*/
int f = 42;
/*
* More comments
*/
return 0;
}
.
$ cpp -P t.c
int main () {
int f = 42;
return 0;
}
Or you can remove the whitespace and condense everything
$ cpp -P t.c | egrep -v "^[ ]*$"
int main () {
int f = 42;
return 0;
}
No use re-inventing the wheel, is there?
[Edit]
If you want to not expand included files and macroa by this approach, cpp
provides flags for this. Consider:
[File: t.c]
#include <stdio.h>
int main () {
int f = 42;
printf(" /* ");
printf(" */ ");
return 0;
}
.
$ cpp -P -fpreprocessed t.c | grep -v "^[ ]*$"
#include <stdio.h>
int main () {
int f = 42;
printf(" /* ");
printf(" */ ");
return 0;
}
There is a slight caveat in that macro expansion can be avoided, but the original definition of the macro is stripped from the source.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…