Will this regex be enough to remove C ++ multi-line comments?

I need to parse some files in C ++, and to make it easier for me, I was thinking about deleting multi-line comments. I tried the following regular expression: /(\/\*.*?\*\/)/using a multi-line modifier, and it seems to work. Do you think there will be any case when he fails?

+3
source share
4 answers

The following actions may harm you:

std::cout << "Printing some /* source code */" << std::endl;

This is a good example. Imagine the damage you could do if a line started a comment and didn't end? You can end up removing huge chunks of your code.

" " ( " ", "" ", ), . , .

EDIT: @MSalters, , , , , , , , , . , , ++, . file.cpp, (- ):

cpp file.cpp

, #include s , , , , . , cpp C ( #include #define .. C), , , GCC, :

gcc -E file.cpp

( gcc g++, - #include <iostream> .)

, , , , (, GCC , , ). , , , , .

, . , .

+10

, :

//**** 
some code
/*
  comments
*/

, .

+5

. . , , , , : ", ".

// This is a single line\
comment

. , , , :

// This is also a single-line??/
comment

, . , , , :

#include <all_headers/*>

, , ...

, , :

%:include <all_headers/*>

:

%:include<all_headers??/*>

, :

#include <all_headers\*>
+3

Not trying to revive the old tides, but if I came across him, hey! As a more specific answer to the @Chris Lutz g ++ method:

g++ -fpreprocessed -E -P MyProgram.cpp -o NoComments.cpp

This will simply remove the comments and change the spaces by sending the new code to "NoComments.cpp" without any other crud.

+2
source

Source: https://habr.com/ru/post/1720274/


All Articles