> How to delete all comment line in one command line(C/C++)?
>
> Before:
>
> //////
> /*comment1*/
> //
> int Fun()
> {
>       int a = 5; // number 1;
>       int b = 10;// number 2;
>       return a+b;// number 1 + number 2;
> }
>
> After:
> int Fun()
> {
>       int a = 5;
>       int b = 10;
>       return a+b;
> }     

Well, for just the simple cases, you could use something like

  :%s!\s*//.*\|/\*\_.\{-}\*/

To make that more understandable, that's

  \s*//.*
  \|
  /\*\_.\{-}\*/

where the first piece gets C++ style comments, and the
second/third bit finds C-style comments.

However, that doesn't catch odd things like:

  printf("In C++, comments can be written with //\n");

  printf("In C, /* this is a comment */\n");

  #ifdef 0
  printf("this is commented-out code\n");
  #endif

It breaks the first one because it's not truely a comment, but a
string containing the comment characters.  However, the above
regexp would mistakenly turn that into

  printf("In C++, comments can be written with //

which will throw off your whole code-base :)

In the second one, it's not quite as dire, but you still are
deleting non-comments.

In the third one, you didn't mention using "#ifdef 0" or any of
its variants as a comment.  I've seen this as a fairly common way
to comment out large blocks of code which might contain "real"
comments.

For any of these more complex cases, a more complex solution
would be required.

-tim






Reply via email to