Feb072013

Tip: Find C# Comments Without Space After Delimiter

After conducting a Twitter mini-poll, I found that most of my followers agree with me on at least one point: code comments should have a space between the delimiter and the text.

You can perform a RegEx search in Visual Studio to find all instances of comments without the trailing space:

TrailingSpaceRegEx

The RegEx is as follows:

\\s\*(?<!\["':\])//(?!\[\\s/\\-\\\*\])

Performing the replace with "$0 " (notice the trailing space), appends the trailing space while leaving any other formatting (e.g. indents) in place.

Anything with these formats will be replaced:

//Give Me Some Space! //More Space Please var shouldGiveSpace = true; //I'll Back Off ///////////////Break It Up, Man

Anything with these formats will not be replaced:

// Already got some space, thanks // Spaced Out, I'm Good var shouldGiveSpace = false; // I'm all set, thank you var webAddress = @"http://dontaddspacesimawebaddress.com/"; var literalText = "//Literally, no need for spaces.";

[html] [/html]

There are still issues, however. The following example doesn't work as expected:

var literalText = "This gets matched //but it shouldn't.";

Also, this RegEx is designed to work mainly with C# files; no other languages were considered.

If anyone would like to offer critiques or suggestions, feel free!