-3

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

aizaz
  • 3,056
  • 9
  • 25
  • 57
Kumar
  • 23
  • 1
  • 8
  • 1
    How does your first set of 4 lines get removed? `/\*` doesn't look like the start of a comment to me; looks more like a syntax error. – Chris Sinclair Jun 04 '13 at 12:17
  • 5
    http://WhatHaveYouTried.com We are volunteers who want to help you with a specific problem. We donate our time; donate some of yours and show that you at least tried **something** – Eonasdan Jun 04 '13 at 12:19
  • Can `//` comments be continued using an escaped linebreak (`//comment \ `)? – Tim Pietzcker Jun 04 '13 at 12:19
  • -1 for not showing any effort yourself. – Daniel Hilgarth Jun 04 '13 at 12:21
  • Follow [this](http://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689). Hope this helps. [1]: http://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689 – Mask Jun 04 '13 at 12:29

2 Answers2

0

If your file is formatted like that just do a read line and check with string.containt(...) if "/, *," are contains by your string.

If it's the case just remove it.

TIPS: Stock the good line in list to display your output.

Hope this could help you.

If not sorry.

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
3wic
  • 500
  • 6
  • 22
  • I don't think this will work if he has a multi-line comments, because there is no syntactical obligation to have a `*` on each line. – jszigeti Jun 04 '13 at 12:32
  • I based my self on his example file. By the way, just check this post : http://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689 You will find an answer there. – 3wic Jun 04 '13 at 12:39
  • This also will fail if the comment start sequence (i.e. `/*` or `//`) occurs inside a quoted string. – Jim Mischel Jun 04 '13 at 12:41
  • can you please tell me , for multiline comment(/*.*/), remove string between comment, i tried but its not working if (s.IndexOf("/*")!=-1) { s = s.Remove(s.IndexOf("/*"), s.IndexOf("*/") - s.IndexOf("/*") + 2); } – Kumar Jun 06 '13 at 12:08
0

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).

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • @user2156184, you bet, and I'm glad I could be of assistance! – Mike Perrenoud Jun 04 '13 at 19:35
  • can you please tell me , for multiline comment(/*.*/), remove string between comment, i tried but its not working,where s i sstring. if (s.IndexOf("/*")!=-1) { s = s.Remove(s.IndexOf("/*"), s.IndexOf("/") - s.IndexOf("/") + 2); } – Kumar Jun 06 '13 at 12:09
  • @user2156184, you should be working with `Regex`, so you would leverage the `Regex` class and its subsequent `Matches` class that's returned when creating a match. The Regex I provided puts the actual text of the *entire comment* in a group, you could easily modify that to just be the contents minus the comment indicators by moving the parenthesis in a couple characters. – Mike Perrenoud Jun 06 '13 at 12:12
  • without using regex, how it is possible please? am not able to delete rows in between comment, thanks – Kumar Jun 06 '13 at 12:57
  • @user2156184, it's not going to be all that possible with Regex. It's a string matching problem that spans lines. – Mike Perrenoud Jun 06 '13 at 13:00
  • what i did is, static string StripComments(string code) { var re = @"(^\/\/.*?$)|(\/\*.*?\*\/)"; return Regex.Replace(code, re, "$1"); } while (s.Contains("#define")) {s= StripComments(s);} , is it correct what i did? thanks – Kumar Jun 06 '13 at 13:13