Search Visual Studio Regular Expressions

http://www.intertech.com/Blog/visual-studio-regular-expressions-search/

I’m sure you’ve all seen the “Use: Regular expressions” option in the Visual Studio search dialog, but maybe you haven’t taken advantage of it. You can use this feature to do some pretty powerful search and replace operations in Visual Studio.

I used this feature today and thought I’d share. I had a situation where I was trying to find every  control (label, textblock, button or tabitem) in the XAML of my WPF UI that did not have a localized string assigned to it. I knew that the controls that were localized looked something like the following:

    <TextBlock Text="{x:Static Member=Strings:UserStringTable.LblDescription}"... />
    <Label Content="{x:Static Member=Strings:UserStringTable.LblDescription}"... />
    <TabItem Header="{x:Static Member=Strings:UserStringTable.LblDescription}"... />

(i.e. that they all referenced a static member called Strings:UserStringTable) and those that didn’t looked something like this:

    <TextBlock Text="Description:"... />
    <Label Content="Description:"... />
    <TabItem Header="Description"... />

So how can use Visual Studio to find all of those controls (TextBlock, Label, TabItem, Button, etc) with one search string and exclude the items that have already been localized? The answer is regular expressions in Visual Studio.

I know that most WPF controls have at Text, Content, Header or ToolTip attribute for assigning text that should be shown to the user. I also know that localized strings begin with =”{. Knowing that, I can put together a regular expression to find only those controls that need to be localized.

Here’s the Visual Studio Search dialog I used to accomplish that search (the regular expression I used is described below–if you’re already familiar with how regular expressions work and their syntax, feel free to skip past my diatribe on the breakdown of my regular expression search string):


So what am I telling the search dialog to look for in this regular expression?

( Content| Text| Header)\=\”[^{]

First let’s describe the regular expression tokens we’re using in this string:

( )

The parenthesis to execute whatever’s inside them as a group. This allows me to put multiple search expressions into a single group.

\=

The equal sign “=” is a reserved token in regular expression syntax, so we have to precede it with a “\” to tell the regular expression we are looking for an equal sign in our text. This is also known as escaping the token.

\”

Same deal here, the quote is a reserved token, so we have to escape it as well.

[ ]

These brackets are used to tell Visual Studio that the stuff inside them is a list, and that I want you to search for each character in that list separately. For example, if I had a string that says “This, that and the other thing” and I had a regular expression search string that looks like this “th[iae]” Visual Studio would find “This”, “That”, “the“, “other” and “thing”. It would not return the word “and” in the results.

[^ ]

The caret ^ actually has multiple meanings, but inside square brackets it says NOT, or exclude. So sticking with our example above, if my regular expression was “th[^iae]” nothing would be found.

So now let’s break down the regular expression I used in the search above. Here’s the regular expression again:

( Content| Text| Header)\=\”[^{]

Here’s what it says, piece by piece:

( Content| Text| Header)

Here we say we’re looking any string that contains one of these string ” Content”, ” Text” or ” Header” in them.

\=\”

Any string found with one of the items in the group above should be followed by an equal sign and a quote. Note that we’re escaping them because they are reserved tokens.

[^{]

Finally, we want to ignore any string that has a left curly bracket “{” after the equal sign and quote.

So, if I have XAML that had the following controls in it:

    <TextBlock Text="{x:Static Member=Strings:UserStringTable.LblDescription}"... />
    <Label Content="{x:Static Member=Strings:UserStringTable.LblDescription}"... />
    <TabItem Header="{x:Static Member=Strings:UserStringTable.LblDescription}"... />
    <TextBlock Text="Description: "... />
    <Label Content="Description: "... />
    <TabItem Header="Description"... />

The only strings it should find are:

    <TextBlock Text="Description: "... />
    <Label Content="Description: "... />
    <TabItem Header="Description"... />

You can get pretty powerful with regular expressions in Visual Studio, especially when it comes to search and replace.

More information can be found on the web about how to do this. The MDSN documentation for the Visual Studio regular expression syntax can be found at http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.80).aspx. The Visual Studio syntax is a modified version of the standard regular expression syntax that most regular expression engines use, especially when it comes to replacing text, but in most cases if you apply what you already know about regular expressions it should work. Not to worry, if you have something wrong in the syntax Visual Studio will let you know in a hurry.

Note: if the whole you search include ".", you should use "\."

for example: search for "customer.aspx" then the regular expression is (customer\.aspx)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值