Saturday, June 16, 2007

Regular expression specifics in VS2005 Find and Replace

I find myself quite often creating "shallow" (or poco) classes, up to 10 fields, that should have properties and normally one default and one "complete" constructor.

set of private fields:

          private string service;
          private string phase;

easy step to a constructor:

    public SystemMessage(
            string service,
            string phase
            )

by copying from the ctor signature I come to a constructor body:

        this.service,
        this.phase

Final step is to achieve:

            this.service = service;
            this.phase = phase;

 

Everything above the final step is very straight forward. The final step was contradicting with my laziness when I was doing copy and paste line by line.

As it was a weekend day, I decided to take a look into VS2005 find and replace capacities.

My first impression even was that I can't use substituting groups in the Replace. Opening the help for this I saw - VS2005 has got regular expressions with its own twist :).

My expressions then to achieve the final step are:

Find expression:
    this\.{.@},@$
Replace expression:
    this.\1 = \1;

Is not this different from the regex syntax? I wonder what is the story behind those differences :).

Resources:

Regular Expressions (Visual Studio)

Technorati Tags: ,

P.S Another sample:

Change method name and add an extra parameter to the method call:

MessagesProvider.GetMessageForEnumValue\({.@},@\).Text
MessagesProvider.GetMessageForEnum(\1, DQV.Apps.Common.Service.Client).Text

Post a Comment