Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all comment (single-/multi-line) & blank lines from source file [duplicate]

Tags:

comments

c#

regex

How can I remove all comments and blank lines from a C# source file. Have in mind that there could be a nested comments. Some examples:

string text = @"//not a comment"; // a comment

/* multiline
comment */ string newText = "/*not a comment*/"; // a comment

/* multiline // not a comment 
/* comment */ string anotherText = "/* not a comment */ // some text here\"// not a comment"; // a comment

We can have much more complex source than those three examples above. Can some one suggest a regex pattern or other way to solve this. I've already browsed a lot a stuff over the internet and coudn't find anything that works.

like image 314
nenito Avatar asked Feb 02 '12 13:02

nenito


People also ask

What is multiline and single line comment?

Multi-line Comment ExamplesMulti-line comments have one or more lines of narrative within a set of comment delimiters. The /* delimiter marks the beginning of the comment, and the */ marks the end. You can have your comment span multiple lines and anything between those delimiters is considered a comment.

How do you delete multiple lines in a comment in Python?

To comment out multiple lines in Python, you can prepend each line with a hash ( # ).


1 Answers

To remove the comments, see this answer. After that, removing empty lines is trivial.

like image 162
sga101 Avatar answered Oct 14 '22 19:10

sga101