Kumar Kumar - 13 days ago 15
C# Question

delete multi line comments line c# "// or /*...*/"

can any one please help me , how to remove lines if it has comments like '//' or single or multi line comments '/*...*/ '

For example:

/\*#define 0x00000000*/

//#define 0x00180000

// #define 0x20000000

// abcd

/*#define 0x00080000

#define 0x40000000*/

#define 0x00014000

#define 0x00000800
/* defg */

#define 0x00080000

#define 0x40000000


The output should be

#define 0x00014000

#define 0x00000800

#define 0x00080000

#define 0x40000000


in C# using Regex or any other method, thanks in advance

Answer

Alright, this Regex (^\/\/.*?$)|(\/\*.*?\*\/) (Rubular proof) will match (and potentially remove if you use Visual Studio and replace it with nothing) the following lines out of your example text:

//#define                0x00180000

// #define                0x20000000

// abcd

/*#define                0x00080000

   #define               0x40000000*/

/* defg  */

and almost gets you what you want. Now, I'm suspect of this line /\*#define 0x00000000*/, but if you wanted to capture it as well you could modify the Regex to be (^\/\/.*?$)|(\/.*?\*.*?\*\/) (Rubular proof).