2

Is there a macro in the Visual Studio editor to remove all comments from a VB.NET source file?

2
  • Just curious. Why would you want to do this? Commented Apr 1, 2010 at 19:50
  • I have refactored a module I've been working on - whereby I commented out X lines of code and then rewrote that code above the comment. Now that my code passes all tests I want to remove the commented out code and add appropriate comments to the functions. Commented Apr 1, 2010 at 19:55

3 Answers 3

4

Using menu Edit -> Find and Replace -> Quick Replace with Regular expressions

Find what: ^{.+}'.+$

Replace with: \1

will replace

text ' comment

to

text
Sign up to request clarification or add additional context in comments.

7 Comments

@abatishchev - It removes the entire line! Which I obviously do not want. e.g. Dim path As String ' temporary file path - would remove the entire line.
@roygbiv: My first variant will remove the whole line. The current one - will not
Highlights on Find next? Right. The whole line matches. And it will be replaced with the part of it - from the beginning up to the comment sign
Just tried out your regex. Works great. There is a problem though, if the commented line is like '' This is a comment then it only deletes ' This is a comment. There is still a ' left in the line.
@Soham: Maybe [']+ will work, should mean ' 1 times or more
|
0

EDIT*

http://bytes.com/topic/visual-basic-net/answers/579000-utility-remove-comments-vb-net-files

has some options.

such as

  • write a VB.NET program to do it? should be easy: any line with a single quote as the first character should be removed. and everything AFTER a single quote (even if it's not the first character), provided the quote is not between a pair of double quotes. and the files you send to this program are any *.vb files.
  • search and replace with a regular expression would probably be quickest.

1 Comment

Interesting suggestions. I knew it would come down to this. Thought there was an easier point and click way.
0

Find: ^{(([^"']*"[^"]*"[^"'])#|[^"']*)}'.*$

Replace with \1
will also work for stuff like this (SQL):

   cs = "INSERT INTO db (time, t1, t2) VALUES ('" & Time.ToString & "', NULL, '" & t2 & "')" ' COMMENT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.