Michel van Engelen
Michel van Engelen

Reputation: 2869

Visual Studio 2008 search and replace regex

I have a large solution with a lot of lines that I need to replace. In Visual Studio, you can search and replace with the aid of regular expressions.

I want to replace lines like:

rst.Fields("CustomerName").Value
rst.Fields("Address").Value
rst.Fields("Invoice").Value

To:

row("CustomerName").ToString()
row("Address").ToString()
row("Invoice").ToString()

Thus keeping the dynamic text part, which can vary.

Is this possible and how?

Update, solution:
Search: rst.Fields{\(.*\)}\.Value
Replace: rst\1.ToString()

Thanks JaredPar!

Upvotes: 7

Views: 2213

Answers (3)

John M
John M

Reputation: 14688

The answer and solution provided helpful in doing a find-replace on messageboxes.

This worked in Visual Studio 2008 (VB .NET):

Example:

MessageBox.Show("Invalid Entry","Error")

Find What:

MessageBox.Show{(.*,*)}

Replace WIth:

Error.ShowError\1\2

Results in:

Error.ShowError("Invalid Entry","Error")

Upvotes: 3

zainnab
zainnab

Reputation: 1871

Looks like you have it nailed. It's what is called a "tagged expression" and you can see another example here: http://blogs.msdn.com/b/zainnab/archive/2010/09/12/replace-in-files-tagged-expressions-vstipfind0016.aspx

Upvotes: 2

JaredPar
JaredPar

Reputation: 755557

Try the following

  • Search Expression: ASpecificCommand(\(.*\))\.ASpecificProperty
  • Replace Expression: ATotallyDifferentCommand\1.ATotallyDifferentProperty

Note: This is not a perfect solution. Since there are (s involved and hence matching of nested parens, a regex won't ever be a perfect solution. However it should get the job done for the specific pattern you posted

Upvotes: 5

Related Questions