Visual Studio统计有效代码行数

突然看到一篇文章,

看网上有人专门做了一些小工具,用来统计代码行数。感觉不是很必要。因为Visual Studio中的搜索功能支持正则表达式(虽然语法比较诡异),我们完全可以通过正则表达式来遍历整个解决方案从而获得代码行数。

^:b*[^:b#/]+.*$

需要注意:#开头和/开头或者空行都不计入代码量。

云云,擦,这不就是说我么?

当时废了好大劲呢

(可以做到剔除所有的注释,统计代码字数,不过ms不太需要)。。。

但激起了很大的兴趣,好歹以前做网站时记得看过一些正则表达式的,

可怎么也看不懂这句话,流水不流,户枢不转,看来全还给书了。。。

 

好歹看一番,

^ $    代表字符串的开始结束,也就是一行代码

[]   代表筛选条件

^:b#/  代表除了*和/两个字符之外的所有字符

+   代表至少出现一次

.*      代表后面出现的>=0个字符

讲解完毕,这样很好理解了吧?

^:b*[^:b#/]+.*$

这则正则表达式的含义就是取出每一行不以“#或/”开头的代码,

配合Ctrl+Shift+F总数一下一下就出来了~~

当然举一反三,为了标新立异,我们也可以这样写

^:b*[\\\{\}\*\&\!\^A-Za-z]+.*$

但似乎不太可取,尤其是代码换行不太规范的~~

正则表达式的作用还有极多,这里就不再多做介绍了。。。

 

 

8/14/2014

 注:最新VS13中正则表达式有所改动:

Using Regular Expressions in Visual Studio

Visual Studio 2013
 

Visual Studio uses .NET Framework regular expressions to find and replace text. In Visual Studio 2010 and earlier versions, Visual Studio used custom regular expression syntax in the Find and Replace windows. This topic explains how to convert some of the more commonly-used custom regular expression symbols to the .NET versions.

Tip Tip

In Windows operating systems, most lines end in “\r\n” (a carriage return followed by a new line). These characters are not visible, but are present in the editor and are passed to the .NET Regular Expression service.

For more information, see .NET Framework Regular Expressions.

Tip Tip

For information about regular expressions that are used in replacement patterns, see Substitutions in Regular Expressions. To use a numbered capture group, the syntax is $1 to specify the numbered group and (x) to specify the group in question:. For example, the grouped regular expression (\d)([a-z]) finds four matches in the following string: 1a 2b 3c 4d. The replacement string z$1 converts that string to z1 z2 z3 z4> The equivalent syntax in Visual Studio 2010 is {:z}([a-z]) for the grouped regular expression and z\1 for the replacement string.

Comparing.NET Framework Regular Expressions

 

You can convert regular expressions from the version used in previous versions of Visual Studio to .NET Framework regular expressions by using the following table.

 

Purpose

New

Old

New Example

Match any single character (except a line break)

.

.

a.o matches "aro" in "around" and "abo" in "about" but not "acro" in "across".

Match zero or more occurrences of the preceding expression (match as many characters as possible)

*

*

a*r matches "r" in "rack", "ar" in "ark", and "aar" in "aardvark"

Match any character zero or more times (Wildcard *)

.*

.*

c.*e matches “cke” in “racket”, “comme” in “comment”, and “code” in “code”

Match one or more occurrences of the preceding expression (match as many characters as possible)

+

+

e.+e matches "eede" in "feeder" but not "ee".

Match any character one or more times (Wildcard ?)

.+

.+

e.+e matches "eede" in "feeder" but not "ee".

Match zero or more occurrences of the preceding expression (match as few characters as possible)

*?

@

e.*?e matches "ee" in "feeder" but not "eede".

Match one or more occurrences of the preceding expression (match as few characters as possible)

+?

#

e.+?e matches "ente" and "erprise" in "enterprise", but not the whole word "enterprise".

Anchor the match string to the beginning of a line or string

^

^

^car matches the word "car" only when it appears at the beginning of a line.

Anchor the match string to the end of a line

\r?$

$

End\r?$ matches "end" only when it appears at the end of a line.

Match any single character in a set

[abc]

[abc]

b[abc] matches "ba", "bb", and "bc".

Match any character in a range of characters

[a-f]

[x-y]

be[n-t] matches "bet" in "between", "ben" in "beneath", and "bes" in "beside", but not "below".

Capture and implicitly number the expression contained within parenthesis

()

()

([a-z])X\1 matches "aXa"and "bXb", but not "aXb". ". “\1” refers to the first expression group “[a-z]”.

Invalidate a match

(?!abc)

~(abc)

real (?!ity) matches "real" in "realty" and "really" but not in "reality." It also finds the second "real" (but not the first "real") in "realityreal".

Match any character that is not in a given set of characters

[^abc]

[^abc]

be[^n-t] matches "bef" in "before", "beh" in "behind", and "bel" in "below", but not "beneath".

Match either the expression before or the one after the symbol.

|

|

(sponge|mud) bath matches "sponge bath" and "mud bath."

Escape the character following the backslash

\

\

\^ matches the character ^.

Specify the number of occurrences of the preceding character or group

{x}, where x is the number of occurrences

\x, where x is the number of occurrences

x(ab){2}x matches "xababx", and x(ab){2,3}x matches "xababx" and "xabababx" but not "xababababx".

Match text in a Unicode character class, where “X” is the Unicode number. For more information about Unicode character classes, see

Unicode Standard 5.2 Character Properties.

\p{X}

:X

\p{Lu} matches "T" and "D" in "Thomas Doe".

Match a word boundary

\b (Outside a character class \b specifies a word boundary, and inside a character class specifies a backspace).

< and > specify the beginning and end of a word

\bin matches "in" in "inside" but not "pinto".

Match a line break (ie a carriage return followed by a new line).

\r?\n

\n

End\r?\nBegin matches "End" and "Begin" only when "End" is the last string in a line and "Begin" is the first string in the next line.

Match any alphanumeric character

\w

:a

a\wd matches "add" and "a1d" but not "a d".

Match any whitespace character.

(?([^\r\n])\s)

:b

Public\sInterface matches the phrase "Public Interface".

Match any numeric character

\d

:d

\d matches and "3" in "3456", "2" in 23", and "1" in "1".

Match a Unicode character

\uXXXX where XXXX specifies the Unicode character value.

\uXXXX where XXXX specifies the Unicode character value

\u0065 matches the character "e".

Match an identifier

\b(_\w+|[\w-[0-9_]]\w*)\b

:i

Matches "type1" but not &type1" or "#define".

Match a string inside quotes

((\".+?\")|('.+?'))

:q

Matches any string inside single or double quotes.

Match a hexadecimal number

\b0[xX]([0-9a-fA-F])\b

:h

Matches "0xc67f" but not "0xc67fc67f".

Match integers and decimals

\b[0-9]*\.*[0-9]+\b

:n

Matches "1.333".

http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(vs.netregularexpressionhelp)&rd=true

 

posted @ 2011-11-22 21:07  Zephyroal  阅读(6475)  评论(4编辑  收藏  举报