[Browse] [Tag cloud]

Log on:
Powered by Elgg

.NET :: Blog :: Line Count in Visual Studio for C#

October 04, 2006

I quite often like to do a line count on my final year project and other stuff I am working to figure out where I am up to, but this feature doesn't exist natively in Visual Studio. Luckily, there is a way to do it anyway:

  • Edit -> Find an Replace -> Find in Files
  • Expand Find Options
  • Tick "Use" and select "Regular Expressions"

When you have set up the search, type the following regular expression in the "Find what" box:

^~(:Wh@//.+)~(:Wh@{:Wh@)~(:Wh@}:Wh@)~(:Wh@#).+

Then hit find, Visual Studio will then find every line in every file that matches the regular expression, and if you scroll to the bottom of the find results window, you'll see the number of lines that matched, i.e. your line count.

The regular expression will match all lines of code that aren't single line comments, opening or closing braces, or processor instructions such as #region.

10 points and a pint to the person who tells me how it works, because I'm not sure any more and I wrote the thing ;-)

Keywords: .NET, C#, REGEX

Posted by Philip Stears @ .NET | Share


Comments

  1. user icon

    Whoa

     

    Maybe you need to provide a bit of an explanation as to what a "regular expression" is; there again maybe while you are buying that pint can get the claimant to write the explanation.

    Shirley Williams on Wednesday, 04 October 2006, 21:27 BST # |

  2. user icon

    It works through Magic.

    Self evident no?

    Rob Ashton on Thursday, 05 October 2006, 09:41 BST # |

  3. user icon

    I  can't really remember my .net regex stuff... come to that, I always have to look up any sort of regex syntax whenever I am writing the things.  But anyway, here is my far-too-early-in-the-morning impression of what it does... (which could be entirely wrong!!)

     

    ^~(:Wh@//.+)~(:Wh@{:Wh@)~(:Wh@}:Wh@)~(:Wh@#).+

    Starting at the beginning of the line (^) don't match (~) whitespace (:Wh) followed by a a comment marker (//) and one or more of any character (. matches any character, and + matches one or more of them, iirc)

    Also, don't match whitespace followed by an open-brace, nor whitespace followed by a closing brace, where either of these can be followed by whitespace.

    And finally, don't match any white space followed by a # for compiler directives

    Mind you, having just tried it in my version of VS2005 it doesn't work - it complains "grouped expression is missing ')'", because your braces are unescaped.  But that is probably because of this systems de-unescaping protocol (is there a better name for that?!?)

    Try

    ^~(:Wh@//.+)~(:Wh@/{:Wh@)~(:Wh@/}:Wh@)~(:Wh@/#).+

    P@ Parslow on Friday, 06 October 2006, 07:26 BST # |

  4. user icon

    And as for Shirley's sensible request for a bit of an explanation as to what a regular expression is:

    Regular expressions, or regexes for short, are a way of pattern matching text.  The syntax varies depends on the system you are working with, although a number of things remain fairly consistent.  Probably one of the best ways to learn about them is to install Un*x of some flavour and do all your work in vi, sed and awk.  I guess these days you might use Perl or similar too.  Python has good support.  Actually, if you are at all interested in programming, the chances are you will run into them at some point.  I once wrote a 400 line awk script to convert between two data formats... I believe it is still in use today some 14 years later, which is probably because despite the script being commented, it was still rather hard to understand!

    P@ Parslow on Friday, 06 October 2006, 07:37 BST # |

  5. user icon

    Hi Pat, 10 points and a beer to you. The braces actually were escaped to start with, it would seem that they've been removed by RedGloo but you can have two pints for noticing ;-)

     If you fancy posting your AWK script, feel free, perhaps we need a text parsing and processing community :-) I think most people have written some pretty <sarcastic>lovely</sarcastic> code for dealing with text at some point in their lives.

    Philip James Stears on Friday, 06 October 2006, 10:37 BST # |

You must be logged in to post a comment.

/