开发者

Help with Regular Expressions

Im having a hard time finding regular expressions for the following cases. Please help! I basically need regular expressions to find .NET naming convention errors in code, using Visual Studio.

  1. All private variables should start with an underscore. Therefore i need to look for any expression that looks something like this:

    Private #ONL开发者_StackOverflow社区Y ONE WORD HERE# As (String|Integer|Boolean)

  2. All ASP.Net Labels should have ID's prefixed with "lbl"

    <asp\:Label.*id="#ANYTHING OTHER THAN lbl HERE#

How do you negate a specific word? I tried doing ^(lbl) and (^lbl)... they dont work.


EDIT

From a comment below I gather you're using the VS regex search box. This has its completely own regex flavor, different from the .NET engine. Try the following:

1.: Private ~(_)[:a_]+ As (String|Integer|Boolean)

2.: <asp\:Label.*id="~(lbl).*


Original answer kept for archival purposes :)

1.: Private (?!_)\w+ As (String|Integer|Boolean)

2.: <asp\:Label.*id="(?!lbl).*

\w+ is "one or more alphanumeric characters" which probably should do in your case.

(?!lbl) is a negative lookahead assertion, making sure that lbl cannot be matched at the current position.

^ means "start of line/string" outside of character classes. It only means "negation" inside of character classes like [^abc] (= "any character except a, b or c)


Tim's first version for #1 does work for me:

Private \w+ As (Integer|String|Boolean)

For the second approach:

<asp:Label.*id="(?!lbl).*?">

The not closing of the tag should be the reason it didn't match your example properly.

By the way make sure you use the proper modifiers on your expression. At least use 'i'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜